-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_processor.py
More file actions
209 lines (179 loc) · 7.27 KB
/
result_processor.py
File metadata and controls
209 lines (179 loc) · 7.27 KB
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
import re
import csv
import json
import io
from datetime import datetime
from typing import List, Optional, Tuple
from entropy import score_token_entropy
def extract_matches(snippet: str, pattern: str) -> list:
"""Extract all matches of the pattern in the snippet."""
try:
regex = re.compile(pattern)
matches = regex.findall(snippet)
# Flatten tuples from regex groups
flat_matches = []
for m in matches:
if isinstance(m, tuple):
# Take the first non-empty group
for group in m:
if group:
flat_matches.append(group)
break
else:
flat_matches.append(m)
return flat_matches
except re.error:
return []
def process_results(results: list, pattern: str, enable_entropy: bool = True) -> list:
"""Process search results and extract matches with optional entropy scoring."""
processed = []
total_matches = 0
total_unique_matches = 0
all_unique_tokens = set()
for item in results:
text_matches = item.get("text_matches", [])
collected = []
fragments = []
for tm in text_matches:
fragment = tm.get("fragment", "")
fragments.append(fragment)
matches = extract_matches(fragment, pattern)
if matches:
total_matches += len(matches)
collected.extend(matches)
if collected:
# Deduplicate tokens for this file
unique_tokens = list(set(collected))
total_unique_matches += len(unique_tokens)
all_unique_tokens.update(unique_tokens)
# Score each token with entropy if enabled
token_details = []
for token in unique_tokens:
detail = {"value": token}
if enable_entropy:
detail["entropy"] = score_token_entropy(token)
token_details.append(detail)
# Try to get the most relevant date from multiple possible fields
last_modified = None
try:
if item.get("repository", {}).get("pushed_at"):
last_modified = item["repository"]["pushed_at"]
elif item.get("repository", {}).get("updated_at"):
last_modified = item["repository"]["updated_at"]
elif item.get("repository", {}).get("created_at"):
last_modified = item["repository"]["created_at"]
if last_modified:
last_modified = datetime.strptime(
last_modified,
"%Y-%m-%dT%H:%M:%SZ"
).strftime("%Y-%m-%d %H:%M:%S UTC")
except (ValueError, KeyError):
pass
result_info = {
"repository": item.get("repository", {}).get("full_name"),
"file_path": item.get("path"),
"html_url": item.get("html_url"),
"last_modified": last_modified or "N/A",
"found_tokens": unique_tokens,
"token_details": token_details,
"total_matches_in_file": len(collected),
"unique_matches_in_file": len(unique_tokens),
"found_date": datetime.now().strftime("%Y-%m-%d"),
"fragments": fragments,
}
processed.append(result_info)
# Add match statistics to the first result if we have any results
if processed:
processed[0]["match_statistics"] = {
"total_files_with_matches": len(processed),
"total_matches_found": total_matches,
"total_unique_matches_in_files": total_unique_matches,
"total_unique_tokens_overall": len(all_unique_tokens),
}
return processed
def sanitize_filename(filename: str) -> str:
"""Replace characters that are invalid in Windows/Unix filenames."""
invalid_chars = [':', '/', '\\', '?', '*', '"', '<', '>', '|']
for char in invalid_chars:
filename = filename.replace(char, '-')
return filename
def save_results(results: list, pattern: str) -> Tuple[Optional[str], Optional[str], Optional[str]]:
"""Save results to JSON files and return file paths and error status."""
# Use safe timestamp format (no colons — compatible with Windows)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
formatted_date = datetime.now().strftime("%Y-%m-%d")
match_stats = results[0].get("match_statistics", {}) if results else {}
all_tokens = []
for result in results:
all_tokens.extend(result["found_tokens"])
all_tokens = list(set(all_tokens))
tokens_file = f"tokens_results_{timestamp}.json"
detailed_file = f"detailed_results_{timestamp}.json"
error_occurred = False
error_message = ""
try:
with open(tokens_file, "w", encoding='utf-8') as f:
json.dump({
"tokens": all_tokens,
"total_unique_tokens": len(all_tokens),
"pattern": pattern,
"scan_date": formatted_date,
"scan_time": datetime.now().strftime("%H:%M:%S"),
"match_statistics": match_stats,
}, f, indent=4)
except (IOError, OSError) as e:
error_occurred = True
error_message += f"Error saving tokens file ({tokens_file}): {str(e)}\n"
try:
with open(detailed_file, "w", encoding='utf-8') as f:
json.dump({
"pattern": pattern,
"scan_date": formatted_date,
"scan_time": datetime.now().strftime("%H:%M:%S"),
"match_statistics": match_stats,
"results": results,
}, f, indent=4)
except (IOError, OSError) as e:
error_occurred = True
error_message += f"Error saving detailed results file ({detailed_file}): {str(e)}"
if error_occurred:
return None, None, error_message
else:
return tokens_file, detailed_file, None
def save_results_csv(results: list) -> Tuple[Optional[str], Optional[str]]:
"""Save results to CSV format. Returns (csv_string, error_message)."""
if not results:
return None, "No results to export"
output = io.StringIO()
writer = csv.writer(output)
# Header row
writer.writerow([
"Repository",
"File Path",
"URL",
"Last Modified",
"Token",
"Entropy",
"Entropy Risk",
"Found Date",
])
for result in results:
for i, token in enumerate(result.get("found_tokens", [])):
# Get entropy info if available
token_details = result.get("token_details", [])
entropy_info = {}
if i < len(token_details):
entropy_info = token_details[i].get("entropy", {})
writer.writerow([
result.get("repository", "N/A"),
result.get("file_path", "N/A"),
result.get("html_url", "N/A"),
result.get("last_modified", "N/A"),
token,
entropy_info.get("entropy", "N/A"),
entropy_info.get("risk", "N/A"),
result.get("found_date", "N/A"),
])
csv_string = output.getvalue()
output.close()
return csv_string, None