Skip to content

Commit

Permalink
Work towards python3 compatible codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
trolldbois committed May 14, 2017
1 parent 420510c commit 3ab7055
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 65 deletions.
2 changes: 1 addition & 1 deletion haystack/allocators/win32/winheap.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def print_heap_analysis(self, heap, verbose):
if heap_bits != process_bits:
special += ' (!%d bits heap!) ' % heap_bits

print(('[+] %sHEAP:0x%0.8x' % (special, addr), m))
print('[+] %sHEAP:0x%0.8x' % (special, addr), m)
if not verbose:
return
#
Expand Down
14 changes: 7 additions & 7 deletions haystack/reverse/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def clean(digraph):
for i, g1 in enumerate(graphs):
for g2 in graphs[i + 1:]:
if networkx.is_isomorphic(g1, g2):
print(('numNodes:%d graphs %d, %d are isomorphic' % (numNodes, i, i + 1)))
print('numNodes:%d graphs %d, %d are isomorphic' % (numNodes, i, i + 1))
isoGraph.add_edge(g1, g2, {'isomorphic': True})
if g2 in todo:
todo.remove(g2)
Expand Down Expand Up @@ -170,9 +170,9 @@ def clean(digraph):
for addr in stack_addrs]) # new, no long

stacknodes = list(set(bigGraph.nodes()) & stack_addrs_txt)
print(('stacknodes left', len(stacknodes)))
print('stacknodes left', len(stacknodes))
orig = list(set(graph.nodes()) & stack_addrs_txt)
print(('stacknodes orig', len(orig)))
print('stacknodes orig', len(orig))

