File size: 7,845 Bytes
4737a47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Stacking classifier inference for SBAN multi-representation malware dataset classification.

Loads sban_weighted_stacking_model.joblib and predicts which source dataset
(bodmas, dike, malwarebazaar, sorel20m) each sample belongs to.
"""

from __future__ import annotations

import argparse
import math
import re
from collections import Counter
from pathlib import Path

import joblib
import numpy as np
import pandas as pd
from scipy.sparse import csr_matrix, hstack
from sklearn.metrics import (
    accuracy_score,
    classification_report,
    f1_score,
)

PROJECT_DIR = Path(__file__).resolve().parent
DEFAULT_MODEL_PATH = PROJECT_DIR / "sban_weighted_stacking_model.joblib"

ID_COLUMN = "ID"
LABEL_COLUMN = "dataset_name"

REPRESENTATION_COLUMNS = {
    "asm": "assembly_code",
    "binary": "binary_code",
    "source": "source_code",
    "nld": "NLD",
}

NUMERIC_FEATURE_NAMES = [
    "char_count",
    "line_count",
    "token_count",
    "avg_line_length",
    "unique_token_ratio",
    "char_entropy",
]


def binary_to_byte_tokens(text: str) -> str:
    tokens: list[str] = []

    for line in text.splitlines():
        line = re.sub(r"\s+", "", line)

        if not line or re.fullmatch(r"[0-9a-fA-F]+", line) is None:
            continue

        byte_count = len(line) // 2
        tokens.extend(line[i * 2 : (i + 1) * 2].lower() for i in range(byte_count))
        tokens.append("instsep")

    return " ".join(tokens)


def calculate_char_entropy(text: str) -> float:
    if not text:
        return 0.0

    counts = Counter(text)
    length = len(text)

    return -sum(
        (count / length) * math.log2(count / length) for count in counts.values()
    )


def extract_numeric_features(text: str) -> list[float]:
    lines = text.splitlines()
    tokens = text.split()

    char_count = len(text)
    line_count = len(lines)
    token_count = len(tokens)

    avg_line_length = (
        sum(len(line) for line in lines) / line_count if line_count else 0.0
    )

    unique_token_ratio = (
        len(set(tokens)) / token_count if token_count else 0.0
    )

    return [
        float(char_count),
        float(line_count),
        float(token_count),
        float(avg_line_length),
        float(unique_token_ratio),
        calculate_char_entropy(text),
    ]


def build_numeric_features(series: pd.Series) -> np.ndarray:
    return np.asarray(
        [extract_numeric_features(text) for text in series.astype(str)],
        dtype=np.float32,
    )


def build_inference_features(
    representation_name: str,
    text_series: pd.Series,
    representation_bundle: dict,
) -> tuple[csr_matrix, np.ndarray]:
    texts = text_series.fillna("").astype(str).tolist()

    if representation_name == "binary":
        tfidf_texts = [binary_to_byte_tokens(text) for text in texts]
    else:
        tfidf_texts = texts

    vectorizer = representation_bundle["vectorizer"]
    numeric_scaler = representation_bundle["numeric_scaler"]

    x_tfidf = vectorizer.transform(tfidf_texts)
    x_numeric = build_numeric_features(text_series)
    x_numeric_scaled = numeric_scaler.transform(x_numeric)

    x_full = hstack(
        [x_tfidf, csr_matrix(x_numeric_scaled)],
        format="csr",
        dtype=np.float32,
    )

    return x_full, np.asarray(x_numeric_scaled, dtype=np.float32)


class StackingPredictor:
    """Wrapper around the exported joblib inference bundle."""

    def __init__(self, model_path: Path | str = DEFAULT_MODEL_PATH) -> None:
        self.model_path = Path(model_path)
        self.model_bundle = joblib.load(self.model_path)
        self.representation_order = self.model_bundle["representation_order"]
        self.representation_columns = self.model_bundle["representation_columns"]
        self.label_encoder = self.model_bundle["label_encoder"]
        self.meta_model = self.model_bundle["meta_model"]
        self.representation_bundles = self.model_bundle["representations"]

    @property
    def classes(self) -> list[str]:
        return self.label_encoder.classes_.tolist()

    def predict(self, df: pd.DataFrame) -> pd.DataFrame:
        missing = [
            col
            for rep in self.representation_order
            if (col := self.representation_columns[rep]) not in df.columns
        ]
        if missing:
            raise ValueError(f"Missing required columns: {missing}")

        meta_features: list[np.ndarray] = []

        for representation_name in self.representation_order:
            bundle = self.representation_bundles[representation_name]

            x_full, x_numeric = build_inference_features(
                representation_name,
                df[self.representation_columns[representation_name]],
                bundle,
            )

            x_selected = x_full[:, bundle["selected_indices"]]
            decision_scores = bundle["base_model"].decision_function(x_selected)

            meta_features.append(decision_scores)
            meta_features.append(x_numeric)

        x_meta = np.hstack(meta_features)

        encoded_predictions = self.meta_model.predict(x_meta)
        prediction_probabilities = self.meta_model.predict_proba(x_meta)

        result = df.copy()
        result["prediction"] = self.label_encoder.inverse_transform(
            encoded_predictions
        )

        for index, class_name in enumerate(self.label_encoder.classes_):
            result[f"prob_{class_name}"] = prediction_probabilities[:, index]

        return result


def load_predictor(model_path: Path | str = DEFAULT_MODEL_PATH) -> StackingPredictor:
    return StackingPredictor(model_path)


def evaluate_predictions(
    predictions: pd.DataFrame,
    label_column: str = LABEL_COLUMN,
) -> None:
    if label_column not in predictions.columns:
        print(f"Skip evaluation: column {label_column!r} not in input.")
        return

    y_true = predictions[label_column]
    y_pred = predictions["prediction"]

    print(f"Accuracy    : {accuracy_score(y_true, y_pred):.6f}")
    print(f"Macro F1    : {f1_score(y_true, y_pred, average='macro'):.6f}")
    print(f"Weighted F1 : {f1_score(y_true, y_pred, average='weighted'):.6f}")
    print("\nClassification Report\n")
    print(
        classification_report(
            y_true,
            y_pred,
            digits=4,
            target_names=sorted(y_true.unique()),
        )
    )


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Run SBAN weighted stacking inference on a parquet file.",
    )
    parser.add_argument(
        "--model-path",
        type=Path,
        default=DEFAULT_MODEL_PATH,
        help="Path to sban_weighted_stacking_model.joblib",
    )
    parser.add_argument(
        "--input",
        type=Path,
        required=True,
        help="Input parquet with representation columns",
    )
    parser.add_argument(
        "--output",
        type=Path,
        required=True,
        help="Output parquet with prediction and prob_* columns",
    )
    parser.add_argument(
        "--evaluate",
        action="store_true",
        help="Print metrics when dataset_name column is present",
    )
    return parser.parse_args()


def main() -> None:
    args = parse_args()

    if not args.model_path.is_file():
        raise FileNotFoundError(f"Model not found: {args.model_path}")
    if not args.input.is_file():
        raise FileNotFoundError(f"Input not found: {args.input}")

    predictor = load_predictor(args.model_path)
    df = pd.read_parquet(args.input)
    predictions = predictor.predict(df)

    args.output.parent.mkdir(parents=True, exist_ok=True)
    predictions.to_parquet(args.output, index=False)

    print(f"Wrote {len(predictions)} rows to {args.output}")

    if args.evaluate:
        evaluate_predictions(predictions)


if __name__ == "__main__":
    main()