{"id":727,"date":"2014-02-17T23:15:19","date_gmt":"2014-02-17T23:15:19","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=727"},"modified":"2014-02-17T23:17:01","modified_gmt":"2014-02-17T23:17:01","slug":"python-and-immunity-debugger","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2014\/02\/python-and-immunity-debugger\/","title":{"rendered":"Python and Immunity Debugger"},"content":{"rendered":"<p>Howdy all!<\/p>\n<p>Been a great few weeks. Lots of ideas flowing and lots more malware to work on. I got it down to a science now. What I&#8217;ve been digging into lately is taking advantage of the Python shell inside immunity debugger. The library is feature rich and combines the capabilities of Immunity with the awesomeness of python. <\/p>\n<p>For example, let&#8217;s set some breakpoints on the commonly used API&#8217;s that malware always seems to abuse. <\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #DD4422\">&quot;&quot;&quot;mybp&quot;&quot;&quot;<\/span>\r\n\r\nDESC<span style=\"color: #333333\">=<\/span><span style=\"background-color: #fff0f0\">&quot;&quot;&quot;Sets Joe&#39;s comprehensive malware breakpoints&quot;&quot;&quot;<\/span>\r\n\r\n<span style=\"color: #888888\"># -*- coding: utf-8 -*-<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">getopt<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">immutils<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">getopt<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold\">immlib<\/span> <span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #333333\">*<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold\">def<\/span> <span style=\"color: #0066BB; font-weight: bold\">usage<\/span>():\r\n\timm<span style=\"color: #333333\">.<\/span>log(<span style=\"background-color: #fff0f0\">&quot;!mybp, no args, just !mybp&quot;<\/span>)\r\n\t\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold\">def<\/span> <span style=\"color: #0066BB; font-weight: bold\">main<\/span>(args):\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> args:\r\n\t\t\tusage()\r\n\t\t<span style=\"color: #008800; font-weight: bold\">else<\/span>:\r\n\t\t\timm <span style=\"color: #333333\">=<\/span> Debugger()\r\n\t\t\t\r\n\t\t\t<span style=\"color: #DD4422\">&quot;&quot;&quot;logs and packers&quot;&quot;&quot;<\/span>\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.CreateFileA&quot;<\/span>)\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.CreateFileW&quot;<\/span>)\r\n\t\t\t<span style=\"color: #DD4422\">&quot;&quot;&quot;packers&quot;&quot;&quot;<\/span>\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.MapViewOfFile&quot;<\/span>) \r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.VirtualProtect&quot;<\/span>)\r\n\t\t\t<span style=\"color: #DD4422\">&quot;&quot;&quot;process stop&quot;&quot;&quot;<\/span>\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.ExitProcess&quot;<\/span>)\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.TerminateProcess&quot;<\/span>)\r\n\t\t\t<span style=\"color: #DD4422\">&quot;&quot;&quot;process and thread creation&quot;&quot;&quot;<\/span>\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.CreateThread&quot;<\/span>)\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.CreateProcessA&quot;<\/span>)\r\n\t\t\timm<span style=\"color: #333333\">.<\/span>setBreakpointOnName(<span style=\"background-color: #fff0f0\">&quot;kernel32.CreateProcessW&quot;<\/span>) \r\n\t\t\t\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"background-color: #fff0f0\">&quot;Set breakpoints for file creation, process creation, process exiting and thread creation.&quot;<\/span>\r\n<\/pre>\n<\/div>\n<p>Compile with idle and place it in the C:\\Program Files (x86)\\Immunity Inc\\Immunity Debugger\\PyCommands\\ folder, then execute it with an exclamation point !cmd<br \/>\nIn the case of my little script, it will set the appropriate breakpoints and let me know in the grey title window when its done:<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/screeny.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/screeny-300x240.png\" alt=\"screeny\" width=\"300\" height=\"240\" class=\"alignnone size-medium wp-image-728\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/screeny-300x240.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/screeny-1024x819.png 1024w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/screeny.png 1280w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Just searching through the &#8216;immlib.py&#8217; file there&#8217;s a hundred other functions to make use of. There&#8217;s also a python shell within IDA Pro I use from time to time with a number of libraries and functions to take advantage of. Not only that there&#8217;s still <a href=\"http:\/\/libdasm.googlecode.com\/svn\/trunk\/pydasm\">pydasm<\/a> which is awesome. I like to make use of pydasm for on the fly stuff like decoding shellcode:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">sys<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">os<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">pydasm<\/span>\r\n<span style=\"color: #888888\"># Open our specimen <\/span>\r\nf <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">open<\/span>(<span style=\"background-color: #fff0f0\">&quot;c:<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0\">\\\\<\/span><span style=\"background-color: #fff0f0\">shellcoded_pdf.pdf&quot;<\/span>,<span style=\"background-color: #fff0f0\">&quot;rb&quot;<\/span>)\r\n<span style=\"color: #888888\"># 0x97 is the start of the ascii encoded shellcode<\/span>\r\nb <span style=\"color: #333333\">=<\/span> f<span style=\"color: #333333\">.<\/span>read(<span style=\"color: #0000DD; font-weight: bold\">2<\/span>)\r\n\r\nbuff <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;&quot;<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">while<\/span> b <span style=\"color: #333333\">!=<\/span> <span style=\"background-color: #fff0f0\">&#39;&#39;<\/span>:\r\n\t<span style=\"color: #008800; font-weight: bold\">try<\/span>:\r\n\t\tbuff <span style=\"color: #333333\">=<\/span> buff<span style=\"color: #333333\">+<\/span><span style=\"color: #007020\">chr<\/span>(<span style=\"color: #007020\">int<\/span>(b,<span style=\"color: #0000DD; font-weight: bold\">16<\/span>))\r\n\t\tb <span style=\"color: #333333\">=<\/span> f<span style=\"color: #333333\">.<\/span>read(<span style=\"color: #0000DD; font-weight: bold\">2<\/span>)\r\n\t<span style=\"color: #008800; font-weight: bold\">except<\/span> <span style=\"color: #FF0000; font-weight: bold\">ValueError<\/span>:\r\n\t\t<span style=\"color: #008800; font-weight: bold\">break<\/span>\r\n\t\toffset <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">while<\/span> offset <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #007020\">len<\/span>(buff):\r\n\t\t\ti <span style=\"color: #333333\">=<\/span> pydasm<span style=\"color: #333333\">.<\/span>get_instruction(buff[offset:],pydasm<span style=\"color: #333333\">.<\/span>MODE_32)\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">print<\/span> offset , <span style=\"background-color: #fff0f0\">&#39; &#39;<\/span>,pydasm<span style=\"color: #333333\">.<\/span>get_instruction_string(i,pydasm<span style=\"color: #333333\">.<\/span>FORMAT_INTEL, offset)\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">not<\/span> i:\r\n\t\t\t\t<span style=\"color: #008800; font-weight: bold\">break<\/span>\r\n\t\t\t\toffset <span style=\"color: #333333\">+=<\/span>  i<span style=\"color: #333333\">.<\/span>length\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> offset <span style=\"color: #333333\">==<\/span> <span style=\"color: #0000DD; font-weight: bold\">17<\/span>:\r\n\t\t\t\toffset <span style=\"color: #333333\">+=<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span>\r\n<\/pre>\n<\/div>\n<p>I guess python and reverse engineering go together like peanut butter and jelly. Fine by me.<br \/>\nHappy hacking!<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/DJXUw.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/DJXUw.png\" alt=\"DJXUw\" width=\"598\" height=\"266\" class=\"alignnone size-full wp-image-729\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/DJXUw.png 598w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/02\/DJXUw-300x133.png 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Howdy all! Been a great few weeks. Lots of ideas flowing and lots more malware to work on. I got it down to a science now. What I&#8217;ve been digging into lately is taking advantage of the Python shell inside immunity debugger. The library is feature rich and combines the capabilities of Immunity with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,7],"tags":[88],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/727"}],"collection":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/comments?post=727"}],"version-history":[{"count":2,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/727\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/727\/revisions\/731"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}