# identify strongly referenced allocators
degreesList = [(bigGraph.in_degree(node), node)
Expand All @@ -188,14 +188,14 @@ def printImportant(ctx, digraph, degreesList, ind, bigGraph):
s1 = ctx.structures[addr] # TODO FIXME RAISES
# s1 = s1._load() #structure.cacheLoad(ctx, int(saddr,16))
s1.decodeFields()
print((s1.to_string()))
print(s1.to_string())
# strip the node from its predecessors, they are numerously too numerous
impDiGraph = networkx.DiGraph()
root = '%d nodes' % nb
impDiGraph.add_edge(root, saddr)
depthSubgraph(bigGraph, impDiGraph, [saddr], 2)
print(('important struct with %d structs pointing to it, %d pointerFields' % (
digraph.in_degree(saddr), digraph.out_degree(saddr))))
print('important struct with %d structs pointing to it, %d pointerFields' % (
digraph.in_degree(saddr), digraph.out_degree(saddr)))
# print 'important struct with %d structs pointing to it, %d
# pointerFields'%(impDiGraph.in_degree(saddr),
# impDiGraph.out_degree(saddr))
Expand All @@ -212,7 +212,7 @@ def printImportant(ctx, digraph, degreesList, ind, bigGraph):
st.resolvePointers()
# st.pointerResolved=True
# st._aggregateFields()
print((node, st.get_signature(text=True)))
print(node, st.get_signature(text=True))
# clean and print
# s1._aggregateFields()
impDiGraph.remove_node(root)
Expand Down
5 changes: 3 additions & 2 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
else:
import unittest

from run_src_app import run_app_test
from run_src_app import makeTests
#from . import run_src_app
#from run_src_app import run_app_test
#from run_src_app import makeTests

__author__ = "Loic Jaquemet"
__copyright__ = "Copyright (C) 2012 Loic Jaquemet"
Expand Down
3 changes: 2 additions & 1 deletion test/haystack/allocators/win32/test_win7walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Tests for haystack.reverse.structure."""

from __future__ import print_function
import logging
import unittest
import sys
Expand Down Expand Up @@ -150,7 +151,7 @@ def test_get_frontendheap(self):
log.debug('UnusedBytes == 0x5, SegmentOffset == %d' % st._0._1._0.SegmentOffset)

# FIXME, in child of 0x005c0000. LFH. What are the flags already ?
print hex(chunk_addr)
print(hex(chunk_addr))
self.assertTrue(st._0._1.UnusedBytes & 0x80, 'UnusedBytes said this is a BACKEND chunk , Flags | 2')
allocs.append((chunk_addr, chunk_size)) # with header

Expand Down
3 changes: 2 additions & 1 deletion test/haystack/mappings/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Tests haystack.utils ."""

from __future__ import print_function
import logging
import mmap
import struct
Expand Down Expand Up @@ -45,7 +46,7 @@ def test_mmap_hack64(self):
ret = [m for m in handler.get_mappings() if heapmap in m]
if len(ret) == 0:
for m in handler.get_mappings():
print m
print(m)
# heapmap is a pointer value in local memory
self.assertEquals(len(ret), 1)
# heapmap is a pointer value to this executable?
Expand Down
2 changes: 1 addition & 1 deletion test/haystack/mappings/test_vol.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestMapper(unittest.TestCase):
def setUp(self):
try:
import volatility
except ImportError,e:
except ImportError as e:
self.skipTest('Volatility not installed')
pass

Expand Down
5 changes: 3 additions & 2 deletions test/haystack/reverse/heuristics/test_dsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Tests for haystack.reverse.structure."""

from __future__ import print_function
import unittest
import logging

Expand Down Expand Up @@ -402,7 +403,7 @@ def test_utf16_1(self):
self.assertTrue(fields[1].is_string())
self.assertTrue(fields[2].is_zeroes())

print _record.to_string()
print(_record.to_string())

def test_utf16_2(self):
_record = self.heap_context.get_record_for_address(0xa4028)
Expand All @@ -413,7 +414,7 @@ def test_utf16_2(self):
rev.reverse_record(self.heap_context, _record)
fields = _record.get_fields()

print _record.to_string()
print(_record.to_string())



Expand Down
21 changes: 11 additions & 10 deletions test/haystack/reverse/heuristics/test_reversers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright (C) 2011 Loic Jaquemet loic.jaquemet+python@gmail.com
#

from __future__ import print_function
import os
import logging
import unittest
Expand Down Expand Up @@ -62,7 +63,7 @@ def test_preload(self):
## TODO enforce better constraints, and a dynamic Contraints engine
results = krtr.get_search_results()
for record_name, addresses in results.items():
print record_name, len(addresses)
print(record_name, len(addresses))
pass


Expand Down Expand Up @@ -106,7 +107,7 @@ def test_double_iter(self):
# print mid.to_string()
# reverse the list
self.dllr.reverse()
print mid.to_string()
print(mid.to_string())
size = len(mid)
# there is a list for this size
self.assertIn(size, self.dllr.lists)
Expand Down Expand Up @@ -690,17 +691,17 @@ def test_doublelink(self):
# list goes from 0xccd28, 0xccd00 to 0x98268
#_record = self._context.get_record_for_address(0xccd28)
_record = self._context.get_record_for_address(0xccd00)
print _record.to_string()
print(_record.to_string())
#_record.set_reverse_level(9)
##
rev.reverse_record(self._context, _record)

print _record.to_string()
print(_record.to_string())
n1 = self._context.get_record_for_address(0x000ccae8)
print n1.to_string()
print(n1.to_string())
tail = self._context.get_record_for_address(0x98268)
print tail.to_string()
expected = [0xccd28,0xccd00L,0xccae8,0xcca50,0xcca28,0xcc428,0xc6878,0xdcbc8,0xdcb40,0xcd300,0xbbf78,0xbefd8,0xbecd8,0xbc560,0xbbee0,0xbbda8,0xbbb38,0xbbae0,0xa6518,0xb5d00,0xb5cd8,0xb5cb0,0xb5b70,0xb1aa8,0xa20b8,0x9e2f8,0xa1920,0xa1838,0x98268]
print(tail.to_string())
expected = [0xccd28,0xccd00,0xccae8,0xcca50,0xcca28,0xcc428,0xc6878,0xdcbc8,0xdcb40,0xcd300,0xbbf78,0xbefd8,0xbecd8,0xbc560,0xbbee0,0xbbda8,0xbbb38,0xbbae0,0xa6518,0xb5d00,0xb5cd8,0xb5cb0,0xb5b70,0xb1aa8,0xa20b8,0x9e2f8,0xa1920,0xa1838,0x98268]
size_records = len(tail)
# offset = 8
offset = 8
Expand Down Expand Up @@ -739,16 +740,16 @@ def test_doublelink(self):
# list goes from 0xccd28, 0xccd00 to 0x98268
#_record = self._context.get_record_for_address(0xccd28)
_record = self._context.get_record_for_address(0xccd00)
print _record.to_string()
print(_record.to_string())
_record.set_reverse_level(10)
rev.reverse_context(self._context)
print _record.to_string()
print(_record.to_string())
pass

def test_otherlink(self):
# 0xa6f40, 0xa6f70
_record = self._context.get_record_for_address(0xccd00)
print _record.to_string()
print(_record.to_string())
#import code
#code.interact(local=locals())

Expand Down
7 changes: 4 additions & 3 deletions test/haystack/reverse/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import logging
import unittest

Expand Down Expand Up @@ -30,12 +31,12 @@ def test_pred(self):
_record = api.get_record_at_address(self.memory_handler, addr)
self.assertEqual(_record.address, addr)
#self.assertEqual(len(_record.get_fields()), 3)
print _record.to_string()
print(_record.to_string())
# FIXME - process must be reversed. Graph must be generated.
pred = api.get_record_predecessors(self.memory_handler, _record)
print 'pred', pred
print('pred', pred)
for p in pred:
print p.to_string()
print(p.to_string())
pass

if __name__ == '__main__':
Expand Down
9 changes: 5 additions & 4 deletions test/haystack/reverse/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
#

from __future__ import print_function

def main():
from haystack.reverse import context
Expand Down Expand Up @@ -99,9 +100,9 @@ def rec_add_child(graph, knowns, addr, t=''):
return
graph.add_edge(n(om[addr]), n(om[c]))
childscount = len(om[c].children)
print 'c:', c, 'has', childscount, 'children'
print('c:', c, 'has', childscount, 'children')
if childscount > 0:
print om[c]
print(om[c])
# add rec
if c in knowns:
return
Expand All @@ -116,9 +117,9 @@ def rec_add_parent(graph, knowns, addr, t=''):
return
graph.add_edge(n(om[p]), n(om[addr]))
childscount = len(om[p].parents)
print 'p:', p, 'has', childscount, 'parents'
print('p:', p, 'has', childscount, 'parents')
if childscount > 0:
print om[p]
print(om[p])
# add rec
if p in knowns:
return
Expand Down
5 changes: 3 additions & 2 deletions test/haystack/reverse/test_fieldtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Tests for haystack.reverse.structure."""

from __future__ import print_function
import logging
import unittest

Expand Down Expand Up @@ -61,7 +62,7 @@ def test_gaps(self):
g1 = fieldtypes.Field('gap_0', 0, fieldtypes.UNKNOWN, 1, False)
self.assertEqual(len(g1), 1)
self.assertTrue(g1.is_gap())
print g1.to_string('\x00\x00\x00\x00')
print(g1.to_string('\x00\x00\x00\x00'))
self.assertIn('ctypes.c_ubyte*1 )', g1.to_string('\x00\x00\x00\x00'))

g2 = fieldtypes.Field('gap_0', 0, fieldtypes.UNKNOWN, 2, False)
Expand Down Expand Up @@ -121,7 +122,7 @@ def test_subtype(self):
self.assertIsInstance(f2.field_type, fieldtypes.FieldTypeStruct)
self.assertEqual(f2.field_type.name, 'LIST_ENTRY')

print _record.to_string()
print(_record.to_string())


if __name__ == '__main__':
Expand Down
13 changes: 7 additions & 6 deletions test/haystack/reverse/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# Copyright (C) 2011 Loic Jaquemet loic.jaquemet+python@gmail.com
#

__author__ = "Loic Jaquemet loic.jaquemet+python@gmail.com"

from __future__ import print_function
import logging
import operator
import unittest
Expand All @@ -18,6 +17,8 @@
from haystack.mappings.base import MemoryHandler, AMemoryMapping
from haystack.mappings.file import LocalMemoryMapping

__author__ = "Loic Jaquemet loic.jaquemet+python@gmail.com"

'''
Testing pointer patterns recognition.
'''
Expand All @@ -37,15 +38,15 @@ def setUpClass(cls):
# make a fake dir
try:
os.mkdir('test/reverse/')
except OSError,e:
except OSError as e:
pass
try:
os.mkdir('test/reverse/fakedump')
except OSError,e:
except OSError as e:
pass
try:
os.mkdir('test/reverse/fakedump/cache')
except OSError,e:
except OSError as e:
pass

def setUp(self):
Expand Down Expand Up @@ -100,7 +101,7 @@ def _make_mmap(self, mstart, mlength, struct_offset, seq, word_size):
raise ValueError('error 1 on length dump %d ' % (len(dump)))
dump2 = ''.join(dump)
if len(dump) * word_size != len(dump2):
print dump
print(dump)
raise ValueError(
'error 2 on length dump %d dump2 %d' %
(len(dump), len(dump2)))
Expand Down

0 comments on commit 3ab7055

Please sign in to comment.