forked from androguard/androguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
androdump.py
executable file
·149 lines (116 loc) · 4.83 KB
/
androdump.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
#!/usr/bin/env python
# This file is part of Androguard.
#
# Copyright (C) 2012, Anthony Desnos <desnos at t0t0.fr>
# All rights reserved.
#
# Androguard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Androguard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Androguard. If not, see <http://www.gnu.org/licenses/>.
import sys, os, cmd, threading, re, atexit
from optparse import OptionParser
import androguard, androconf, jvm
# External Libraries
# python-ptrace : http://bitbucket.org/haypo/python-ptrace/
from ptrace import PtraceError
from ptrace.tools import locateProgram
from ptrace.debugger import ProcessExit, DebuggerError, PtraceDebugger, ProcessExit, ProcessSignal, NewProcessEvent, ProcessExecution
from ptrace.debugger.memory_mapping import readProcessMappings
####################
option_0 = { 'name' : ('-i', '--input'), 'help' : 'pid', 'nargs' : 1 }
option_1 = { 'name' : ('-v', '--version'), 'help' : 'version of the API', 'action' : 'count' }
options = [option_0, option_1]
MAGIC_PATTERN = "\xca\xfe\xba\xbe"
class AndroPreDump :
def __init__(self, input) :
self.data = []
self.pid = int(input)
self.debugger = PtraceDebugger()
self.process = self.debugger.addProcess(self.pid, is_attached=False)
atexit.register(self.debugger.quit)
Header = False
Code = False
self.procmaps = readProcessMappings(self.process)
for pm in self.procmaps:
if pm.permissions.find("w") != -1 and pm.pathname == None :
# if Code == False and Header == True :
# data = self.process.readBytes(pm.start, pm.end-pm.start)
# idx = data.find("SourceFile")
# if idx != -1 :
# print "CODE", pm
# self.data.append( (pm, data, idx) )
# Code = True
if Header == False :
data = self.process.readBytes(pm.start, pm.end-pm.start)
idx = data.find(MAGIC_PATTERN)
if idx != -1 :
print "HEADER", pm
self.data.append( (pm, data) )
Header = True
self.dumpMemory( "java_dump_memory" )
# self.dumpFiles( "java_files" )
def write(self, idx, buff) :
self.process.writeBytes( idx, buff )
def getFilesBuffer(self) :
for i in self.data :
d = i[1]
x = d.find(MAGIC_PATTERN)
idx = x
while x != -1 :
yield i[0].start + idx, d[x:]
d = d[x+len(MAGIC_PATTERN):]
idx += len(MAGIC_PATTERN)
x = d.find(MAGIC_PATTERN)
idx += x
def dumpMemory(self, base_filename) :
for i in self.data :
fd = open(base_filename + "-" + "0x%x-0x%x" % (i[0].start, i[0].end), "w")
fd.write( i[1] )
fd.close()
def dumpFiles(self, base_filename) :
for i in self.data :
fd = open(base_filename + "-" + "0x%x-0x%x" % (i[0].start + i[2], i[0].end), "w")
fd.write( i[1][i[2]:] )
fd.close()
class AndroDump :
def __init__(self, adp) :
self.__adp = adp
for i in self.__adp.getFilesBuffer() :
try :
print "0x%x :" % (i[0])
j = jvm.JVMFormat( i[1] )
for method in j.get_methods() :
print "\t -->", method.get_class_name(), method.get_name(), method.get_descriptor()
# if (method.get_class_name() == "Test2" and method.get_name() == "main") :
# print "patch"
# code = method.get_code()
#code.remplace_at( 51, [ "bipush", 20 ] )
# code.show()
# print "\t\t-> %x" % (len(j.save()))
# self.__adp.write( i[0], j.save() )
except Exception, e :
print e
def main(options, arguments) :
if options.input != None :
apd = AndroPreDump( options.input )
AndroDump( apd )
elif options.version != None :
print "Androdump version %s" % androconf.ANDROGUARD_VERSION
if __name__ == "__main__" :
parser = OptionParser()
for option in options :
param = option['name']
del option['name']
parser.add_option(*param, **option)
options, arguments = parser.parse_args()
sys.argv[:] = arguments
main(options, arguments)