-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
326 lines (290 loc) · 10.7 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import argparse
from pathlib import Path
import torch
from loguru import logger
from kyn.dataset import KYNDataset
from kyn.trainer import KYNTrainer
from kyn.networks import (
GraphConvInstanceGlobalMaxSmall,
GraphConvInstanceGlobalMaxSmallSoftMaxAggr,
GraphConvInstanceGlobalMaxSmallSoftMaxAggrEdge,
GraphConvGraphNormGlobalMaxSmallSoftMaxAggrEdge,
GraphConvLayerNormGlobalMaxSmallSoftMaxAggrEdge,
)
from kyn.config import KYNConfig
from kyn.eval import KYNEvaluator, KYNVulnEvaluator
def get_model(model_name: str, config: KYNConfig) -> torch.nn.Module:
"""Get the appropriate model based on the model name."""
models = {
"GraphConvInstanceGlobalMaxSmall": GraphConvInstanceGlobalMaxSmall,
"GraphConvInstanceGlobalMaxSmallSoftMaxAggr": GraphConvInstanceGlobalMaxSmallSoftMaxAggr,
"GraphConvInstanceGlobalMaxSmallSoftMaxAggrEdge": GraphConvInstanceGlobalMaxSmallSoftMaxAggrEdge,
"GraphConvGraphNormGlobalMaxSmallSoftMaxAggrEdge": GraphConvGraphNormGlobalMaxSmallSoftMaxAggrEdge,
"GraphConvLayerNormGlobalMaxSmallSoftMaxAggrEdge": GraphConvLayerNormGlobalMaxSmallSoftMaxAggrEdge,
}
if model_name not in models:
raise ValueError(
f"Unknown model: {model_name}. Available models: {list(models.keys())}"
)
return models[model_name](
config.model_channels, config.feature_dim, config.dropout_ratio
)
def generate_dataset(args):
"""Generate and save a dataset."""
dataset = KYNDataset(
root_data_path=args.root_data_path,
dataset_naming_convetion=args.dataset_type,
filter_strs=args.filter_strs,
sample_size=args.sample_size,
exclude=args.exclude_filter,
with_edge_features=args.with_edge_features,
)
dataset.load_and_transform_graphs()
dataset.save_dataset(args.output_prefix)
logger.info(f"Dataset saved with prefix: {args.output_prefix}")
def train_model(args):
"""Train a model using the specified configuration."""
config = KYNConfig(
learning_rate=args.learning_rate,
model_channels=args.model_channels,
feature_dim=args.feature_dim,
epochs=args.epochs,
batch_size=args.batch_size,
train_data=args.train_data,
train_labels=args.train_labels,
model_arch=args.model_name,
)
if (
args.model_name == "GraphConvInstanceGlobalMaxSmall"
or args.model_name == "GraphConvInstanceGlobalMaxSmallSoftMaxAggr"
):
config.with_edges = False
model = get_model(args.model_name, config)
trainer = KYNTrainer(
model=model,
config=config,
device=args.device,
log_to_wandb=args.use_wandb,
wandb_project=args.wandb_project,
)
trainer.train(validate_examples=args.validate_examples)
trainer.save_model()
logger.info(f"Model saved with UUID: {config.exp_uuid}")
def evaluate_model(args):
"""Evaluate a trained model."""
model = get_model(
args.model_name,
KYNConfig(model_channels=args.model_channels, feature_dim=args.feature_dim),
)
model.load_state_dict(torch.load(args.model_path))
evaluator = KYNEvaluator(
model=model,
model_name=args.model_name,
dataset_path=args.dataset_path,
eval_prefix=args.experiment_prefix,
search_pool_size=args.search_pool_sizes,
num_search_pools=args.num_search_pools,
random_seed=args.random_seed,
requires_edge_feats=args.requires_edge_feats,
)
evaluator.evaluate()
def evaluate_vuln_model(args):
"""Evaluate a trained model on vulnerability detection."""
model = get_model(
args.model_name,
KYNConfig(model_channels=args.model_channels, feature_dim=args.feature_dim),
)
model.load_state_dict(torch.load(args.model_path))
evaluator = KYNVulnEvaluator(
model=model,
model_name=args.model_name,
target_data_path=args.target_data_path,
search_data_paths=args.search_data_paths,
vulnerable_functions=args.vulnerable_functions,
device=args.device,
target_arch=args.target_arch,
no_metadata=args.no_metadata,
save_metrics_to_file=not args.no_save_metrics,
)
results = evaluator.evaluate()
# Print summary of results
for result in results:
logger.info(f"\nResults for {Path(result['search_data']).name}:")
logger.info(f"Mean Rank: {result['mean_rank']}")
logger.info(f"Median Rank: {result['median_rank']}")
logger.info(f"Mean Similarity: {result['mean_similarity']}")
def main():
parser = argparse.ArgumentParser(
description="KYN - Dataset Generation, Training, and Evaluation CLI"
)
parser.add_argument(
"--verbose", "-v", action="store_true", help="Enable verbose logging"
)
subparsers = parser.add_subparsers(dest="command", help="Command to execute")
# Dataset generation parser
dataset_parser = subparsers.add_parser("generate", help="Generate a dataset")
dataset_parser.add_argument(
"--root-data-path", required=True, help="Root path containing JSON graph files"
)
dataset_parser.add_argument(
"--dataset-type",
required=True,
choices=["cisco", "binkit", "trex", "binarycorp"],
help="Dataset naming convention",
)
dataset_parser.add_argument(
"--filter-strs", nargs="*", default=[], help="Strings to filter dataset files"
)
dataset_parser.add_argument(
"--sample-size",
type=int,
default=-1,
help="Number of samples to include (-1 for all)",
)
dataset_parser.add_argument(
"--exclude-filter",
action="store_true",
help="Exclude rather than include filter matches",
)
dataset_parser.add_argument(
"--with-edge-features", action="store_true", help="Include edge features"
)
dataset_parser.add_argument(
"--output-prefix", required=True, help="Output file prefix"
)
# Training parser
train_parser = subparsers.add_parser("train", help="Train a model")
train_parser.add_argument(
"--model-name", required=True, help="Name of the model architecture to use"
)
train_parser.add_argument(
"--train-data", required=True, help="Path to training data pickle file"
)
train_parser.add_argument(
"--train-labels", required=True, help="Path to training labels pickle file"
)
train_parser.add_argument(
"--learning-rate", type=float, default=5e-4, help="Learning rate"
)
train_parser.add_argument(
"--model-channels", type=int, default=256, help="Number of model channels"
)
train_parser.add_argument(
"--feature-dim", type=int, default=6, help="Feature dimension"
)
train_parser.add_argument(
"--epochs", type=int, default=350, help="Number of epochs"
)
train_parser.add_argument("--batch-size", type=int, default=256, help="Batch size")
train_parser.add_argument(
"--device", default="cuda", help="Device to use (cuda, cpu, mps)"
)
train_parser.add_argument(
"--use-wandb", action="store_true", help="Log to Weights & Biases"
)
train_parser.add_argument("--wandb-project", help="Weights & Biases project name")
train_parser.add_argument(
"--validate-examples",
action="store_true",
help="Validate examples during training",
)
# Evaluation parser
eval_parser = subparsers.add_parser(
"evaluate", help="Evaluate a trained model on generic search"
)
eval_parser.add_argument(
"--model-path", required=True, help="Path to the trained model file"
)
eval_parser.add_argument(
"--model-name", required=True, help="Name of the model architecture"
)
eval_parser.add_argument(
"--model-channels", type=int, default=256, help="Number of model channels"
)
eval_parser.add_argument(
"--feature-dim", type=int, default=6, help="Feature dimension"
)
eval_parser.add_argument(
"--dataset-path", required=True, help="Path to evaluation dataset"
)
eval_parser.add_argument(
"--eval-prefix", required=True, help="Prefix for eval results"
)
eval_parser.add_argument(
"--search-pool-sizes",
type=int,
nargs="+",
default=[100],
help="Search pool sizes",
)
eval_parser.add_argument(
"--num-search-pools", type=int, default=500, help="Number of search pools"
)
eval_parser.add_argument(
"--random-seed", type=int, default=1337, help="Random seed"
)
eval_parser.add_argument(
"--requires-edge-feats",
action="store_true",
help="Model requires edge features",
)
# Vulnerability Evaluation parser
vuln_eval_parser = subparsers.add_parser(
"vuln-evaluate", help="Evaluate a trained model on vulnerability detection"
)
# Model parameters
vuln_eval_parser.add_argument(
"--model-path", required=True, help="Path to the trained model file"
)
vuln_eval_parser.add_argument(
"--model-name", required=True, help="Name of the model architecture"
)
vuln_eval_parser.add_argument(
"--model-channels", type=int, default=256, help="Number of model channels"
)
vuln_eval_parser.add_argument(
"--feature-dim", type=int, default=6, help="Feature dimension"
)
vuln_eval_parser.add_argument(
"--device", default="cuda", help="Device to use (cuda, cpu, mps)"
)
# Data paths
vuln_eval_parser.add_argument(
"--target-data-path", required=True, help="Path to the target firmware data"
)
vuln_eval_parser.add_argument(
"--search-data-paths", required=True, nargs="+", help="Paths to search datasets"
)
# Vulnerability configuration
vuln_eval_parser.add_argument(
"--target-arch", required=True, help="Target architecture (e.g., mips32, arm32)"
)
vuln_eval_parser.add_argument(
"--vulnerable-functions",
required=True,
nargs="+",
help="List of vulnerable function names to search for",
)
# Additional options
vuln_eval_parser.add_argument(
"--no-metadata",
action="store_true",
help="Skip metadata processing in graph loading",
)
vuln_eval_parser.add_argument(
"--no-save-metrics", action="store_true", help="Don't save metrics to files"
)
args = parser.parse_args()
logger.level("INFO")
if args.command == "generate":
generate_dataset(args)
elif args.command == "train":
train_model(args)
elif args.command == "evaluate":
evaluate_model(args)
elif args.command == "vuln-evaluate":
evaluate_vuln_model(args)
else:
parser.print_help()
if __name__ == "__main__":
main()