Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install haystack

How to install haystack

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install haystack
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.15 Available View build log
0.13 Available View build log
0.12 Available View build log
0.11 Available View build log
0.10 Available View build log
0.9 Available View build log
0.8 Available View build log
0.7 Available View build log
0.6 Available View build log
0.5 Available View build log
0.4 Available View build log
0.3 Available View build log
0.2 Available View build log
Windows (64-bit)
0.15 Available View build log
0.13 Available View build log
0.12 Available View build log
0.11 Available View build log
0.10 Available View build log
0.9 Available View build log
0.8 Available View build log
0.7 Available View build log
0.6 Available View build log
0.5 Available View build log
0.4 Available View build log
0.3 Available View build log
0.2 Available View build log
Mac OS X (10.5+)
0.15 Available View build log
0.13 Available View build log
0.12 Available View build log
0.11 Available View build log
0.10 Available View build log
0.9 Available View build log
0.8 Available View build log
0.7 Available View build log
0.6 Available View build log
0.5 Available View build log
0.4 Available View build log
0.3 Available View build log
0.2 Available View build log
Linux (32-bit)
0.15 Available View build log
0.13 Available View build log
0.12 Available View build log
0.11 Available View build log
0.10 Available View build log
0.9 Available View build log
0.8 Available View build log
0.7 Available View build log
0.6 Available View build log
0.5 Available View build log
0.4 Available View build log
0.3 Available View build log
0.2 Available View build log
Linux (64-bit)
0.15 Available View build log
0.13 Available View build log
0.12 Available View build log
0.11 Available View build log
0.10 Available View build log
0.9 Available View build log
0.8 Available View build log
0.7 Available View build log
0.6 Available View build log
0.5 Available View build log
0.4 Available View build log
0.3 Available View build log
0.2 Available View build log
 
Author
License
GPL
Dependencies
Depended by
Lastest release
version 0.15 on May 23rd, 2012

Introduction:

python-haystack is an heap analysis framework, focused on classic C structure matching.

The first class of algorithms gives the ability to search for known structures in a live process's memory, or in a memory dump.

The second class of algorithms aims at giving a reverse engineering look at a memory dump, focusing on reconstruction, classification of classic C structures from memory. Heap analysis. Dynamic types definition.

How to get a memory dump:

While technically you could use a third party tool, haystack actually need memory mapping information to work with. So there is a dumping tool included :

$ sudo haystack-dump dump <pid> dumps/myssh.dump

You can easily reproduce the format of the dump, its a folder/archive containing each memory map in a separate file : * content in a file named after it's start/end addresses ( 0x000700000-0x000800000 ) * 'mappings' file containing memory mappings metadata. ( mappings )

Search for known structures:

You need the python definition/ctypes structures of the known structures. An example would be sslsnoop, which provide python ctypes structures for openssl and openssh structures.

Quick info: This demonstrate the ability to brute-force the search of a known structure, based on fields types assumptions or constraints.

The magic is performed in the model.py module. The constraints are applied on the python ctypes structures by the 'expectedValues' static field.

Command line example:

For example, this will dump the session_state structures + pointed children structures as an python object that we can play with. Lets assume we have an ssh client or server as pid 4042.

$ sudo haystack --pid 4042 sslsnoop.ctypes_openssh.session_state search > instance.pickled $ sudo haystack --pid 4042 sslsnoop.ctypes_openssh.session_state refresh 0xb8b70d18 > instance.pickled $ sudo haystack --pid xxxx <your ctypes Structure> search > instance.pickled

Graphic example :

There is also an attempt at a Graphical GUI ( Qt4 ) Dump the process, then you can open it in the GUI :

$ haystack-gui # ( and Ctrl-O , click click) $ haystack-gui --dumpname dumps/myssh.dump

You can the search a structure from the heap of that memory mapping. You have to import your extensions before that to have them listed in the search dialog. ( try sslsnoop.ctypes_openssh )

Tip: As this is a beta version, sslsnoop is hard-imported in the GUI. You should have it installed.

python script interpreter example:
>>> import haystack
>>> pid = 4042
>>> haystack.findStruct( pid , sslsnoop.ctypes_openssh.session_state)
>>> haystack.findStruct( pid , ctypes_example.big_struct)
Extensibility:

It's easy to add new structures. Its basically the ctypes definition of C structures that should be done following the next 4 steps :

  1. Your class must extend haystack.model.LoadableMembers.
  2. You must give your class a completed _fields_ (with one _ ), like all ctypes.Structure

System Message: WARNING/2 (<string>, line 91)

Enumerated list ends without a blank line; unexpected unindent.
b bis) Optional - You can add an expectedValues dict() to your ctype
classes to add some constraints.
b ter) Optional - You can override isValid and loadMembers to implement
advanced constraints validation.

System Message: WARNING/2 (<string>, line 95)

Definition list ends without a blank line; unexpected unindent.
  1. call model.registerModule(sys.modules[__name__])
Easy 'creation' :
use h2xml and xml2py from ctypeslib to generate a python module from a C header.
Advanced use :
You can override methods isValid and loadMembers to implements advanced data loading and constraints validation. ( see sslsnoop for loading cipher structures from void pointers )

