-
Notifications
You must be signed in to change notification settings - Fork 9
/
finder.py
144 lines (117 loc) · 3.76 KB
/
finder.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
import argparse
import json
import logging
import os
from datetime import datetime
from requests import head, get, ConnectionError, Timeout
API_URL = "https://www.loldrivers.io/api/drivers.json"
FILE_NAME = "drivers.json"
HEADERS_FILE = "headers.json"
TERMINATE_FUNCTIONS = ["ZwTerminateProcess", "NtTerminateProcess"]
OPEN_FUNCTIONS = ["ZwOpenProcess", "NtOpenProcess"]
TIMEOUT = 5
def load_json(file_path):
"""
Load JSON data from a file.
"""
try:
with open(file_path, "r", encoding="utf-8") as file:
return json.load(file)
except FileNotFoundError:
return -1
except json.JSONDecodeError as e:
logging.error(f"Failed to decode JSON data from '{file_path}': {e}")
return -2
def save_json(data, file_path):
"""
Save data as JSON to a file.
"""
try:
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4)
logging.info(f"Data saved to '{file_path}'.")
return True
except Exception as e:
logging.error(f"Failed to save data to '{file_path}': {e}")
return False
def check_data_changed(api_url):
"""
Check if the API data has changed since the last retrieval.
If the data file is not present or the content has changed, download the file.
"""
saved_headers = load_json(HEADERS_FILE)
if saved_headers == -1:
logging.info(f"Headers file not found. Redownloading.")
try:
response = head(api_url, timeout=TIMEOUT)
except (ConnectionError, Timeout) as exception:
logging.error(f"Connection error. No internet connection?")
logging.info(f"Using local version of drivers.json")
return False
current_headers = {
"ETag": response.headers.get("ETag"),
"Last-Modified": response.headers.get("Last-Modified"),
}
if current_headers == saved_headers:
logging.info("Data has not changed.")
return False
try:
response = get(api_url)
response.raise_for_status()
data = response.json()
save_json(data, FILE_NAME)
save_json(current_headers, HEADERS_FILE)
return True
except Exception as e:
logging.error(f"Failed to download data from '{api_url}': {e}")
return False
def process_data(drivers_data, desired_keys=['filename', 'md5']):
functions_list = [TERMINATE_FUNCTIONS, OPEN_FUNCTIONS]
processed_data = {}
for driver in drivers_data:
for sample in driver.get("KnownVulnerableSamples", []):
imported_functions = set(sample.get("ImportedFunctions", []))
if all(
any(func in imported_functions for func in func_list)
for func_list in functions_list
):
driver_id = driver.get("Id")
if driver_id not in processed_data:
processed_data[driver_id] = {dk : [] for dk in desired_keys}
for key in sample:
lowered_key = key.lower()
if lowered_key in desired_keys:
processed_data[driver_id][lowered_key].append(sample.get(key))
print(json.dumps(processed_data, indent=4))
return processed_data
def main(api_url):
"""
Main function to retrieve and process data from LOLDrivers API.
"""
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
check_data_changed(api_url)
try:
drivers_data = load_json(FILE_NAME)
processed_data = process_data(drivers_data)
if processed_data:
output_file = os.path.splitext(FILE_NAME)[0] + "_processed.json"
if save_json(processed_data, output_file):
logging.info(f"Processed data saved to '{output_file}'.")
else:
logging.error("Failed to save processed data.")
except json.JSONDecodeError as e:
logging.error(f"Failed to decode JSON data from '{FILE_NAME}': {e}")
if __name__ == "__main__":
# Configure argparse
parser = argparse.ArgumentParser(
description="Retrieve and process data from LOLDrivers API"
)
parser.add_argument(
"--api-url",
type=str,
default=API_URL,
help="URL of the API to retrieve data from",
)
args = parser.parse_args()
# Run the main function
main(args.api_url)