{"id":3245,"date":"2021-06-29T12:56:29","date_gmt":"2021-06-29T10:56:29","guid":{"rendered":"https:\/\/andreas-klingler.de\/infosec\/?p=3245"},"modified":"2021-06-29T12:56:29","modified_gmt":"2021-06-29T10:56:29","slug":"modifying-pe-files","status":"publish","type":"post","link":"https:\/\/infosec.andreas-klingler.de\/?p=3245","title":{"rendered":"Modifying <span class=\"caps\">PE<\/span>&nbsp;files"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><span class=\"caps\">PE<\/span> Portable Exe\u00adcu\u00adtung or <span class=\"caps\">DLL<\/span> Dynam\u00adic Link\u00ading Libraries can be edit\u00aded to remove or add capa\u00adbil\u00adi\u00adties or own&nbsp;code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Read and modify a <span class=\"caps\">PE<\/span>&nbsp;file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The fol\u00adlow\u00ading Python3 script reads a file, prints out a head\u00ader, mod\u00adi\u00adfied it to remove <span class=\"caps\">ASLR<\/span> and write a new file with\u00adout this&nbsp;flag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">f = pefile.PE('filename.exe')\nprint(hex(f.OPTIONAL_HEADER.DllCharacteristics)) \/\/ print as hex to work with masks\nf.OPTIONAL_HEADER.DllCharacteristics = f.OPTIONAL_HEADER.DllCharacteristics ^ 0x0040 \/\/ xor with 0x0040 which is the value for DYNAMIC_BASE (=&gt; ASLR) to enable or disable it.\nf.write('filename.exe.new') \/\/ write the modified file.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Ressources<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/debug\/pe-format\">Doc\u00adu\u00admen\u00adta\u00adtion of the <span class=\"caps\">PE<\/span> format.<\/a><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"caps\">PE<\/span> Portable Exe\u00adcu\u00adtung or <span class=\"caps\">DLL<\/span> Dynam\u00adic Link\u00ading Libraries can be edit\u00aded to remove or add capa\u00adbil\u00adi\u00adties or own&nbsp;code. Read and mod\u00adi\u00adfy a <span class=\"caps\">PE<\/span>&nbsp;file The fol\u00adlow\u00ading Python3 script reads a file, prints out a head\u00ader, mod\u00adi\u00adfied it to remove <span class=\"caps\">ASLR<\/span> and write a new file with\u00adout this&nbsp;flag. f = pefile.<span class=\"caps\">PE<\/span>(\u2018filename.exe\u2019) print(hex(f.<span class=\"caps\">OPTIONAL_HEADER<\/span>.DllCharacteristics)) \/\/ print as hex&nbsp;to&nbsp;[\u2026]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wp_typography_post_enhancements_disabled":false,"footnotes":""},"categories":[355],"tags":[408,407,406,409],"class_list":["post-3245","post","type-post","status-publish","format-standard","hentry","category-reverse-engineering","tag-dll","tag-exe","tag-pe","tag-reverse-engineering"],"_links":{"self":[{"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=\/wp\/v2\/posts\/3245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3245"}],"version-history":[{"count":1,"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=\/wp\/v2\/posts\/3245\/revisions"}],"predecessor-version":[{"id":3246,"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=\/wp\/v2\/posts\/3245\/revisions\/3246"}],"wp:attachment":[{"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infosec.andreas-klingler.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}