-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_maker.py
68 lines (55 loc) · 2.5 KB
/
db_maker.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
import sqlite3
import csv
def remove_null_bytes(input_path, output_path):
with open(input_path, "rb") as infile, open(output_path, "wb") as outfile:
for line in infile:
outfile.write(line.replace(b'\0', b''))
def export_sorted_malware_list_csv(db_path, output_file):
# Write to the output file
i = 0
with open(output_file, "w", encoding="utf-8") as file:
with open(db_path, encoding="latin1") as csvfile: # Use latin1 to avoid decode errors
reader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
for row in reader:
if row and row != None and row != "" and "md5_hash" in row.keys() and row["md5_hash"] != None:
hash_value = row["md5_hash"].replace('"', '')
name = "MalwareBazaar[found by " + row["reporter"].replace('"', '').strip() +"]-" + row["file_name"].replace('"', '').strip() + "-" + row["file_type_guess"].replace('"', '').strip()
if hash_value[0] == ' ':
hash_value = hash_value[1:]
name = name.replace(":", "-")
file.write(f"{hash_value}: {name}\n")
i += 1
print(f"Export completed. Rows written to {output_file} total {i}")
def export_sorted_malware_list(db_path, output_file): # sql
try:
# Connect to the SQLite database
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Query the malwareList table
cursor.execute("SELECT hash, name FROM malwareList ORDER BY hash ASC")
rows = cursor.fetchall()
# Write to the output file
i=0
with open(output_file, "w") as file:
for row in rows:
# if i>=20:
# break
hash_value, name = row
if hash_value[0] == ' ':
hash_value = hash_value[1:]
name = name.replace(":", "-")
file.write(f"{hash_value}: {name}\n")
i+=1
print(f"Export completed. Rows written to {output_file} total {i}")
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
# Ensure the connection is closed
if conn:
conn.close()
if __name__ == "__main__":
# Define the database path and output file
db_path = "full.csv" # Replace with your database path
output_file = "malwareBazaar.txt"
export_sorted_malware_list_csv(db_path, output_file)
# remove_null_bytes(d)