-
Notifications
You must be signed in to change notification settings - Fork 38
/
flatten.py
66 lines (53 loc) · 1.98 KB
/
flatten.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
#!/usr/bin/env python
#
# Copyright 2011 ATC-NY (http://www.cyber-marshal.com/)
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# This program takes a mach-o image produced by the Mac Memory Reader and
# turns it into a flat file (an offset into the file is the physical memory
# address).
#
# NOTE - Using this on the 64-bit kernels is probably a mistake and may be
# buggy, because there are often large gaps in the mach-o image, which
# will result in extremely large flat file images.
import sys
import mmap
import volafox.vatopa.machaddrspace
if len(sys.argv) != 3:
print "Usage: %s <MAC MEMORY READER IMAGE> <FLAT IMAGE>."%sys.argv[0]
exit(0)
fin = open(sys.argv[1], 'rb')
ncmds = volafox.vatopa.machaddrspace.getncmds(fin)
lcmds = volafox.vatopa.machaddrspace.loadcommand(fin, ncmds)
fout = open(sys.argv[2], 'wb')
def copy(fin, fin_where, fout, fout_where, size):
fin.seek(fin_where)
fout.seek(fout_where)
remaining = size
while remaining > 0:
if remaining > 4096*16:
buffer = fin.read(4096*16)
remaining = remaining - 4096*16
else:
buffer = fin.read(remaining)
remaining = 0
fout.write(buffer)
for cmd in lcmds:
print("Copying: %s" % cmd)
copy(fin, cmd.fileoff, fout, cmd.vmaddr, cmd.filesize)
fin.close
fout.close