Popular recipes by Simon Harrison http://code.activestate.com/recipes/users/4191738/2016-06-07T13:11:50-07:00ActiveState Code RecipesCreate a zip bomb, for testing (Python) 2016-06-07T13:11:50-07:00Simon Harrisonhttp://code.activestate.com/recipes/users/4191738/http://code.activestate.com/recipes/580676-create-a-zip-bomb-for-testing/ <p style="color: grey"> Python recipe 580676 by <a href="/recipes/users/4191738/">Simon Harrison</a> . </p> <p>This creates a zip bomb with a programmable recursion depth. It's useful when you want to create a very deep zip archive for testing decompression software, virus scanners and so on. Recursion depth is limited by Python itself, so very high values are probably not going to work. The algorithm could be re-structured to avoid recursion, but I've never needed a nesting depth of more than 900!</p> <p>Each zip file will get the name stack&lt;n&gt;.zip where &lt;n&gt; is the number of zip files inside each layer. The data file at the core is called needle.txt.</p> How to execute x86 64-bit assembly code directly from Python on Linux (requires Nasm) (Python) 2015-03-24T18:23:19-07:00Simon Harrisonhttp://code.activestate.com/recipes/users/4191738/http://code.activestate.com/recipes/579037-how-to-execute-x86-64-bit-assembly-code-directly-f/ <p style="color: grey"> Python recipe 579037 by <a href="/recipes/users/4191738/">Simon Harrison</a> (<a href="/recipes/tags/nasm/">nasm</a>). Revision 4. </p> <p>This particular example pertains to running 64-bit assembly code under 64-bit Python, however if you are fortunate enough to be running on a 32-bit Linux platform you may get away with doing a lot less. Since the introduction of DEP you can no longer just shove some code in a buffer and execute it. You must first allow execute permission. Further, you cannot change memory protection settings for anything other than an entire page, so I've padded the memory allocated to include a page before and a page after, so I can seek back to the start of page for the start of my assembler, then change memory protection from there secure in the knowledge that I won't change protection for anything beyond the end of my allocated region. I could probably have used valloc() instead.</p> <p>After that it's simply a matter of changing protection. There is some doubt over whether you can leave this memory read/writable after you set the execute bit, I haven't played with that.</p> <p>Finally, it's important to reset the memory protection before the memory gets freed/reused by Python because Python has no knowledge of the hackery you've just been up to.</p> <p>For a Windows version, use VirtualProtect() to achieve similar results. I could have conditionally added this and made the code portable, however I rarely do any assembler on Windows, so it's left as an exercise for the reader.</p> Drop a minimal, valid Windows executable file to disk, for testing (Python) 2015-03-03T10:47:32-08:00Simon Harrisonhttp://code.activestate.com/recipes/users/4191738/http://code.activestate.com/recipes/579029-drop-a-minimal-valid-windows-executable-file-to-di/ <p style="color: grey"> Python recipe 579029 by <a href="/recipes/users/4191738/">Simon Harrison</a> (<a href="/recipes/tags/executable/">executable</a>, <a href="/recipes/tags/pe/">pe</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 3. </p> <p>Sometimes I need to create a valid windows executable file from a Python script for the sake of running a test.</p>