pidsmaker 5.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pidsmaker/__init__.py +1 -0
- pidsmaker/cli.py +81 -0
- pidsmaker/config/__init__.py +2 -0
- pidsmaker/config/config.py +2100 -0
- pidsmaker/config/pipeline.py +1824 -0
- pidsmaker/config/schemas.py +1032 -0
- pidsmaker/datasets.py +323 -0
- pidsmaker/debug_tests.py +30 -0
- pidsmaker/decoders/__init__.py +5 -0
- pidsmaker/decoders/custom_edge_mlp_decoder.py +17 -0
- pidsmaker/decoders/custom_mlp_decoder.py +10 -0
- pidsmaker/decoders/edge_linear_decoder.py +16 -0
- pidsmaker/decoders/inner_product_decoder.py +18 -0
- pidsmaker/decoders/nodlink_decoder.py +56 -0
- pidsmaker/detection/__init__.py +0 -0
- pidsmaker/detection/evaluation_methods/__init__.py +9 -0
- pidsmaker/detection/evaluation_methods/edge_evaluation.py +255 -0
- pidsmaker/detection/evaluation_methods/evaluation_utils.py +1788 -0
- pidsmaker/detection/evaluation_methods/host_two_stage_evaluation.py +115 -0
- pidsmaker/detection/evaluation_methods/node_evaluation.py +459 -0
- pidsmaker/detection/evaluation_methods/node_tw_evaluation.py +196 -0
- pidsmaker/detection/evaluation_methods/plot.py +192 -0
- pidsmaker/detection/evaluation_methods/queue_evaluation.py +456 -0
- pidsmaker/detection/evaluation_methods/tw_evaluation.py +93 -0
- pidsmaker/detection/flash_detection.py +162 -0
- pidsmaker/detection/magic_knn.py +190 -0
- pidsmaker/detection/node_scoring.py +52 -0
- pidsmaker/detection/threatrace_detection.py +65 -0
- pidsmaker/detection/training_methods/__init__.py +1 -0
- pidsmaker/detection/training_methods/inference_loop.py +287 -0
- pidsmaker/detection/training_methods/ocrapt_early_stop.py +165 -0
- pidsmaker/detection/training_methods/threatrace_training.py +168 -0
- pidsmaker/detection/training_methods/training_loop.py +383 -0
- pidsmaker/encoders/__init__.py +22 -0
- pidsmaker/encoders/argus_encoder.py +69 -0
- pidsmaker/encoders/custom_mlp.py +75 -0
- pidsmaker/encoders/custom_mlp_encoder.py +10 -0
- pidsmaker/encoders/egraphsage_encoder.py +56 -0
- pidsmaker/encoders/gat.py +67 -0
- pidsmaker/encoders/gcn.py +27 -0
- pidsmaker/encoders/gin.py +48 -0
- pidsmaker/encoders/glstm.py +298 -0
- pidsmaker/encoders/graph_attention.py +63 -0
- pidsmaker/encoders/gru.py +29 -0
- pidsmaker/encoders/hetero/__init__.py +4 -0
- pidsmaker/encoders/hetero/ancestor_encoder.py +98 -0
- pidsmaker/encoders/hetero/entity_encoder.py +33 -0
- pidsmaker/encoders/hetero/event_encoder.py +81 -0
- pidsmaker/encoders/hetero/hetero_graph_transformer.py +40 -0
- pidsmaker/encoders/lares_encoder.py +68 -0
- pidsmaker/encoders/linear_encoder.py +16 -0
- pidsmaker/encoders/magic_encoder.py +161 -0
- pidsmaker/encoders/rcaid_encoder.py +81 -0
- pidsmaker/encoders/rgcn.py +75 -0
- pidsmaker/encoders/sage.py +51 -0
- pidsmaker/encoders/shadewatcher_encoder.py +149 -0
- pidsmaker/encoders/sum_aggregation.py +20 -0
- pidsmaker/encoders/temporal_wrappers.py +80 -0
- pidsmaker/encoders/textrcnn.py +82 -0
- pidsmaker/encoders/tgn_encoder.py +193 -0
- pidsmaker/experiments/tuning.py +102 -0
- pidsmaker/experiments/uncertainty.py +261 -0
- pidsmaker/factory.py +1456 -0
- pidsmaker/featurization/__init__.py +0 -0
- pidsmaker/featurization/feat_inference_methods/__init__.py +7 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_HFH.py +30 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_TRW.py +65 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_alacarte.py +14 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_doc2vec.py +53 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_fasttext.py +37 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_flash.py +69 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_graph2vec.py +84 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_ocrapt_features.py +95 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_pretrained.py +1728 -0
- pidsmaker/featurization/feat_inference_methods/feat_inference_word2vec.py +55 -0
- pidsmaker/featurization/featurization_methods/__init__.py +0 -0
- pidsmaker/featurization/featurization_methods/build_trw.py +66 -0
- pidsmaker/featurization/featurization_methods/featurization_alacarte.py +747 -0
- pidsmaker/featurization/featurization_methods/featurization_doc2vec.py +98 -0
- pidsmaker/featurization/featurization_methods/featurization_fasttext.py +74 -0
- pidsmaker/featurization/featurization_methods/featurization_flash.py +201 -0
- pidsmaker/featurization/featurization_methods/featurization_graph2vec.py +242 -0
- pidsmaker/featurization/featurization_methods/featurization_trw.py +114 -0
- pidsmaker/featurization/featurization_methods/featurization_word2vec.py +89 -0
- pidsmaker/featurization/featurization_utils.py +275 -0
- pidsmaker/featurization/utils/build_random_walks.py +66 -0
- pidsmaker/featurization/utils/trw.py +203 -0
- pidsmaker/hetero.py +81 -0
- pidsmaker/losses.py +170 -0
- pidsmaker/main.py +478 -0
- pidsmaker/mimicry.py +284 -0
- pidsmaker/model.py +382 -0
- pidsmaker/objectives/__init__.py +16 -0
- pidsmaker/objectives/autoencode_edge_emb.py +43 -0
- pidsmaker/objectives/deep_graph_infomax.py +94 -0
- pidsmaker/objectives/detect_few_shot.py +21 -0
- pidsmaker/objectives/one_class.py +75 -0
- pidsmaker/objectives/predict_edge_contrastive.py +165 -0
- pidsmaker/objectives/predict_edge_supervised.py +77 -0
- pidsmaker/objectives/predict_edge_type.py +28 -0
- pidsmaker/objectives/predict_masked_struct.py +49 -0
- pidsmaker/objectives/predict_node_type.py +36 -0
- pidsmaker/objectives/predict_process_tag.py +169 -0
- pidsmaker/objectives/predict_snapshot_embedding.py +97 -0
- pidsmaker/objectives/reconstruct_edge_emb.py +16 -0
- pidsmaker/objectives/reconstruct_masked_feat.py +51 -0
- pidsmaker/objectives/reconstruct_node_emb.py +13 -0
- pidsmaker/objectives/reconstruct_node_feat.py +13 -0
- pidsmaker/objectives/validation_wrapper.py +55 -0
- pidsmaker/preprocessing/__init__.py +0 -0
- pidsmaker/preprocessing/build_graph_methods/__init__.py +1 -0
- pidsmaker/preprocessing/build_graph_methods/build_default_graphs.py +825 -0
- pidsmaker/preprocessing/build_graph_methods/build_magic_graphs.py +216 -0
- pidsmaker/preprocessing/cleaning.py +258 -0
- pidsmaker/preprocessing/transformation_methods/__init__.py +2 -0
- pidsmaker/preprocessing/transformation_methods/attack_generation/synthetic_attack_naive.py +194 -0
- pidsmaker/preprocessing/transformation_methods/transformation_dag.py +28 -0
- pidsmaker/preprocessing/transformation_methods/transformation_merge_edges.py +136 -0
- pidsmaker/preprocessing/transformation_methods/transformation_rcaid_pseudo_graph.py +510 -0
- pidsmaker/preprocessing/transformation_methods/transformation_undirected.py +24 -0
- pidsmaker/preprocessing/transformation_methods/transformation_utils.py +36 -0
- pidsmaker/pretrained/__init__.py +5 -0
- pidsmaker/pretrained/data/__init__.py +4 -0
- pidsmaker/pretrained/data/behavior_signatures.py +548 -0
- pidsmaker/pretrained/data/canonical_tokens.py +1035 -0
- pidsmaker/pretrained/data/edge_filter.py +381 -0
- pidsmaker/pretrained/data/entity_classes.py +476 -0
- pidsmaker/pretrained/data/sampler.py +926 -0
- pidsmaker/pretrained/data/tokenizer_bpe.py +1659 -0
- pidsmaker/pretrained/detect.py +1427 -0
- pidsmaker/pretrained/embedding_generator.py +252 -0
- pidsmaker/pretrained/finetune.py +1434 -0
- pidsmaker/pretrained/models/__init__.py +20 -0
- pidsmaker/pretrained/models/behavior.py +348 -0
- pidsmaker/pretrained/models/bert.py +286 -0
- pidsmaker/pretrained/models/deepwalk.py +128 -0
- pidsmaker/pretrained/models/dgi.py +120 -0
- pidsmaker/pretrained/models/gae.py +114 -0
- pidsmaker/pretrained/models/gnn_distill.py +475 -0
- pidsmaker/pretrained/models/graphmae.py +373 -0
- pidsmaker/pretrained/models/hf_pretrained.py +213 -0
- pidsmaker/pretrained/models/llama.py +110 -0
- pidsmaker/pretrained/models/modernbert.py +567 -0
- pidsmaker/pretrained/models/roberta.py +147 -0
- pidsmaker/pretrained/models/rope_utils.py +99 -0
- pidsmaker/pretrained/models/ropebert.py +322 -0
- pidsmaker/pretrained/models/spider.py +204 -0
- pidsmaker/pretrained/models/tgn.py +770 -0
- pidsmaker/pretrained/pretrain/__init__.py +3 -0
- pidsmaker/pretrained/pretrain/pretrain_behavior_cluster.py +513 -0
- pidsmaker/pretrained/pretrain/pretrain_common.py +585 -0
- pidsmaker/pretrained/pretrain/pretrain_deepwalk.py +31 -0
- pidsmaker/pretrained/pretrain/pretrain_dgi.py +143 -0
- pidsmaker/pretrained/pretrain/pretrain_gae.py +143 -0
- pidsmaker/pretrained/pretrain/pretrain_gnn_distill.py +438 -0
- pidsmaker/pretrained/pretrain/pretrain_graphmae.py +151 -0
- pidsmaker/pretrained/pretrain/pretrain_hf.py +169 -0
- pidsmaker/pretrained/pretrain/pretrain_main.py +275 -0
- pidsmaker/pretrained/pretrain/pretrain_mlm.py +491 -0
- pidsmaker/pretrained/pretrain/pretrain_spider.py +502 -0
- pidsmaker/pretrained/training_utils.py +157 -0
- pidsmaker/process_reapr_gt.py +101 -0
- pidsmaker/resources/config/anomal_e.yml +25 -0
- pidsmaker/resources/config/argus.yml +56 -0
- pidsmaker/resources/config/attack_edges/cadets_e3.yml +435 -0
- pidsmaker/resources/config/attack_edges/cadets_e5.yml +407 -0
- pidsmaker/resources/config/attack_edges/theia_e3.yml +382 -0
- pidsmaker/resources/config/attack_edges/theia_e5.yml +387 -0
- pidsmaker/resources/config/attack_edges/trace_e3.yml +415 -0
- pidsmaker/resources/config/attack_edges/trace_e5.yml +417 -0
- pidsmaker/resources/config/default.yml +315 -0
- pidsmaker/resources/config/egraphsage.yml +95 -0
- pidsmaker/resources/config/euler.yml +104 -0
- pidsmaker/resources/config/eval/eval_set_clustering.json +3170 -0
- pidsmaker/resources/config/experiments/tuning/components/tuning_featurization_methods.yml +29 -0
- pidsmaker/resources/config/experiments/tuning/systems/clearscope_e5/tuning_orthrus.yml +14 -0
- pidsmaker/resources/config/experiments/tuning/systems/default/tuning_default_baselines.yml +13 -0
- pidsmaker/resources/config/experiments/tuning/systems/default/tuning_orthrus_fixed.yml +16 -0
- pidsmaker/resources/config/experiments/uncertainty/none.yml +5 -0
- pidsmaker/resources/config/experiments/uncertainty/run_n_times.yml +12 -0
- pidsmaker/resources/config/experiments/uncertainty/run_n_times_hyperparameters.yml +12 -0
- pidsmaker/resources/config/experiments/uncertainty/uncertainty.yml +22 -0
- pidsmaker/resources/config/flash.yml +79 -0
- pidsmaker/resources/config/jbeil.yml +65 -0
- pidsmaker/resources/config/kairos.yml +121 -0
- pidsmaker/resources/config/lares.yml +115 -0
- pidsmaker/resources/config/magic.yml +86 -0
- pidsmaker/resources/config/nodlink.yml +79 -0
- pidsmaker/resources/config/ocrapt.yml +91 -0
- pidsmaker/resources/config/orthrus.yml +126 -0
- pidsmaker/resources/config/orthrus_fixed.yml +17 -0
- pidsmaker/resources/config/pretrained/README.md +101 -0
- pidsmaker/resources/config/pretrained/cybergfm/cybergfm.yml +49 -0
- pidsmaker/resources/config/pretrained/pretrained.yml +190 -0
- pidsmaker/resources/config/pretrained/pretrained_flash.yml +9 -0
- pidsmaker/resources/config/pretrained/pretrained_kairos.yml +12 -0
- pidsmaker/resources/config/pretrained/pretrained_magic.yml +1 -0
- pidsmaker/resources/config/pretrained/pretrained_nodlink.yml +1 -0
- pidsmaker/resources/config/pretrained/pretrained_orthrus.yml +1 -0
- pidsmaker/resources/config/pretrained/pretrained_supervised.yml +13 -0
- pidsmaker/resources/config/pretrained/pretrained_threatrace.yml +8 -0
- pidsmaker/resources/config/pretrained/pretrained_velox.yml +14 -0
- pidsmaker/resources/config/prographer.yml +136 -0
- pidsmaker/resources/config/rcaid.yml +88 -0
- pidsmaker/resources/config/shadewatcher.yml +146 -0
- pidsmaker/resources/config/tests.yml +10 -0
- pidsmaker/resources/config/threatrace.yml +73 -0
- pidsmaker/resources/config/tuned_baselines/optc_net/tuned_argus.yml +4 -0
- pidsmaker/resources/config/tuned_baselines/optc_net/tuned_lares.yml +13 -0
- pidsmaker/resources/config/velox.yml +10 -0
- pidsmaker/resources/config/viz_config.yml +10 -0
- pidsmaker/resources/ground_truth/orthrus/E3-CADETS/node_Nginx_Backdoor_06.csv +8 -0
- pidsmaker/resources/ground_truth/orthrus/E3-CADETS/node_Nginx_Backdoor_11.csv +4 -0
- pidsmaker/resources/ground_truth/orthrus/E3-CADETS/node_Nginx_Backdoor_12.csv +43 -0
- pidsmaker/resources/ground_truth/orthrus/E3-CADETS/node_Nginx_Backdoor_13.csv +24 -0
- pidsmaker/resources/ground_truth/orthrus/E3-CLEARSCOPE/node_clearscope_e3_firefox_0411.csv +41 -0
- pidsmaker/resources/ground_truth/orthrus/E3-CLEARSCOPE/node_clearscope_e3_firefox_0412.csv +0 -0
- pidsmaker/resources/ground_truth/orthrus/E3-FIVEDIRECTIONS/node_fivedirections_e3_browser_0412.csv +1 -0
- pidsmaker/resources/ground_truth/orthrus/E3-FIVEDIRECTIONS/node_fivedirections_e3_excel_0409.csv +63 -0
- pidsmaker/resources/ground_truth/orthrus/E3-FIVEDIRECTIONS/node_fivedirections_e3_firefox_0411.csv +56 -0
- pidsmaker/resources/ground_truth/orthrus/E3-THEIA/node_Browser_Extension_Drakon_Dropper.csv +61 -0
- pidsmaker/resources/ground_truth/orthrus/E3-THEIA/node_Firefox_Backdoor_Drakon_In_Memory.csv +58 -0
- pidsmaker/resources/ground_truth/orthrus/E3-TRACE/node_trace_e3_firefox_0410.csv +11 -0
- pidsmaker/resources/ground_truth/orthrus/E3-TRACE/node_trace_e3_phishing_executable_0413.csv +11 -0
- pidsmaker/resources/ground_truth/orthrus/E3-TRACE/node_trace_e3_pine_0413.csv +14 -0
- pidsmaker/resources/ground_truth/orthrus/E5-CADETS/node_Nginx_Drakon_APT.csv +19 -0
- pidsmaker/resources/ground_truth/orthrus/E5-CADETS/node_Nginx_Drakon_APT_17.csv +107 -0
- pidsmaker/resources/ground_truth/orthrus/E5-CLEARSCOPE/node_clearscope_e5_appstarter_0515.csv +26 -0
- pidsmaker/resources/ground_truth/orthrus/E5-CLEARSCOPE/node_clearscope_e5_firefox_0517.csv +1 -0
- pidsmaker/resources/ground_truth/orthrus/E5-CLEARSCOPE/node_clearscope_e5_lockwatch_0517.csv +17 -0
- pidsmaker/resources/ground_truth/orthrus/E5-CLEARSCOPE/node_clearscope_e5_tester_0517.csv +8 -0
- pidsmaker/resources/ground_truth/orthrus/E5-FIVEDIRECTIONS/node_fivedirections_e5_bits_0515.csv +54 -0
- pidsmaker/resources/ground_truth/orthrus/E5-FIVEDIRECTIONS/node_fivedirections_e5_copykatz_0509.csv +87 -0
- pidsmaker/resources/ground_truth/orthrus/E5-FIVEDIRECTIONS/node_fivedirections_e5_dns_0517.csv +11 -0
- pidsmaker/resources/ground_truth/orthrus/E5-FIVEDIRECTIONS/node_fivedirections_e5_drakon_0517.csv +6 -0
- pidsmaker/resources/ground_truth/orthrus/E5-THEIA/node_THEIA_1_Firefox_Drakon_APT_BinFmt_Elevate_Inject.csv +70 -0
- pidsmaker/resources/ground_truth/orthrus/E5-TRACE/node_Trace_Firefox_Drakon.csv +71 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_m1.csv +1 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_m2.csv +1 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_m3.csv +1 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_m4.csv +5 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_m5.csv +23 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_m6.csv +18 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_s1.csv +1 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_s2.csv +1 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_s3.csv +16 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_edr/atlasv2_edr_s4.csv +22 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_m1.csv +126 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_m2.csv +105 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_m3.csv +94 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_m4.csv +139 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_m5.csv +228 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_m6.csv +131 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_s1.csv +118 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_s2.csv +89 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_s3.csv +133 -0
- pidsmaker/resources/ground_truth/orthrus/atlasv2_h1/node_h1_s4.csv +135 -0
- pidsmaker/resources/ground_truth/orthrus/carbanakv2_edr/carbanakv2_edr.csv +2453 -0
- pidsmaker/resources/ground_truth/orthrus/h051/node_h051_0925.csv +114 -0
- pidsmaker/resources/ground_truth/orthrus/h201/node_h201_0923.csv +2905 -0
- pidsmaker/resources/ground_truth/orthrus/h501/node_h501_0924.csv +749 -0
- pidsmaker/resources/ground_truth/orthrus/readme.md +98 -0
- pidsmaker/resources/ground_truth/reapr/E3-CADETS/node_Nginx_Backdoor_06.csv +3 -0
- pidsmaker/resources/ground_truth/reapr/E3-CADETS/node_Nginx_Backdoor_12.csv +655 -0
- pidsmaker/resources/ground_truth/reapr/E3-CADETS/node_Nginx_Backdoor_13.csv +770 -0
- pidsmaker/resources/ground_truth/reapr/E3-FIVEDIRECTIONS/node_fivedirections_e3_excel_0409.csv +0 -0
- pidsmaker/resources/ground_truth/reapr/E3-FIVEDIRECTIONS/node_fivedirections_e3_firefox_0411.csv +18 -0
- pidsmaker/resources/ground_truth/reapr/E3-THEIA/node_Browser_Extension_Drakon_Dropper.csv +42 -0
- pidsmaker/resources/ground_truth/reapr/E3-THEIA/node_Firefox_Backdoor_Drakon_In_Memory.csv +81 -0
- pidsmaker/resources/ground_truth/reapr/E3-TRACE/node_trace_e3_firefox_0410.csv +37 -0
- pidsmaker/resources/ground_truth/reapr/E3-TRACE/node_trace_e3_phishing_executable_0413.csv +9 -0
- pidsmaker/resources/ground_truth/reapr/E3-TRACE/node_trace_e3_pine_0413.csv +40 -0
- pidsmaker/resources/ground_truth/threatrace/E3-CADETS/ground_truth.txt +12858 -0
- pidsmaker/resources/ground_truth/threatrace/E3-FIVEDIRECTIONS/ground_truth.txt +18420 -0
- pidsmaker/resources/ground_truth/threatrace/E3-THEIA/ground_truth.txt +25363 -0
- pidsmaker/resources/ground_truth/threatrace/E3-TRACE/ground_truth.txt +68265 -0
- pidsmaker/resources/ground_truth/threatrace/README.md +123 -0
- pidsmaker/tasks/__init__.py +10 -0
- pidsmaker/tasks/batching.py +47 -0
- pidsmaker/tasks/construction.py +31 -0
- pidsmaker/tasks/evaluation.py +241 -0
- pidsmaker/tasks/feat_inference.py +413 -0
- pidsmaker/tasks/featurization.py +43 -0
- pidsmaker/tasks/training.py +17 -0
- pidsmaker/tasks/transformation.py +306 -0
- pidsmaker/tasks/triage.py +143 -0
- pidsmaker/tgn.py +635 -0
- pidsmaker/tokenization/__init__.py +112 -0
- pidsmaker/tokenization/base.py +58 -0
- pidsmaker/tokenization/bpe_tokenizer.py +83 -0
- pidsmaker/tokenization/hf_tokenizer.py +61 -0
- pidsmaker/tokenization/hierarchical_tokenizer.py +44 -0
- pidsmaker/tokenization/label_tokenizer.py +24 -0
- pidsmaker/tokenization/nltk_tokenizer.py +84 -0
- pidsmaker/tokenization/substitution.py +295 -0
- pidsmaker/triage/__init__.py +0 -0
- pidsmaker/triage/tracing_methods/__init__.py +1 -0
- pidsmaker/triage/tracing_methods/depimpact.py +213 -0
- pidsmaker/triage/tracing_methods/depimpact_utils.py +537 -0
- pidsmaker/triage/tracing_methods/ocrapt_subgraph.py +267 -0
- pidsmaker/utils/__init__.py +0 -0
- pidsmaker/utils/cache_utils.py +256 -0
- pidsmaker/utils/console.py +282 -0
- pidsmaker/utils/data_utils.py +1742 -0
- pidsmaker/utils/database.py +150 -0
- pidsmaker/utils/dataset_utils.py +413 -0
- pidsmaker/utils/dataset_writer.py +570 -0
- pidsmaker/utils/graph_store.py +634 -0
- pidsmaker/utils/labelling.py +445 -0
- pidsmaker/utils/loss_files.py +193 -0
- pidsmaker/utils/process_tags.py +44 -0
- pidsmaker/utils/utils.py +712 -0
- pidsmaker/vizgen/__init__.py +1 -0
- pidsmaker/vizgen/backfill_manifests.py +100 -0
- pidsmaker/vizgen/dimensionality_reduction.py +249 -0
- pidsmaker/vizgen/embed_exporter.py +467 -0
- pidsmaker/vizgen/exporter.py +493 -0
- pidsmaker/vizgen/html_builder.py +936 -0
- pidsmaker/vizgen/web/__init__.py +1 -0
- pidsmaker/vizgen/web/export.py +62 -0
- pidsmaker/vizgen/web/static/css/app.css +250 -0
- pidsmaker/vizgen/web/static/index.html +182 -0
- pidsmaker/vizgen/web/static/js/app.js +627 -0
- pidsmaker/vizgen/web/static/js/campaign.js +123 -0
- pidsmaker/vizgen/web/static/js/data.js +119 -0
- pidsmaker/vizgen/web/static/js/dialogs.js +240 -0
- pidsmaker/vizgen/web/static/js/jobs.js +75 -0
- pidsmaker/vizgen/web/static/js/scene.js +651 -0
- pidsmaker/vizgen/web/static/js/ui.js +370 -0
- pidsmaker/vizgen/web/static/vendor/OrbitControls.js +1045 -0
- pidsmaker/vizgen/web/static/vendor/d3.v4.min.js +2 -0
- pidsmaker/vizgen/web/static/vendor/three.min.js +6 -0
- pidsmaker/vizgen/web/viz_server.py +2176 -0
- pidsmaker-5.0.0.dist-info/METADATA +597 -0
- pidsmaker-5.0.0.dist-info/RECORD +339 -0
- pidsmaker-5.0.0.dist-info/WHEEL +5 -0
- pidsmaker-5.0.0.dist-info/entry_points.txt +2 -0
- pidsmaker-5.0.0.dist-info/licenses/LICENSE +201 -0
- pidsmaker-5.0.0.dist-info/top_level.txt +1 -0
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from sklearn.feature_extraction import FeatureHasher
|
|
3
|
+
|
|
4
|
+
from pidsmaker.tokenization import load_tokenizer
|
|
5
|
+
from pidsmaker.utils.utils import get_indexid2msg, log_tqdm
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main(cfg):
|
|
9
|
+
indexid2msg = get_indexid2msg(cfg)
|
|
10
|
+
|
|
11
|
+
emb_dim = cfg.featurization.emb_dim
|
|
12
|
+
FH_string = FeatureHasher(n_features=emb_dim, input_type="string")
|
|
13
|
+
|
|
14
|
+
tokenizer = load_tokenizer(cfg)
|
|
15
|
+
indexid2vec = {}
|
|
16
|
+
sorted_indexid2msg = dict(sorted(indexid2msg.items(), key=lambda item: int(item[0])))
|
|
17
|
+
label2vec = {} # the embedding only depends on (node_type, label)
|
|
18
|
+
for indexid, msg in log_tqdm(sorted_indexid2msg.items(), desc="Embedding all nodes in the dataset"):
|
|
19
|
+
node_type, node_label = msg[0], msg[1]
|
|
20
|
+
cached = label2vec.get((node_type, node_label))
|
|
21
|
+
if cached is not None:
|
|
22
|
+
indexid2vec[indexid] = cached
|
|
23
|
+
continue
|
|
24
|
+
tokens = tokenizer.tokenize(node_label, node_type)
|
|
25
|
+
dense_vector = FH_string.fit_transform([tokens]).toarray()
|
|
26
|
+
|
|
27
|
+
normalized_vector = dense_vector / (np.linalg.norm(dense_vector) + 1e-12)
|
|
28
|
+
indexid2vec[indexid] = label2vec[(node_type, node_label)] = normalized_vector.squeeze()
|
|
29
|
+
|
|
30
|
+
return indexid2vec
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import torch
|
|
3
|
+
from gensim.models import Word2Vec
|
|
4
|
+
|
|
5
|
+
from pidsmaker.tokenization import load_tokenizer
|
|
6
|
+
from pidsmaker.utils.utils import (
|
|
7
|
+
get_all_graphs_for_dates,
|
|
8
|
+
get_indexid2msg,
|
|
9
|
+
log_tqdm,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def cal_word_weight(n, percentage):
|
|
14
|
+
d = -1 / n * percentage / 100
|
|
15
|
+
a_1 = 1 / n - 0.5 * (n - 1) * d
|
|
16
|
+
sequence = []
|
|
17
|
+
for i in range(n):
|
|
18
|
+
a_i = a_1 + i * d
|
|
19
|
+
sequence.append(a_i)
|
|
20
|
+
return sequence
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def main(cfg):
|
|
24
|
+
base_dir = cfg.transformation._graphs_dir
|
|
25
|
+
sorted_paths = get_all_graphs_for_dates(
|
|
26
|
+
base_dir, cfg.dataset.train_dates + cfg.dataset.test_dates + cfg.dataset.val_dates
|
|
27
|
+
)
|
|
28
|
+
used_nodes = set()
|
|
29
|
+
for file_path in log_tqdm(sorted_paths, desc="Get nodes in graphs"):
|
|
30
|
+
graph = torch.load(file_path)
|
|
31
|
+
used_nodes = used_nodes | set(graph.nodes())
|
|
32
|
+
used_nodes = list(used_nodes)
|
|
33
|
+
|
|
34
|
+
indexid2msg = get_indexid2msg(cfg)
|
|
35
|
+
|
|
36
|
+
trw_word2vec_model_path = cfg.featurization._model_dir + "trw_word2vec.model"
|
|
37
|
+
model = Word2Vec.load(trw_word2vec_model_path)
|
|
38
|
+
decline_percentage = cfg.featurization.temporal_rw.decline_rate
|
|
39
|
+
|
|
40
|
+
tokenizer = load_tokenizer(cfg)
|
|
41
|
+
indexid2vec = {}
|
|
42
|
+
label2vec = {} # the embedding only depends on (node_type, label)
|
|
43
|
+
for indexid in log_tqdm(used_nodes, desc="Embeding all nodes in the dataset"):
|
|
44
|
+
msg = indexid2msg[indexid]
|
|
45
|
+
node_type, node_label = msg[0], msg[1]
|
|
46
|
+
cached = label2vec.get((node_type, node_label))
|
|
47
|
+
if cached is not None:
|
|
48
|
+
indexid2vec[indexid] = cached
|
|
49
|
+
continue
|
|
50
|
+
tokens = tokenizer.tokenize(node_label, node_type)
|
|
51
|
+
if not tokens:
|
|
52
|
+
indexid2vec[indexid] = label2vec[(node_type, node_label)] = np.zeros(model.vector_size)
|
|
53
|
+
continue
|
|
54
|
+
|
|
55
|
+
weight_list = cal_word_weight(len(tokens), decline_percentage)
|
|
56
|
+
word_vectors = [model.wv[word] for word in tokens]
|
|
57
|
+
weighted_vectors = [
|
|
58
|
+
weight * word_vec for weight, word_vec in zip(weight_list, word_vectors)
|
|
59
|
+
]
|
|
60
|
+
sentence_vector = np.mean(weighted_vectors, axis=0)
|
|
61
|
+
|
|
62
|
+
normalized_vector = sentence_vector / (np.linalg.norm(sentence_vector) + 1e-12)
|
|
63
|
+
indexid2vec[indexid] = label2vec[(node_type, node_label)] = np.array(normalized_vector)
|
|
64
|
+
|
|
65
|
+
return indexid2vec
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def main(cfg):
|
|
8
|
+
|
|
9
|
+
# TODO: this part is broken, need some work to make it work (the training part of this method seems too complicated)
|
|
10
|
+
|
|
11
|
+
trained_w2v_dir = cfg.featurization.alacarte._vec_graphs_dir
|
|
12
|
+
indexid2vec = torch.load(os.path.join(trained_w2v_dir, "indexid2vec"))
|
|
13
|
+
|
|
14
|
+
return indexid2vec
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import zlib
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from gensim.models.doc2vec import Doc2Vec
|
|
6
|
+
|
|
7
|
+
from pidsmaker.featurization.featurization_methods.featurization_doc2vec import doc2vec_label_fn
|
|
8
|
+
from pidsmaker.tokenization import load_tokenizer
|
|
9
|
+
from pidsmaker.utils.utils import get_indexid2msg, log_tqdm
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def infer_node_vector(model, tokens):
|
|
13
|
+
"""Normalized doc2vec vector of a node's tokens, the same whatever the nodes inferred before.
|
|
14
|
+
|
|
15
|
+
`infer_vector` draws its negative samples from the model's shared random state, which is
|
|
16
|
+
seeded here from the tokens. A node whose tokens have no characters (e.g. a process
|
|
17
|
+
without path, whose name is removed by `truncate_process_name`: its label "" gives the
|
|
18
|
+
tokens [""]) gets a zero vector: gensim seeds its initial vector from the joined tokens,
|
|
19
|
+
and starts it from an unseeded random vector when they are empty, different on every run.
|
|
20
|
+
"""
|
|
21
|
+
if not "".join(tokens):
|
|
22
|
+
return np.zeros(model.dv.vector_size, dtype=np.float32)
|
|
23
|
+
model.random.seed(zlib.crc32(" ".join(tokens).encode()))
|
|
24
|
+
vector = model.infer_vector(tokens)
|
|
25
|
+
return vector / np.linalg.norm(vector)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def main(cfg):
|
|
29
|
+
indexid2msg = get_indexid2msg(cfg)
|
|
30
|
+
|
|
31
|
+
doc2vec_model_path = os.path.join(cfg.featurization._model_dir, "doc2vec_model.model")
|
|
32
|
+
model = Doc2Vec.load(doc2vec_model_path)
|
|
33
|
+
|
|
34
|
+
tokenizer = load_tokenizer(cfg)
|
|
35
|
+
label_fn = doc2vec_label_fn(cfg)
|
|
36
|
+
indexid2vec = {}
|
|
37
|
+
# Nodes with the same tokens share their vector (inferred once)
|
|
38
|
+
tokens2vec = {}
|
|
39
|
+
sorted_indexid2msg = dict(sorted(indexid2msg.items(), key=lambda item: int(item[0])))
|
|
40
|
+
for indexid, msg in log_tqdm(sorted_indexid2msg.items(), desc="Embedding all nodes in the dataset"):
|
|
41
|
+
node_type, node_label = msg[0], msg[1]
|
|
42
|
+
if label_fn is not None:
|
|
43
|
+
node_label = label_fn(node_type, node_label)
|
|
44
|
+
tokens = tokenizer.tokenize(node_label, node_type)
|
|
45
|
+
|
|
46
|
+
key = tuple(tokens)
|
|
47
|
+
normalized_vector = tokens2vec.get(key)
|
|
48
|
+
if normalized_vector is None:
|
|
49
|
+
normalized_vector = infer_node_vector(model, tokens)
|
|
50
|
+
tokens2vec[key] = normalized_vector
|
|
51
|
+
indexid2vec[indexid] = normalized_vector
|
|
52
|
+
|
|
53
|
+
return indexid2vec
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from gensim.models import FastText
|
|
5
|
+
|
|
6
|
+
from pidsmaker.tokenization import load_tokenizer
|
|
7
|
+
from pidsmaker.utils.utils import get_indexid2msg, log_tqdm
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main(cfg):
|
|
11
|
+
indexid2msg = get_indexid2msg(cfg)
|
|
12
|
+
|
|
13
|
+
model_path = os.path.join(cfg.featurization._model_dir, "fasttext.pkl")
|
|
14
|
+
model = FastText.load(model_path)
|
|
15
|
+
|
|
16
|
+
tokenizer = load_tokenizer(cfg)
|
|
17
|
+
indexid2vec = {}
|
|
18
|
+
sorted_indexid2msg = dict(sorted(indexid2msg.items(), key=lambda item: int(item[0])))
|
|
19
|
+
label2vec = {} # the embedding only depends on (node_type, label)
|
|
20
|
+
for indexid, msg in log_tqdm(sorted_indexid2msg.items(), desc="Embedding all nodes in the dataset"):
|
|
21
|
+
node_type, node_label = msg[0], msg[1]
|
|
22
|
+
cached = label2vec.get((node_type, node_label))
|
|
23
|
+
if cached is not None:
|
|
24
|
+
indexid2vec[indexid] = cached
|
|
25
|
+
continue
|
|
26
|
+
tokens = tokenizer.tokenize(node_label, node_type)
|
|
27
|
+
if not tokens:
|
|
28
|
+
indexid2vec[indexid] = label2vec[(node_type, node_label)] = np.zeros(model.vector_size)
|
|
29
|
+
continue
|
|
30
|
+
|
|
31
|
+
word_vectors = [model.wv[word] for word in tokens]
|
|
32
|
+
sentence_vector = np.mean(word_vectors, axis=0)
|
|
33
|
+
|
|
34
|
+
normalized_vector = sentence_vector / (np.linalg.norm(sentence_vector) + 1e-12)
|
|
35
|
+
indexid2vec[indexid] = label2vec[(node_type, node_label)] = np.array(normalized_vector)
|
|
36
|
+
|
|
37
|
+
return indexid2vec
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import torch
|
|
6
|
+
from gensim.models import Word2Vec
|
|
7
|
+
|
|
8
|
+
from pidsmaker.featurization.featurization_methods.featurization_flash import get_node2corpus
|
|
9
|
+
from pidsmaker.utils.utils import log_tqdm
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def infer(document, w2vmodel, encoder):
|
|
13
|
+
"""
|
|
14
|
+
Each node is associated to a `document` which is the list of (msg => edge type => msg)
|
|
15
|
+
involving this node.
|
|
16
|
+
We get the embedding of each word inside this document and we do the mean of all embeddings.
|
|
17
|
+
OOV words are simply ignored.
|
|
18
|
+
"""
|
|
19
|
+
# Same values as stacking `w2vmodel.wv[word]` for the words in `w2vmodel.wv` (the tokens are
|
|
20
|
+
# strings, so `word in wv` is a lookup in `key_to_index`), gathered at once
|
|
21
|
+
key_to_index = w2vmodel.wv.key_to_index
|
|
22
|
+
indices = [key_to_index[word] for word in document if word in key_to_index]
|
|
23
|
+
|
|
24
|
+
embedding_dim = w2vmodel.vector_size
|
|
25
|
+
|
|
26
|
+
if not indices:
|
|
27
|
+
return np.zeros(embedding_dim)
|
|
28
|
+
|
|
29
|
+
word_embeddings_array = w2vmodel.wv.vectors[indices]
|
|
30
|
+
|
|
31
|
+
# float32 additions, as with the torch tensors before
|
|
32
|
+
if len(document) < 100000:
|
|
33
|
+
word_embeddings_array = encoder.embed_numpy(word_embeddings_array)
|
|
34
|
+
|
|
35
|
+
return np.mean(word_embeddings_array, axis=0)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class PositionalEncoder:
|
|
39
|
+
def __init__(self, d_model, max_len=100000):
|
|
40
|
+
position = torch.arange(max_len).unsqueeze(1)
|
|
41
|
+
div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
|
|
42
|
+
self.pe = torch.zeros(max_len, d_model)
|
|
43
|
+
self.pe[:, 0::2] = torch.sin(position * div_term)
|
|
44
|
+
self.pe[:, 1::2] = torch.cos(position * div_term)
|
|
45
|
+
|
|
46
|
+
self.pe_numpy = self.pe.numpy()
|
|
47
|
+
|
|
48
|
+
def embed(self, x):
|
|
49
|
+
return x + self.pe[: x.size(0)]
|
|
50
|
+
|
|
51
|
+
def embed_numpy(self, x):
|
|
52
|
+
"""`embed` on a float32 numpy array."""
|
|
53
|
+
return x + self.pe_numpy[: x.shape[0]]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def main(cfg):
|
|
57
|
+
|
|
58
|
+
trained_w2v_dir = cfg.featurization._model_dir
|
|
59
|
+
w2vmodel = Word2Vec.load(os.path.join(trained_w2v_dir, "word2vec_model_final.model"))
|
|
60
|
+
w2v_vector_size = cfg.featurization.emb_dim
|
|
61
|
+
|
|
62
|
+
node2corpus = get_node2corpus(cfg, splits=["train", "val", "test"])
|
|
63
|
+
indexid2vec = {}
|
|
64
|
+
# The encoder is a constant (max_len, d) table: build it once rather than once per node
|
|
65
|
+
encoder = PositionalEncoder(w2v_vector_size)
|
|
66
|
+
for indexid, corpus in log_tqdm(node2corpus.items(), desc="Embeding all nodes in the dataset"):
|
|
67
|
+
indexid2vec[indexid] = infer(corpus, w2vmodel, encoder)
|
|
68
|
+
|
|
69
|
+
return indexid2vec
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Adds the graph2vec embedding and the rooted subgraphs (RSGs) of each snapshot to its graph.
|
|
2
|
+
|
|
3
|
+
graph2vec embeds whole graphs, not nodes: nodes only get their type as features, and each time
|
|
4
|
+
window graph written by `feat_inference` gets two graph-level attributes:
|
|
5
|
+
|
|
6
|
+
- `graph_emb` (1, d): the graph2vec embedding of the snapshot, inferred with the model trained
|
|
7
|
+
in the featurization task (on the train split with `featurization.training_split=train`).
|
|
8
|
+
All snapshots are inferred in the same way, including those of the training split, so that
|
|
9
|
+
the embeddings of all splits follow the same distribution.
|
|
10
|
+
- `snapshot_rsg` (1, n, D + 2) int32: for each node of the snapshot (sorted by node ID), its ID
|
|
11
|
+
followed by the vocabulary index of its RSG at each depth 0..D (-1 for RSGs unseen when
|
|
12
|
+
training graph2vec). Used to rank the RSGs of anomalous snapshots (key indicators).
|
|
13
|
+
|
|
14
|
+
Both have a first dimension of size 1, so that they are kept whole when the edges of a graph are
|
|
15
|
+
selected (`CollatableTemporalData.index_select` slices the tensors whose first dimension is the
|
|
16
|
+
number of edges).
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import multiprocessing
|
|
20
|
+
import os
|
|
21
|
+
from concurrent.futures import ProcessPoolExecutor
|
|
22
|
+
|
|
23
|
+
import numpy as np
|
|
24
|
+
import torch
|
|
25
|
+
from gensim.models.doc2vec import Doc2Vec
|
|
26
|
+
|
|
27
|
+
from pidsmaker.featurization.featurization_methods.featurization_graph2vec import (
|
|
28
|
+
MODEL_FILE,
|
|
29
|
+
infer_snapshot_embedding,
|
|
30
|
+
snapshot_rsgs,
|
|
31
|
+
snapshot_seed,
|
|
32
|
+
snapshot_words,
|
|
33
|
+
)
|
|
34
|
+
from pidsmaker.utils.utils import log, log_tqdm
|
|
35
|
+
|
|
36
|
+
# Set in the parent process before forking the workers, to share the model without copies
|
|
37
|
+
_MODEL = None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _embed_snapshot(args):
|
|
41
|
+
"""Worker: adds `graph_emb` and `snapshot_rsg` to the feat_inference output of a graph."""
|
|
42
|
+
path, out_path, depth, epochs = args
|
|
43
|
+
node_ids, labels = snapshot_rsgs(path, depth)
|
|
44
|
+
vector = infer_snapshot_embedding(
|
|
45
|
+
_MODEL, snapshot_words(labels), epochs=epochs, seed=snapshot_seed(path)
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
key_to_index = _MODEL.wv.key_to_index
|
|
49
|
+
vocab_ids = np.vectorize(lambda w: key_to_index.get(w, -1), otypes=[np.int64])(labels)
|
|
50
|
+
table = np.concatenate([node_ids[:, None], vocab_ids.T], axis=1).astype(np.int32)
|
|
51
|
+
|
|
52
|
+
data = torch.load(out_path)
|
|
53
|
+
data.graph_emb = torch.from_numpy(vector).float().view(1, -1)
|
|
54
|
+
data.snapshot_rsg = torch.from_numpy(table).unsqueeze(0)
|
|
55
|
+
torch.save(data, out_path)
|
|
56
|
+
return float((vocab_ids < 0).mean())
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def main(cfg, sorted_paths, out_dir):
|
|
60
|
+
"""Embeds the snapshots of `sorted_paths`, whose graphs were written to `out_dir`."""
|
|
61
|
+
global _MODEL
|
|
62
|
+
_MODEL = Doc2Vec.load(os.path.join(cfg.featurization._model_dir, MODEL_FILE))
|
|
63
|
+
|
|
64
|
+
g2v_cfg = cfg.featurization.graph2vec
|
|
65
|
+
tasks = []
|
|
66
|
+
for path in sorted_paths:
|
|
67
|
+
out_path = os.path.join(out_dir, f"{os.path.basename(path)}.TemporalData.simple")
|
|
68
|
+
if os.path.exists(out_path): # empty graphs have no output
|
|
69
|
+
tasks.append((path, out_path, g2v_cfg.wl_iterations, g2v_cfg.infer_epochs))
|
|
70
|
+
|
|
71
|
+
workers = max(1, g2v_cfg.num_workers)
|
|
72
|
+
# Forked workers share the model loaded in this process
|
|
73
|
+
fork = multiprocessing.get_context("fork")
|
|
74
|
+
with ProcessPoolExecutor(max_workers=workers, mp_context=fork) as pool:
|
|
75
|
+
oov = list(
|
|
76
|
+
log_tqdm(
|
|
77
|
+
pool.map(_embed_snapshot, tasks, chunksize=1),
|
|
78
|
+
desc="Inferring graph2vec snapshot embeddings",
|
|
79
|
+
total=len(tasks),
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
if oov:
|
|
83
|
+
log(f"Mean ratio of RSGs unseen during graph2vec training: {np.mean(oov):.3f}")
|
|
84
|
+
_MODEL = None
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""OCR-APT's per-node behavioral features
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
from pidsmaker.utils.dataset_utils import get_num_edge_type, get_rel2id
|
|
9
|
+
from pidsmaker.utils.utils import get_split_to_files, log, log_tqdm
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _minmax(col):
|
|
13
|
+
lo, hi = col.min(), col.max()
|
|
14
|
+
if hi <= lo:
|
|
15
|
+
return np.zeros_like(col)
|
|
16
|
+
return (col - lo) / (hi - lo)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def main(cfg):
|
|
20
|
+
edge_type_dim = get_num_edge_type(cfg)
|
|
21
|
+
rel2id = get_rel2id(cfg, from_zero=True)
|
|
22
|
+
oc = cfg.featurization.ocrapt_features
|
|
23
|
+
|
|
24
|
+
in_counts, out_counts, times = {}, {}, {}
|
|
25
|
+
|
|
26
|
+
base_dir = cfg.transformation._graphs_dir
|
|
27
|
+
split_to_files = get_split_to_files(cfg, base_dir)
|
|
28
|
+
all_paths = [p for paths in split_to_files.values() for p in paths]
|
|
29
|
+
|
|
30
|
+
for path in log_tqdm(all_paths, desc="Computing OCR-APT behavioral features"):
|
|
31
|
+
g = torch.load(path, weights_only=False)
|
|
32
|
+
for u, v, attr in g.edges(data=True):
|
|
33
|
+
label = attr.get("label")
|
|
34
|
+
if label not in rel2id:
|
|
35
|
+
continue
|
|
36
|
+
idx = rel2id[label]
|
|
37
|
+
t = attr.get("time")
|
|
38
|
+
|
|
39
|
+
u, v = int(u), int(v)
|
|
40
|
+
for n in (u, v):
|
|
41
|
+
if n not in in_counts:
|
|
42
|
+
in_counts[n] = np.zeros(edge_type_dim, dtype=np.float64)
|
|
43
|
+
out_counts[n] = np.zeros(edge_type_dim, dtype=np.float64)
|
|
44
|
+
times[n] = []
|
|
45
|
+
out_counts[u][idx] += 1
|
|
46
|
+
in_counts[v][idx] += 1
|
|
47
|
+
if t is not None:
|
|
48
|
+
times[u].append(t)
|
|
49
|
+
times[v].append(t)
|
|
50
|
+
|
|
51
|
+
nodes = sorted(in_counts.keys())
|
|
52
|
+
n_nodes = len(nodes)
|
|
53
|
+
|
|
54
|
+
hist = np.zeros((n_nodes, 2 * edge_type_dim), dtype=np.float64)
|
|
55
|
+
idle = np.zeros((n_nodes, 3), dtype=np.float64)
|
|
56
|
+
lifespan = np.zeros(n_nodes, dtype=np.float64) if oc.use_lifespan else None
|
|
57
|
+
cum_active = np.zeros(n_nodes, dtype=np.float64) if oc.use_cumulative_active_time else None
|
|
58
|
+
|
|
59
|
+
for i, n in enumerate(nodes):
|
|
60
|
+
hist[i, :edge_type_dim] = in_counts[n]
|
|
61
|
+
hist[i, edge_type_dim:] = out_counts[n]
|
|
62
|
+
ts = sorted(times[n])
|
|
63
|
+
if len(ts) >= 2:
|
|
64
|
+
gaps = np.diff(np.asarray(ts, dtype=np.float64))
|
|
65
|
+
idle[i] = [gaps.min(), gaps.max(), gaps.mean()]
|
|
66
|
+
if lifespan is not None:
|
|
67
|
+
lifespan[i] = ts[-1] - ts[0]
|
|
68
|
+
if cum_active is not None:
|
|
69
|
+
# total time between consecutive actions with gaps under one second
|
|
70
|
+
# 1st-percentile-scale are treated as sub-second bursts
|
|
71
|
+
thr = np.quantile(gaps, 0.01) if len(gaps) else 0.0
|
|
72
|
+
cum_active[i] = float(gaps[gaps <= max(thr, 0.0)].sum())
|
|
73
|
+
|
|
74
|
+
# L2-normalize each node's histogram
|
|
75
|
+
norms = np.linalg.norm(hist, axis=1, keepdims=True)
|
|
76
|
+
hist = hist / np.where(norms > 0, norms, 1.0)
|
|
77
|
+
|
|
78
|
+
# min-max normalize idle stats; column-wise
|
|
79
|
+
for c in range(idle.shape[1]):
|
|
80
|
+
idle[:, c] = _minmax(idle[:, c])
|
|
81
|
+
if lifespan is not None:
|
|
82
|
+
lifespan = _minmax(lifespan)
|
|
83
|
+
if cum_active is not None:
|
|
84
|
+
cum_active = _minmax(cum_active)
|
|
85
|
+
|
|
86
|
+
extra_cols = [idle]
|
|
87
|
+
if lifespan is not None:
|
|
88
|
+
extra_cols.append(lifespan.reshape(-1, 1))
|
|
89
|
+
if cum_active is not None:
|
|
90
|
+
extra_cols.append(cum_active.reshape(-1, 1))
|
|
91
|
+
features = np.concatenate([hist] + extra_cols, axis=1)
|
|
92
|
+
|
|
93
|
+
indexid2vec = {str(n): features[i] for i, n in enumerate(nodes)}
|
|
94
|
+
log(f"OCR-APT behavioral features: {len(indexid2vec)} nodes, dim={features.shape[1]}")
|
|
95
|
+
return indexid2vec
|