The global algorithm :

  1. The ctypes structure is mapped at the first offset of the memory mapping.
  2. The method loadMembers is called.
  3. The method isValid is called on self.
  4. A validation test is done for each members, constraints and memory space validity (pointers) are tested. The validation does not recurse.
  5. Each members is then 'loaded' to local space. If the value is a pointer or a model.LoadableMembers type, it's recursively Loaded. ( and validated). If the recursive loading fails, the calls fails. bye-bye.
  6. If all contraints are respected, we have a match.
  7. Move to see next offset, goto a)

Heap analysis / Memory Reverser / Memory forensics:

Quick info: This tool parse the heap for allocator structures, pointers values, small integers and text (ascii/utf). Given all the previous information, it can extract instances and helps you in classifying and defining structures types.

TODO: implement feedback

Command line example:

System Message: WARNING/2 (<string>, line 134)

Title underline too short.

Command line example:
--------------------

This will create several files in the folder containing <yourdumpname>.

$ python haystack-reverse instances <yourdumpname>

The most insteresting one being the <yourdumpname>.headers_values.py that gives you an ctypes listing of all found structures, with gestimates on fields types.

A <yourdumpname>.gexf file is also produced to help you visualize instances links. It gets messy for any kind of serious application.

Show ordered list of structures, by similarities :

$ python haystack-reverse show <yourdumpname>

Show only structures of size 324:

$ python haystack-reverse show --size 324 <yourdumpname>

Write to file an attempt to reversed the original types hierachy :

$ python haystack-reverse typemap <yourdumpname>

Extension examples :

@ see sslsnoop in the Pypi repo. openssl and nss structures are generated. @ see ctypes-kernel on my github. Linux kernel structure are generated from a build kernel tree. (VMM is abitch)

Pseudo Example for extension :

from haystack.model import LoadableMembers, RangeValue, NotNull

class OpenSSLStruct(LoadableMembers):
pass
class RSA(OpenSSLStruct):

''' rsa/rsa.h ''' _fields_ = [ ("pad", ctypes.c_int), ("version", ctypes.c_long), ("meth",ctypes.POINTER(BIGNUM)),#const RSA_METHOD *meth; ("engine",ctypes.POINTER(ENGINE)),#ENGINE *engine; ('n', ctypes.POINTER(BIGNUM) ), ## still in ssh memap ('e', ctypes.POINTER(BIGNUM) ), ## still in ssh memap ('d', ctypes.POINTER(BIGNUM) ), ## still in ssh memap ('p', ctypes.POINTER(BIGNUM) ), ## still in ssh memap ('q', ctypes.POINTER(BIGNUM) ), ## still in ssh memap ('dmp1', ctypes.POINTER(BIGNUM) ), ('dmq1', ctypes.POINTER(BIGNUM) ), ('iqmp', ctypes.POINTER(BIGNUM) ), ("ex_data", CRYPTO_EX_DATA ), ("references", ctypes.c_int), ("flags", ctypes.c_int), ("_method_mod_n", ctypes.POINTER(BN_MONT_CTX) ), ("_method_mod_p", ctypes.POINTER(BN_MONT_CTX) ), ("_method_mod_q", ctypes.POINTER(BN_MONT_CTX) ), ("bignum_data",ctypes.POINTER(ctypes.c_ubyte)), ## moue c_char_p ou POINTER(c_char) ? ("blinding",ctypes.POINTER(BIGNUM)),#BN_BLINDING *blinding; ("mt_blinding",ctypes.POINTER(BIGNUM))#BN_BLINDING *mt_blinding; ] expectedValues={

System Message: WARNING/2 (<string>, line 175); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (<string>, line 175); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (<string>, line 175); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (<string>, line 175); backlink

Inline emphasis start-string without end-string.

System Message: ERROR/3 (<string>, line 200)

Unexpected indentation.
"pad": [0], "version": [0], "references": RangeValue(0,0xfff), "n": [NotNull], "e": [NotNull], "d": [NotNull], "p": [NotNull], "q": [NotNull], "dmp1": [NotNull], "dmq1": [NotNull], "iqmp": [NotNull]

System Message: WARNING/2 (<string>, line 211)

Block quote ends without a blank line; unexpected unindent.

} def loadMembers(self, mappings, maxDepth):

System Message: ERROR/3 (<string>, line 213)

Unexpected indentation.

print 'example' if not LoadableMembers.loadMembers(self, mappings, maxDepth):

System Message: ERROR/3 (<string>, line 215)

Unexpected indentation.
log.debug('RSA not loaded') return False

System Message: WARNING/2 (<string>, line 217)

Block quote ends without a blank line; unexpected unindent.

return True

# register to haystack model.registerModule(sys.modules[__name__])

#EOF

not so FAQ :

What does it do ?:

The basic functionnality is to search in a process' memory maps for a specific C Structures. The extended reverse engineering functionnality aims at reversing structures from memory/heap analysis.

How do it knows that the structures is valid ? :

You add some constraints ( expectedValues ) on the fields. Pointers are also a good start.

Where does the idea comes from ? :

http://www.hsc.fr/ressources/breves/passe-partout.html.fr originally. since I started in March 2011, I have uncovered several other related previous work. Most of them are in the docs/ folder. Other related work are mona.py from Immunity, some other Madiant stuff... In a nutshell, this is probably not an original idea. But yet, I could not find a operational standalone lib for live memory extraction for sslsnoop, soo....

What are the dependencies ? :

System Message: WARNING/2 (<string>, line 251)

Title underline too short.

What are the dependencies ? :
----------------------------

python-ptrace on linux winappdbg on win32 several others...

Subscribe to package updates

Last updated May 23rd, 2012

Download Stats

Last month:3

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.