-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_pe.py
89 lines (74 loc) · 2.55 KB
/
module_pe.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
#!/usr/bin/env python
# Main function is handler_<object>
# Return should be structured as JSON, { <module> : { result : <result>, risk : <risk>, indicators : <[indicators]>, additional_info : { <custom> : <custom> } } }
from __future__ import print_function
from subprocess import Popen, PIPE
import re, sys
# Custom imports
import filescan_config
# Set variables from config for later use
filescanner_logs_dir = filescan_config.scan_logs_directory
filescanner_proc_dir = filescan_config.files_out_directory
peframe_exe = filescan_config.peframe_exe
# Output JSON
module = 'pe'
output = {}
##
# SUPPORT FUNCTIONS
##
def read_file(file_to_read):
f = open(file_to_read, mode='r')
lines = f.readlines()
f.close()
return lines
##
# MAIN
##
def handler_pe(file, md5, tool, args, environment):
risk = 'Unknown Risk'
result = 'See detailed txt log'
# List to hold summary of findings from pe analysis
indicators = []
scan_exe_cmd = peframe_exe+' "'+filescanner_proc_dir+file+'" >> "'+filescanner_logs_dir+file+'.txt"'
scan_exe_cmd_results = Popen(scan_exe_cmd, shell=True, stdout=PIPE).communicate()[0]
# Read exe results for printing to console
data = read_file(filescanner_logs_dir+file+'.txt')
# Print specific indicators to console
for line in data:
# Append discovered items to indicator list
# Example: Anti Debug discovered [4]
if re.search(r"discovered", line):
indicator = line.replace(' discovered',':').rstrip()
indicator = indicator.replace('[','')
indicator = indicator.replace(']','')
indicators.append(indicator)
# Print indicator metadata to console
elif re.search(r"LegalCopyright", line):
print(" "+line.replace('\n',''))
sys.stdout.flush()
elif re.search(r"InternalName", line):
print(" "+line.replace('\n',''))
sys.stdout.flush()
elif re.search(r"CompanyName", line):
print(" "+line.replace('\n',''))
sys.stdout.flush()
elif re.search(r"FileDescription", line):
print(" "+line.replace('\n',''))
sys.stdout.flush()
elif re.search(r"OriginalFileName", line):
print(" "+line.replace('\n',''))
sys.stdout.flush()
elif re.search(r"(Packer)\W{2,}(?!.*Yes)", line):
print(" "+line.replace('\n',''))
sys.stdout.flush()
elif re.search(r"Url\W\W", line):
print(" "+line.replace('\n',''))
sys.stdout.flush()
##
# OUTPUT
##
output[module] = {'result' : result, 'risk' : risk, 'indicators' : indicators }
output[module]['additional_info'] = {'md5' : md5}
return output
#if __name__ == '__main__':
#scan_exe()