Breaking Code

December 13, 2009

Reusing Python generator objects

Filed under: Uncategorized — Tags: , — Mario Vilas @ 9:46 pm

Generators are one of those little things that I love about Python. Never heard of generators? Well, this is all much better explained here, but in a nutshell:

    def squares( size ):
        for x in xrange( size ):
            yield x * x
        return

The above code returns a generator object. This object is iterable, and each item is returned by the yield statement. The return statement ends the iteration.

    >>> list( squares( 10 ) )
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    >>> for value in squares( 5 ):
    ...     print value
    ...
    0
    1
    4
    9
    16

Now, there’s a little problem I found… I wanted an object than, when iterated, returned a certain sequence of objects. No problem so far. But here’s the catch: once it finished returning the list, I wanted to be able to reuse the generator.

    >>> gen = squares( 5 )
    >>> list( gen )        # Works the first time...
    [0, 1, 4, 9, 16]
    >>> list( gen )        # But not the second!
    []

So I came up with this solution:

    class Regenerator(object):
        """
        Calls a generator and iterates it. When it's finished iterating, the
        generator is called again. This allows you to iterate a generator more
        than once (well, sort of, since the generator is actually recreated).
        """
        def __iter__(self):
            return self
        def __init__(self, g_function, *v_args, **d_args):
            self.__g_function = g_function
            self.__v_args     = v_args
            self.__d_args     = d_args
            self.__g_object   = None
        def next(self):
            if self.__g_object is None:
                self.__g_object = self.__g_function( *self.__v_args, **self.__d_args )
            try:
                return self.__g_object.next()
            except StopIteration:
                self.__g_object = None
                raise

So, in the example we presented, this is what we’d do to be able to iterate the list of numbers more than once:

    def _squares( size ):
        for x in xrange( size ):
            yield x * x
        return
    def squares( size ):
        return Regenerator( _squares, size )

And alas, it works! 🙂

    >>> gen = squares( 10 )
    >>> list( gen )
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    >>> list( gen )
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    >>> list( gen )
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Download

regenerator.py

December 1, 2009

WinAppDbg 1.3 is out!

Filed under: Tools — Tags: , , , , , , , , , , , , , — Mario Vilas @ 4:12 am

What is WinAppDbg?

The WinAppDbg python module allows developers to quickly code instrumentation scripts in Python under a Windows environment.

It uses ctypes to wrap many Win32 API calls related to debugging, and provides an object-oriented abstraction layer to manipulate threads, libraries and processes, attach your script as a debugger, trace execution, hook API calls, handle events in your debugee and set breakpoints of different kinds (code, hardware and memory). Additionally it has no native code at all, making it easier to maintain or modify than other debuggers on Windows.

The intended audience are QA engineers and software security auditors wishing to test / fuzz Windows applications with quickly coded Python scripts. Several ready to use utilities are shipped and can be used for this purposes.

Current features also include disassembling x86 native code (using the open source diStorm project, see http://ragestorm.net/distorm/), debugging multiple processes simultaneously and produce a detailed log of application crashes, useful for fuzzing and automated testing.

Where can I find WinAppDbg?

Project homepage

Windows installer (32 bits)

Windows installer (64 bits)

Source code

Online documentation

Offline documentation

What’s new in this version?

In a nutshell…

  • 64 bits support.
  • Windows Vista and 7 support.
  • Memory dumping support.
  • Wait chain support.
  • New tool: SelectMyParent (based on the tool by Didier Stevens).
  • More code examples.
  • Supports detecting the current processor architecture and Windows version.
  • Crash logger works with SQLite databases in addition to the old DBM format. It also has a smaller memory footprint now.
  • Win32 API wrappers were refactored and improved. Many new definitions and API calls were added, up to Windows 7.
  • Many bugfixes as usual… 🙂 also several improvements to make the code more robust.

Here’s the full changelog.

Create a free website or blog at WordPress.com.

Design a site like this with WordPress.com
Get started