-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe_analysis.py
79 lines (61 loc) · 2.05 KB
/
pe_analysis.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
''' Windows Portable Executable Format Analysis
Malware Data Science Notes
Description: Uses pefile to examine the PE Format.
usage example from command line: python3 pe_analysis.py ircbot.exe
(make sure script is in same folder to analyze)
pip3 install pefile
Author: N.Sepe
License: MIT License
'''
import pefile
import sys
import os
class LoopArgs:
def __init__(self, *args, **kwargs):
number_args = []
self.args = number_args.append((args,kwargs))
self.program_name = sys.argv[0]
def analyze_sections(self, file):
print("Analyzing Windows Portable Executable Format sections...")
file = file
pe = pefile.PE(file)
for section in pe.sections:
print(section.Name, hex(section.VirtualAddress),
hex(section.Misc_VirtualSize), section.SizeOfRawData )
def number_args(self):
args=[]
i = 0
for name in sys.argv[1:]:
args.append(name)
if len(args) > 0:
for command in args:
print(f"arg{i} = {command}")
i+=1
return args
def run_args(self):
analyze_sections = self.analyze_sections
args=[]
i = 0
for name in sys.argv[1:]:
args.append(name)
if len(args) > 0:
for command in args:
if command[-4:] == ".exe":
print(f"Found a .exe: {command}")
self.analyze_sections(command)
else:
print(f"Did not analyze {command}, it is not a .exe")
def list_DLLs(self, file=sys.argv[1]):
# Use pefile to list the DLLs a binary will load and function calls in the DLLs
print("\nPrinting the DLLs...")
file = file
pe = pefile.PE(file)
for entry in pe.DIRECTORY_ENTRY_IMPORT:
print(entry.dll)
for function in entry.imports:
print(f"\t {function.name}")
if __name__ == "__main__":
a = LoopArgs()
a.number_args()
a.run_args()
a.list_DLLs()