-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevasion_analyze.py
executable file
·174 lines (133 loc) · 6.63 KB
/
evasion_analyze.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
'''
GOAL
====
Understanding which return value we need to bypass an evasion technique.
PROBLEM:
=========
We have to investigate manually which value we need to reach the correct malware behavior,
here we can apply surgically a symexecution and simply get that value.
REFERENCE WRITE-UPs:
===================
1) https://www.lastline.com/labsblog/malware-evasion-techniques/
NOTES:
======
1) This script assumes that you are running the malware on a Windows7 Vm that you can reach via ssh.
You can specify this information using this script:
python evasion_analyze.py <VM_IP> <GDB_SERVER_PORT> <SSH_USER> <SSH_PASSWORD>
The binary has to be inside the virtual machine to be run concretely
and on the local machine to create an angr project.
The name of the binary it's specified in the global var MALWARE_BIN in this python script.
2) Make sure you have disabled the 'DLL can move' feature of the binary so we can use absolute addresses
for breakpoints. Use CFFExplorer to edit the binary or similar software.
See this -> https://www.sans.org/blog/tools-for-analyzing-static-properties-of-suspicious-files-on-windows/.
MALWARE URL:
============
- https://www.virustotal.com/gui/file/47d02763457fe39edd3b84f59e145330ffd455547da7cbf67c3f0cb3ddf10542/detection
- MD5: 53f6f9a0d0867c10841b815a1eea1468
'''
import angr
import avatar2
import paramiko
import logging
import warnings
import sys
l = logging.getLogger(name=__name__)
l.setLevel(logging.DEBUG)
warnings.filterwarnings(action='ignore',module='.*paramiko.*')
from angr_targets import AvatarGDBConcreteTarget
from angr.procedures.win32.GetProcessAffinityMask import GetProcessAffinityMask
from angr import options as o
###########################################################################################
# GLOBAL OBJECTS
###########################################################################################
SSH_CLIENT = None
AVATAR_GDB = None
ANGR_PROJECT = None
###########################################################################################
###########################################################################################
# CONFIG
###########################################################################################
MALWARE_BIN = '53f6f9a0d.exe'
VM_IP = ''
GDB_SERVER_PORT = None
SSH_USER = ''
SSH_PASSWORD = ''
# Remember to change the flag of the binary 'DLL can move' so we can use absolute
# addresses to set breakpoints.
RESTART_MALWARE = "C:\MinGW\\bin\\gdbserver.exe 0.0.0.0:9999 E:\\" + MALWARE_BIN
###########################################################################################
###########################################################################################
# INTERESTING ADDRESSES
###########################################################################################
END_UNPACKING = 0x439D2E
MALWARE_MAIN = 0x40FAE6
# TODO address taken from the debugging, we can extract it automatically.
CALL_TO_GetProcessAffinityMask = 0x7502A889
RANSOMWARE_BEHAVIOR = 0x4214e4
###########################################################################################
###########################################################################################
# HELPERS
###########################################################################################
def connectToVM():
global SSH_CLIENT
SSH_CLIENT = paramiko.SSHClient()
SSH_CLIENT.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SSH_CLIENT.connect(VM_IP, port=22, username=SSH_USER, password=SSH_PASSWORD)
def setup_x86():
print("Configure a windows machine with a static IP %s. "
"Check windows firewall configurations to be sure that the connections to %s:%s are not blocked\n"
"Install gdbserver on the machine, b"
"e careful the architecture (x86 or x64) of gdbserver should be the same as the debugged binary.\n"
"Currently using Cygwin for 32 bit gdbserver and Cygwin for 64 bit gdbserver" % (VM_IP,
VM_IP,
GDB_SERVER_PORT))
print("On windows machine execute gdbserver %s:%s path/to/binary.exe" % (VM_IP, GDB_SERVER_PORT))
input("Press enter when gdbserver has been executed")
def sendCommandToVM(cmd):
(stdin, stdout, stderr) = SSH_CLIENT.exec_command(cmd)
def teardown():
global AVATAR_GDB
if AVATAR_GDB:
AVATAR_GDB.exit()
def execute_concretly(p, state, address, memory_concretize=[], register_concretize=[], timeout=0):
simgr = p.factory.simgr(state)
simgr.use_technique(angr.exploration_techniques.Symbion(find=[address], memory_concretize=memory_concretize,
register_concretize=register_concretize, timeout=timeout))
exploration = simgr.run()
return exploration.stashes['found'][0]
###########################################################################################
# LET'S START!
if __name__== "__main__":
try:
VM_IP = sys.argv[1]
GDB_SERVER_PORT = sys.argv[2]
SSH_USER = sys.argv[3]
SSH_PASSWORD = sys.argv[4]
except Exception:
print("Usage: python evasion_analyze.py <VM_IP> <GDB_SERVER_PORT> <SSH_USER> <SSH_PASSWORD>")
sys.exit(1)
connectToVM()
sendCommandToVM(RESTART_MALWARE)
AVATAR_GDB = AvatarGDBConcreteTarget(avatar2.archs.x86.X86, VM_IP, GDB_SERVER_PORT)
ANGR_PROJECT = angr.Project("./"+MALWARE_BIN, concrete_target=AVATAR_GDB, use_sim_procedures=True,
page_size=0x1000)
entry_state = ANGR_PROJECT.factory.entry_state(add_options=angr.options.unicorn)
new_concrete_state = execute_concretly(ANGR_PROJECT, entry_state, END_UNPACKING, [], [])
new_concrete_state = execute_concretly(ANGR_PROJECT, new_concrete_state, CALL_TO_GetProcessAffinityMask, [], [])
new_concrete_state.project.hook(CALL_TO_GetProcessAffinityMask, GetProcessAffinityMask())
new_concrete_state.options.update(o.refs)
simgr = ANGR_PROJECT.factory.simgr(new_concrete_state)
simgr.use_technique(angr.exploration_techniques.DFS())
while True:
next_simgr = simgr.step()
next_state = next_simgr.active[0]
print(next_state)
address = next_state.solver.eval(next_state.regs.pc)
if address == RANSOMWARE_BEHAVIOR:
l.warn("Reached ransomware behavior, concretize memory returned by GetProcessAffinityMask!")
break
# DO A
# next_state.mem[<address of lpSystemAffinityMask of GetProcessAffinityMask(2nd arg)> ].int.concrete
import ipdb; ipdb.set_trace()
teardown()
SSH_CLIENT.close()