|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import sqlite3 |
| 4 | +import zlib |
| 5 | + |
| 6 | +import rapidjson |
| 7 | +import xxhash |
| 8 | + |
| 9 | + |
| 10 | +# TODO: reqs: |
| 11 | +# pip install xxhash |
| 12 | + |
| 13 | +DETECTION_TYPES = {} |
| 14 | +TAG_NAMES = {} |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +def initialize_schema(connection): |
| 19 | + cur = connection.cursor() |
| 20 | + |
| 21 | + cur.execute(""" |
| 22 | + CREATE TABLE scans ( |
| 23 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 24 | + package_name VARCHAR(256) UNIQUE, |
| 25 | + metadata JSON |
| 26 | + ) |
| 27 | + """) |
| 28 | + |
| 29 | + cur.execute(""" |
| 30 | + CREATE TABLE tag_names (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) UNIQUE) |
| 31 | + """) |
| 32 | + |
| 33 | + cur.execute(""" |
| 34 | + CREATE TABLE detection_types (id INTEGER PRIMARY KEY AUTOINCREMENT, name text) |
| 35 | + """) |
| 36 | + |
| 37 | + cur.execute(""" |
| 38 | + CREATE TABLE detections |
| 39 | + ( |
| 40 | + id INTEGER PRIMARY KEY AUTOINCREMENT , |
| 41 | + scan INTEGER NOT NULL , |
| 42 | + type INTEGER NOT NULL, |
| 43 | + signature TEXT UNIQUE, |
| 44 | + message TEXT NOT NULL, |
| 45 | + score INTEGER DEFAULT 0, |
| 46 | + extra BLOB, |
| 47 | + FOREIGN KEY (type) REFERENCES detection_types (id), |
| 48 | + FOREIGN KEY (scan) REFERENCES scans (id) |
| 49 | + ) |
| 50 | + """) |
| 51 | + |
| 52 | + cur.execute(""" |
| 53 | + CREATE TABLE tags ( |
| 54 | + detection INTEGER NOT NULL, |
| 55 | + tag INTEGER NOT NULL, |
| 56 | + FOREIGN KEY (detection) REFERENCES detections (id), |
| 57 | + FOREIGN KEY (tag) REFERENCES tag_names (id), |
| 58 | + PRIMARY KEY (detection, tag) |
| 59 | + ) WITHOUT ROWID |
| 60 | + """) |
| 61 | + |
| 62 | + connection.commit() |
| 63 | + |
| 64 | + |
| 65 | +def create_or_open(name: str): |
| 66 | + exists = os.path.exists(name) |
| 67 | + conn = sqlite3.connect(name) |
| 68 | + if not exists: |
| 69 | + initialize_schema(conn) |
| 70 | + else: |
| 71 | + load_existing(conn.cursor()) |
| 72 | + |
| 73 | + return conn |
| 74 | + |
| 75 | + |
| 76 | +def load_existing(cursor): |
| 77 | + cursor.execute("SELECT id, name FROM tag_names") |
| 78 | + |
| 79 | + for (id, name) in cursor.fetchall(): |
| 80 | + TAG_NAMES[name] = id |
| 81 | + |
| 82 | + cursor.execute("SELECT id, name FROM detection_types") |
| 83 | + |
| 84 | + for (id, name) in cursor.fetchall(): |
| 85 | + DETECTION_TYPES[name] = id |
| 86 | + |
| 87 | + |
| 88 | +def get_tag_id(name, cursor) -> int: |
| 89 | + if name not in TAG_NAMES: |
| 90 | + cursor.execute("INSERT INTO tag_names (name) VALUES (?)", (name,)) |
| 91 | + TAG_NAMES[name] = cursor.lastrowid |
| 92 | + |
| 93 | + return TAG_NAMES[name] |
| 94 | + |
| 95 | + |
| 96 | +def get_detection_type_id(name, cursor) -> int: |
| 97 | + if name not in DETECTION_TYPES: |
| 98 | + cursor.execute("INSERT INTO detection_types (name) VALUES (?)", (name,)) |
| 99 | + DETECTION_TYPES[name] = cursor.lastrowid |
| 100 | + |
| 101 | + return DETECTION_TYPES[name] |
| 102 | + |
| 103 | + |
| 104 | +def add_detection(scan_id, detection, package, cursor): |
| 105 | + # TODO: add severity to table cols |
| 106 | + type_id = get_detection_type_id(detection["type"], cursor) |
| 107 | + signature = xxhash.xxh64(f"{package}#{detection['signature']}").hexdigest() |
| 108 | + |
| 109 | + extra = zlib.compress(rapidjson.dumps(detection.get("extra", {})).encode()) |
| 110 | + |
| 111 | + try: |
| 112 | + cursor.execute(""" |
| 113 | + INSERT INTO detections (scan, type, signature, message, score, extra) VALUES (?, ?, ?, ?, ?, ?) |
| 114 | + """, (scan_id, type_id, signature, detection["message"], detection.get("score", 0), extra)) |
| 115 | + except sqlite3.IntegrityError: |
| 116 | + print(f"Warning detection already exists: {detection}") |
| 117 | + return |
| 118 | + |
| 119 | + detection_id = cursor.lastrowid |
| 120 | + |
| 121 | + for tag in detection.get("tags", []): |
| 122 | + tag_id = get_tag_id(tag, cursor) |
| 123 | + try: |
| 124 | + cursor.execute(""" |
| 125 | + INSERT INTO tags (detection, tag) VALUES (?, ?) |
| 126 | + """, (detection_id, tag_id)) |
| 127 | + except sqlite3.IntegrityError: |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +def add_scan(scan: dict, cursor): |
| 132 | + try: |
| 133 | + pkg_name = scan["metadata"]["uri_input"]["package"] |
| 134 | + print(f"Processing package: `{pkg_name}`") |
| 135 | + |
| 136 | + cursor.execute(""" |
| 137 | + INSERT INTO scans (package_name, metadata) VALUES (?, ?) |
| 138 | + """, (pkg_name, rapidjson.dumps(scan["metadata"]))) |
| 139 | + except sqlite3.IntegrityError: |
| 140 | + # Data for this package already exists: |
| 141 | + return |
| 142 | + |
| 143 | + scan_id = cursor.lastrowid |
| 144 | + for d in scan.get("detections", []): |
| 145 | + add_detection(scan_id, d, pkg_name, cursor) |
| 146 | + |
| 147 | + |
| 148 | +def process_scans(dataset_path, cursor): |
| 149 | + with open(dataset_path, "r") as fd: |
| 150 | + for line in fd: |
| 151 | + if line := line.strip(): |
| 152 | + add_scan(rapidjson.loads(line), cursor) |
| 153 | + |
| 154 | + |
| 155 | +def main(name: str, dataset_path): |
| 156 | + c = create_or_open(name) |
| 157 | + cur = c.cursor() |
| 158 | + |
| 159 | + process_scans(dataset_path, cur) |
| 160 | + |
| 161 | + c.commit() |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main(sys.argv[1], sys.argv[2]) |
0 commit comments