-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucag.py
More file actions
276 lines (209 loc) · 10.1 KB
/
Copy pathucag.py
File metadata and controls
276 lines (209 loc) · 10.1 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
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
r"""
Universal CSV to Axiom Python Script Generator
This script automatically generates Axiom-compatible Python artifact scripts
from CSV files. It reads the CSV headers and creates corresponding .py files
following the template pattern.
Usage:
python csv_to_axiom_generator.py <input_csv_folder> <output_py_folder>
Example:
python csv_to_axiom_generator.py "./csvs" "./artifacts_py"
This is a universal python script that points to a folder containing CSV files that you want to use to create a Custom Python Artifact, so that you can put the CSV results into Axiom.
It also does a 'best effort' in determine the DataType, so any columns that are Time/Date or GPS, it will try and apply those data types into the script, so that they are properly represented in Axiom.
Once custom artifact python scripts have been created, put these in the 'plugins' folder in following locations:
AXIOM GUI - C:\Program Files\Magnet Forensics\Magnet AXIOM\AXIOM Process\plugins
AUTOMATE Node - C:\Program Files\Magnet Forensics\Magnet AUTOMATE\agent\AXIOM Process\plugins
Typical use case:
- running a 3rd party tool/script over a phone extraction to get CSV outputs, make custom artifacts, then run CSV files (with newly created custom artifacts) in Axiom to get proper results. Additionally, run the extraction in Axiom 'nomrally' to get other Artifacts, you will have an Axiom case with CSV results + normal results (more result completeness that is all searchable, filterable, etc) - and put all into Portable Case or Review
- obtain CSV results from another source (like drone data), automatically create custom artifacts and run in Axiom
"""
import csv
import os
import sys
from pathlib import Path
import re
# Ensure console output uses UTF-8 encoding. When the script is
# executed through a PowerShell wrapper the default code page may be
# a legacy ANSI encoding that cannot represent characters like ✓ or ✗.
# Attempt to reconfigure the text streams early so that subsequent
# print() calls never trigger UnicodeEncodeError.
try:
# Python 3.7+ provides TextIOBase.reconfigure()
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
except AttributeError:
# Fallback for older versions: wrap the buffer manually.
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
def sanitize_class_name(name):
"""Convert CSV name to valid Python class name"""
# Replace special characters and spaces with underscores
sanitized = re.sub(r'[^a-zA-Z0-9_]', '_', name)
# Remove leading underscores and numbers
sanitized = re.sub(r'^[0-9_]+', '', sanitized)
# Remove consecutive underscores
sanitized = re.sub(r'_+', '_', sanitized)
# Remove trailing underscores
sanitized = sanitized.rstrip('_')
return sanitized if sanitized else "CSVArtifact"
def get_fragment_type(column_name):
"""Determine appropriate fragment type based on column name"""
column_lower = column_name.lower()
if 'time' in column_lower or 'date' in column_lower or 'timestamp' in column_lower:
return "Category.DateTime", "FragmentType.DateTime"
elif 'latitude' in column_lower:
return "Category.Latitude", "FragmentType.Float"
elif 'longitude' in column_lower:
return "Category.Longitude", "FragmentType.Float"
elif 'lat' in column_lower or 'lon' in column_lower:
# latitude/longitude shorthand - avoid referencing a non-existent
# Category.Location. Use the SDK's 'None' category at runtime
# instead (referenced via getattr) and keep the fragment type
# as float.
return "getattr(Category, 'None')", "FragmentType.Float"
else:
# Default string-type category. We cannot simply return
# "Category.None" because "None" is a Python keyword and
# writing `Category.None` produces a syntax error when the
# generated artifact script is parsed. The Axiom SDK does have a
# Category member named "None", so we reference it indirectly
# at runtime using getattr(). This expression is still valid
# Python source and evaluates to the proper value when imported.
return "getattr(Category, 'None')", "FragmentType.String"
def read_csv_headers(csv_path):
"""Read the first row of CSV to get column headers"""
try:
with open(csv_path, 'r', encoding='utf-8-sig') as f:
reader = csv.reader(f)
headers = next(reader)
return headers
except Exception as e:
print(f"Error reading {csv_path}: {e}")
return None
def generate_fragments(headers):
"""Generate CreateFragments method content"""
fragments = []
for col in headers:
category, frag_type = get_fragment_type(col)
fragments.append(f' self.AddFragment("{col}", {category}, {frag_type})')
return '\n'.join(fragments)
def generate_value_assignments(headers):
"""Generate the try/except blocks for each column value"""
assignments = []
for i, col in enumerate(headers):
try_block = f''' try:
foundHit.AddValue("{col}", row[{i}])
except:
foundHit.AddValue("{col}", 'Error: {{}}. {{}}. script line: {{}}'.format(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2].tb_lineno))'''
assignments.append(try_block)
return '\n'.join(assignments)
def generate_header_validation(headers):
"""Generate the header validation check.
IronPython’s compiler has difficulty reducing long ``or`` chains; by
comparing the entire ``row`` list to a literal list of expected
headers we avoid constructing any ``OrExpression`` nodes.
"""
literal = '[' + ', '.join(f'"{col}"' for col in headers) + ']'
return f'if row != {literal}:'
def generate_py_content(csv_name, headers):
"""Generate complete Python artifact file content using generic class names"""
# we intentionally avoid embedding the CSV filename in the class names
fragments = generate_fragments(headers)
value_assignments = generate_value_assignments(headers)
header_validation = generate_header_validation(headers)
content = f'''from axiom import *
import csv
import datetime
import sys
import codecs
import time
import io
class CSVReader(Artifact):
def __init__(self):
self.AddHunter(ReadCSV())
def GetName(self):
return "{csv_name}"
def CreateFragments(self):
{fragments}
class ReadCSV(Hunter):
def Register(self, registrar):
registrar.RegisterFileName("{csv_name}.csv")
def Hunt(self, context):
temp_file_path = context.Searchable.SaveAsTempFile()
skip_bom = False
with io.open(temp_file_path, mode="rb") as csv_file:
bom = csv_file.read(3)
if bom == codecs.BOM_UTF8:
skip_bom = True
with codecs.open(temp_file_path,"rb","utf-8") as csv_file:
if skip_bom:
csv_file.seek(len(codecs.BOM_UTF8))
csv_reader = csv.reader(csv_file, delimiter=",")
for row in csv_reader:
if csv_reader.line_num == 1:
{header_validation}
break
else:
continue
foundHit = Hit()
foundHit.SetLocation("Line Number: " + str(csv_reader.line_num))
{value_assignments}
self.PublishHit(foundHit)
RegisterArtifact(CSVReader())
'''
return content
def process_csvs(input_dir, output_dir):
"""Process all CSV files in input directory"""
input_path = Path(input_dir)
output_path = Path(output_dir)
# Create output directory if it doesn't exist
output_path.mkdir(parents=True, exist_ok=True)
# remove any existing .py files to avoid stale/legacy artifacts
for old in output_path.glob('*.py'):
try:
old.unlink()
except Exception:
pass
if not input_path.exists():
print(f"Error: Input directory '{input_dir}' does not exist")
return False
csv_files = list(input_path.glob('*.csv'))
if not csv_files:
print(f"No CSV files found in '{input_dir}'")
return False
print(f"Found {len(csv_files)} CSV file(s)")
success_count = 0
for csv_file in sorted(csv_files):
csv_name = csv_file.stem
print(f"\nProcessing: {csv_file.name}")
headers = read_csv_headers(csv_file)
if not headers:
print(f" ✗ Failed to read headers")
continue
print(f" Columns: {len(headers)}")
try:
py_content = generate_py_content(csv_name, headers)
filename_safe = sanitize_class_name(csv_name)
output_file = output_path / f"{filename_safe}.py"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(py_content)
print(f" ✓ Generated: {output_file.name}")
success_count += 1
except Exception as e:
print(f" ✗ Error generating file: {e}")
print(f"\n{'='*60}")
print(f"Complete: {success_count}/{len(csv_files)} files generated successfully")
print(f"Output directory: {output_path.absolute()}")
return success_count > 0
def main():
if len(sys.argv) != 3:
print("Usage: python csv_to_axiom_generator.py <input_csv_folder> <output_py_folder>")
print("\nExample:")
print(" python csv_to_axiom_generator.py './csv_data' './artifacts_py'")
sys.exit(1)
input_dir = sys.argv[1]
output_dir = sys.argv[2]
success = process_csvs(input_dir, output_dir)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()