-
Notifications
You must be signed in to change notification settings - Fork 24
/
Create_Malware_Profile.py
120 lines (103 loc) · 5.82 KB
/
Create_Malware_Profile.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
#!/usr/bin/env python
__version__ = "0.0.1"
__date__ = "07.01.2013"
__author__ = "Joseph Zeranski"
__maintainer__ = "Joseph Zeranski"
__email__ = "madsc13ntist gmail.com"
__copyright__ = "Copyright 2013, " + __author__
__license__ = "MIT"
__status__ = "Prototype"
__credits__ = [""]
__description__= "Use IDAPython to create a profile for a malware sample."
####################### MIT License #######################
# Copyright (c) 2013 Joseph Zeranski
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###########################################################
from idautils import *
import os
import time
from time import strftime
#import struct
def carveSelectedBytes(outfile="", start=SelStart(), end=SelEnd(), assemble=False):
"""
Carve specified bytes out to a file.
"""
if outfile == "":
outfile = AskFile(1, hex(start)+".bin", "Save As")
with open(outfile, "wb") as fp:
if not assemble:
for ea in range(start, end+1):
fp.write(chr(GetOriginalByte(ea)))
else:
for head in Heads(start, end):
#ops = [ struct.pack("I", GetOriginalByte(b)) for b in range(head, ItemEnd(head)+1)]
#i=pydasm.get_instruction(''.join(ops),pydasm.MODE_32)
#asm = pydasm.get_instruction_string(i,pydasm.FORMAT_INTEL,0)
asm = str(GetDisasm(head).split(';')[0]).rstrip(" ") ###### REMOVE COMMENTING. It can throw off hashes/diff later.
fp.write(asm+"\n")
def SafeName(filename):
"""
Sanitize a filename if it contains illegal characters.
"""
return "".join([c for c in filename if c.isalpha() or c.isdigit() or c in [' ', '_'] ]).rstrip()
def BestName(ea, directory):
"""
Figure out the most useful legal/available filename to use for dumping carved bytes.
ea - the effective address for the object you are trying to name
directory - the directory where the file will be created.
"""
try:
open(directory + os.sep + SafeName(Demangle(Name(ea), INF_SHORT_DN)), 'w').close()
os.unlink(directory + os.sep + SafeName(Demangle(Name(ea), INF_SHORT_DN)))
return directory + os.sep + SafeName(Demangle(Name(ea), INF_SHORT_DN))
except:
try:
open(directory + os.sep + SafeName(Name(ea)), 'w').close()
os.unlink(directory + os.sep + SafeName(Name(ea)))
return directory + os.sep + SafeName(Name(ea))
except:
return directory + os.sep + SafeName("sub_" + hex(ea).lstrip('0x'))
########################### MAIN ###########################
if __name__ == '__main__':
print("NOTE: Addresses and names (\"sub_403D50\", \"loc_4022EA\", \"4022ec\", etc) are (double) clickable.")
print(" %s" % (strftime("%Y-%m-%d %H:%M:%S")))
print("-------------------- Functions ------------------------------")
ea = SegByBase(SegByName(".text"))
md5 = GetInputFileMD5().lower()
profile_dir = os.getenv("USERPROFILE") + os.sep + "Desktop" + os.sep + md5
func_dir = profile_dir + os.sep + "functions"
obj_dir = profile_dir + os.sep + "objects"
######### DUMP SUBROUTINES #########
print("Adding %d subroutines... saved to %s" % (len([ x for x in Functions(SegStart(ea), SegEnd(ea)) ]), func_dir))
if not os.path.exists(func_dir):
os.makedirs(func_dir)
for funcea in Functions(SegStart(ea), SegEnd(ea)):
fname = BestName(funcea, func_dir)
#carveSelectedBytes(fname, funcea, FindFuncEnd(funcea))
carveSelectedBytes(fname+".asm", funcea, FindFuncEnd(funcea), True)
#print("%x-%x saved to: %s" % (funcea, FindFuncEnd(funcea), fname))
######### DUMP OBJECTS #########
if not os.path.exists(obj_dir):
os.makedirs(obj_dir)
for seg in Segments():
if SegName(seg) != ".text":
if not os.path.exists(obj_dir + os.sep + SegName(seg)):
os.makedirs(obj_dir + os.sep + SegName(seg))
print("Adding %d data objects... saved to %s" % (len([ x for x in Heads(SegStart(seg), SegEnd(seg)) ]), obj_dir + os.sep + SegName(seg)))
for head in Heads(SegStart(seg), SegEnd(seg)):
fname = BestName(head, obj_dir + os.sep + SegName(seg))
carveSelectedBytes(fname, head, head+ItemSize(head))
#print("%s (%d bytes) saved to: %s\n%s" % (Name(head), ItemSize(head), fname, GetString(head)))
######### Create ssdeep signatures #########
'''
ssdeep_file = profile_dir+os.sep+md5+'.ssdeep'
if os.path.exists(ssdeep_file):
os.unlink(ssdeep_file)
fuzzy_hashes = CreateFuzzyProfile(profile_dir) ###################<<<< FIX THIS PART!!!! WHY IS THIS TAKING SO LONG?!?!??!?!!
print("Creating ssdeep signatures. saved to: %s" % (ssdeep_file))
with open(ssdeep_file, 'w') as ss:
for line in fuzzy_hashes:
ss.write(line + "\n")
'''