-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrypto_detect.py
415 lines (349 loc) · 15 KB
/
crypto_detect.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# SPDX-FileCopyrightText: 2024 2024 SCAN Open Source Software SL (scanoss.com)
# SPDX-FileContributor: [Author Name(s)] <[Optional: Email Address(es)]>
#
# SPDX-License-Identifier: MIT
"""
SPDX-License-Identifier: MIT
Copyright (c) 2024, SCANOSS
"""
import os
import yaml
import json
import sys
import argparse
import binaryornot.check
from collections import defaultdict
from progress.spinner import Spinner
from progress.bar import Bar
__version__ = '0.0.2'
debug = False
quiet = False
trace = False
all_hidden = False # Process hidden files/folders also
def print_stderr(*args, **kwargs):
"""
Print the given message to STDERR
"""
print(*args, file=sys.stderr, **kwargs)
def print_msg(*args, **kwargs):
"""
Print message if quite mode is not enabled
"""
if not quiet:
print_stderr(*args, **kwargs)
def print_debug(*args, **kwargs):
"""
Print debug message if enabled
"""
if debug:
print_stderr(*args, **kwargs)
def print_trace(*args, **kwargs):
"""
Print trace message if enabled
"""
if trace:
print_stderr(*args, **kwargs)
def is_binary(path: str):
"""
Check if the specified file is a potential "binary" file
:param path: Path to the file to check
:return: True if binary, False otherwise
"""
if path:
binary_path = binaryornot.check.is_binary(path)
if binary_path:
print_trace(f'Detected binary file: {path}')
return binary_path
return False
def filter_dirs(dirs: list) -> list:
"""
Filter which folders should be considered for processing
:param dirs: list of directories to filter
:return: list of filtered directories
"""
dir_list = []
for d in dirs:
ignore = False
if d.startswith(".") and not all_hidden: # Ignore all . folders unless requested
ignore = True
if not ignore:
dir_list.append(d)
return dir_list
def filter_files(files: list) -> list:
"""
Filter which files should be considered for processing
:param files: list of files to filter
:return list of filtered files
"""
file_list = []
for f in files:
ignore = False
if f.startswith(".") and not all_hidden: # Ignore all . files unless requested
ignore = True
if not ignore:
file_list.append(f)
return file_list
def load_algorithm_metadata(definitions_dir: str) -> dict:
"""
Load algorithm metadata from the algorithms subfolder
:param definitions_dir: root definition folder
:return: dictionary of algorithm metadata indexed by algorithmId
"""
algorithms_dir = os.path.join(definitions_dir, 'algorithms')
if not os.path.exists(algorithms_dir):
print_stderr(f'Error: Algorithms directory does not exist: {algorithms_dir}')
return {}
algorithm_metadata = {}
# Walk through category directories
for category_dir in os.listdir(algorithms_dir):
category_path = os.path.join(algorithms_dir, category_dir)
if os.path.isdir(category_path):
# Process all YAML files in the category
for file in os.listdir(category_path):
if file.endswith('.yaml') or file.endswith('.yml'):
file_path = os.path.join(category_path, file)
print_trace(f'Loading algorithm metadata: {file_path}')
try:
with open(file_path, 'r') as yaml_file:
data = yaml.safe_load(yaml_file)
if 'algorithmId' in data:
algorithm_metadata[data['algorithmId']] = {
'algorithm': data.get('algorithm', ''),
'category': data.get('category', category_dir),
'strength': data.get('strength', '')
}
except Exception as e:
print_stderr(f'Error loading algorithm metadata file {file_path}: {e}')
return algorithm_metadata
def load_definitions(definitions_dir: str) -> tuple:
"""
Load all the YAML definition files from the specified root folder with new structure
:param definitions_dir: root definition folder
:return: tuple of (keyword definitions dictionary, algorithm metadata dictionary)
"""
if not definitions_dir or not os.path.exists(definitions_dir):
print_stderr(f'Error: No definition folder specified, or it does not exist: {definitions_dir}')
return None, None
# Load algorithm metadata first
algorithm_metadata = load_algorithm_metadata(definitions_dir)
print_debug(f'Loaded {len(algorithm_metadata)} algorithm metadata entries')
# Now load the keyword definitions
definitions_subdir = os.path.join(definitions_dir, 'definitions')
if not os.path.exists(definitions_subdir):
print_stderr(f'Error: Definitions subfolder does not exist: {definitions_subdir}')
return None, None
keyword_definitions = defaultdict(list)
algorithm_map = {} # Maps yaml files to algorithm IDs
# Walk through category directories in the definitions subfolder
for category_dir in os.listdir(definitions_subdir):
category_path = os.path.join(definitions_subdir, category_dir)
if os.path.isdir(category_path):
# Process all YAML files in the category
for file in os.listdir(category_path):
if file.endswith('.yaml') or file.endswith('.yml'):
file_path = os.path.join(category_path, file)
print_trace(f'Loading definition: {file_path}')
try:
with open(file_path, 'r') as yaml_file:
data = yaml.safe_load(yaml_file)
if 'algorithmId' in data and 'keywords' in data:
algorithm_id = data['algorithmId']
# Map this file to its algorithm ID
algorithm_map[file] = algorithm_id
# Add all keywords for this algorithm
for keyword in data['keywords']:
keyword_definitions[str(keyword)].append({
'file': file,
'algorithmId': algorithm_id
})
except Exception as e:
print_stderr(f'Error loading definition file {file_path}: {e}')
return keyword_definitions, algorithm_metadata, algorithm_map
def analyse_file(file_path: str, definitions: dict, algorithm_metadata: dict) -> dict:
"""
Analyse the given file for the specified definitions
:param file_path: File to analyse
:param definitions: keyword definitions to search for
:param algorithm_metadata: metadata for algorithms
:return: Dictionary of matching keywords
"""
if not definitions:
print_stderr(f'Error: No definitions specified.')
return None
if not os.path.exists(file_path):
print_stderr(f'Error: File does not exist: {file_path}')
return None
if is_binary(file_path):
return {} # Skip binary files
detections = []
with open(file_path, 'rb') as file:
print_debug(f'Analysing {file_path}...')
contents = file.read()
content_str = contents.decode('utf-8', 'ignore')
if len(content_str) > 0:
for keyword, matches in definitions.items():
keyword_str = str(keyword)
if keyword_str in content_str:
match_info = []
for match in matches:
algorithm_id = match.get('algorithmId')
match_data = {
'def_file': match.get('file'),
'algorithmId': algorithm_id
}
# Add metadata if available
if algorithm_id in algorithm_metadata:
match_data.update({
'algorithm': algorithm_metadata[algorithm_id].get('algorithm', ''),
'category': algorithm_metadata[algorithm_id].get('category', ''),
'strength': algorithm_metadata[algorithm_id].get('strength', '')
})
match_info.append(match_data)
detections.append({
'keyword': keyword_str,
'matches': match_info
})
return detections
def analyse_directory(target_dir: str, definitions: dict, algorithm_metadata: dict) -> list:
"""
Analyse the given folder for the specified definitions
:param target_dir: Target folder to scan
:param definitions: Keyword definitions to search for
:param algorithm_metadata: Metadata for algorithms
:return: Detection dictionary
"""
if not target_dir or not os.path.exists(target_dir):
print_stderr(f'Error: No target folder specified, or it does not exist: {target_dir}')
return None
if not definitions:
print_stderr(f'No definitions specified to search for.')
return None
progress_enabled = True if not quiet and not debug and not trace and sys.stderr.isatty() else False
spinner = None
if progress_enabled:
spinner = Spinner('Detecting files ')
file_list = []
# Walk the directory tree looking for file to analyse
for root, dirs, files in os.walk(target_dir):
dirs[:] = filter_dirs(dirs) # Strip out unwanted directories
filtered_files = filter_files(files) # Strip out unwanted files
for file in filtered_files:
file_path = os.path.join(root, file)
f_size = 0
try:
f_size = os.stat(file_path).st_size
except Exception as e:
print_trace(
f'Ignoring missing symlink file: {file_path} ({e})') # Can fail if there is a broken symlink
if f_size > 0: # skip empty files
if spinner:
spinner.next()
file_list.append(file_path)
if spinner:
spinner.finish()
all_detections = []
# Analyse the given list of files
if file_list:
bar = None
if progress_enabled:
bar = Bar('Analysing', max=len(file_list))
bar.next(0)
for file_path in file_list:
if bar:
bar.next(1)
detections = analyse_file(file_path, definitions, algorithm_metadata)
if detections:
all_detections.append({'file': file_path, 'crypto': detections})
if bar:
bar.finish()
return all_detections
def run_scan(target_dir: str, def_dir: str, output: str) -> bool:
"""
Run a scan of the specified target directory using the specified keyword definitions
:param target_dir: Target directory to scan
:param def_dir: Definition folder to load from
:param output: Output file to write results to
:return: True on success, False otherwise
"""
if not target_dir:
print_stderr(f'Error: No target folder/directory specified.')
return False
if not os.path.exists(target_dir):
print_stderr(f'Error: Target folder does not exist: {target_dir}.')
return False
if not def_dir:
print_stderr(f'Error: No definition folder/directory specified.')
return False
if not os.path.exists(def_dir):
print_stderr(f'Error: Definitions folder does not exist: {def_dir}.')
return False
if output:
open(output, 'w').close()
# Load the crypto definitions and algorithm metadata
definitions, algorithm_metadata, algorithm_map = load_definitions(def_dir)
if not definitions:
print_stderr(f'Error: Failed to load definitions for: {def_dir}')
return False
print_debug(f'Loaded {len(definitions)} keyword definitions')
print_debug(f'Loaded {len(algorithm_metadata)} algorithm metadata entries')
detections = analyse_directory(target_dir, definitions, algorithm_metadata)
if detections:
print_msg(f'Writing output to: {output}')
results = {
'files': detections,
'metadata': {
'total_files': len(detections)
}
}
with open(output, 'w') as json_file:
json.dump(results, json_file, indent=2)
else:
print_msg(f'No cryptography found in {target_dir}')
return True
def setup_args():
"""
Setup command line arguments
:return: arguments object
"""
global debug, quiet, trace, all_hidden
parser = argparse.ArgumentParser(description=f'SCANOSS Keyword Analyser. Ver: {__version__}, License: MIT')
parser.add_argument('--version', '-v', action='store_true', help='Display version details')
parser.add_argument('target_dir', metavar='TARGET-DIR', type=str, nargs='?', help='Folder to scan')
parser.add_argument('--definitions', '-c', type=str, default='definitions_crypto_algorithms',
help='The directory containing cryptography definitions (default: definitions_crypto_algorithms/)')
parser.add_argument('--output', '-o', type=str, default='crypto-detect.json',
help='The output JSON file (default: crypto-detect.json).')
parser.add_argument('--all-hidden', action='store_true', help='Scan all hidden files/folders')
parser.add_argument('--debug', '-d', action='store_true', help='Enable debug messages')
parser.add_argument('--quiet', '-q', action='store_true', help='Enable quiet mode')
parser.add_argument('--trace', '-t', action='store_true', help='Enable trace mode')
args = parser.parse_args()
if args.version:
ver()
exit(0)
if not args.target_dir:
print_stderr('Error: Need to specify target directory to process.')
parser.print_help()
exit(1)
debug = args.debug
quiet = args.quiet
trace = args.trace
all_hidden = args.all_hidden
return args
def ver():
"""
Print version information
"""
print(f'Version: {__version__}')
def main():
"""
Run the keyword analyser to detect cryptographic algorithms
"""
args = setup_args()
if not args:
exit(1)
if not run_scan(args.target_dir, args.definitions, args.output):
exit(1)
exit(0)
if __name__ == "__main__":
main()