{"id":376,"date":"2011-07-13T18:45:22","date_gmt":"2011-07-13T20:45:22","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=376"},"modified":"2011-07-13T18:45:22","modified_gmt":"2011-07-13T20:45:22","slug":"isx64-gem","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=376","title":{"rendered":"isX64 Gem"},"content":{"rendered":"<p>I needed a multi-arch shellcode for both x86 and x64 in the same code. Suppose you want to attack a platform, which can either be x86 or x64 where you don&#8217;t know in advance which it is. The problem is which version you really need to use at runtime then, right?<\/p>\n<p>This is a tiny trick I&#8217;ve been using for a long while now which tells whether you run on x64 or not:<\/p>\n<pre lang=\"asm\">\r\nXOR EAX, EAX\r\nINC EAX ; = DB 0x40\r\nNOP\r\nJZ x64_code\r\nx86_code:\r\nbits 32\r\n.\r\n.\r\n.\r\nRET\r\nx64_code:\r\nbits 64\r\n.\r\n.\r\n<\/pre>\n<p>The idea is very simple, since x64 and x86 share most opcodes&#8217; values, there is a small in-similarity with the range of 0x40-0x50, in x86 it used for one byte INC <reg> and DEC <reg> opcodes. Since there&#8217;re 8 GPRs (General Purpose Register), and 2 opcodes, it spans over the whole range of  0x40-0x50.<br \/>\nNow when AMD64&#8217;s ISA (Instruction Set Architecture) was designed, they added another set of 8 GPRs, making it a total of whopping 16 GPRs. In a world where x86 ruled, you only needed 3 bits in the ModRM byte (some byte in the instruction that tells the processor how to read its operands) to access a specific register from 0 to 8. With the new ISA, an extra bit was required in order to be able to address all 16 registers. Therefore, a new prefix (called the REX prefix) was added to solve this problem with an extra bit (and there&#8217;s more to it, not relevant for now). The new prefix used the range of 0x40-0x50, thus eliminating old one byte INC\/DEC (no worries however, now compilers use the 2 bytes existent variation for these instructions).<\/p>\n<p>Back to our assembly code, it depends on the fact that in x86 the INC EAX, really increments EAX by one, and so it will become 1 if the code runs on x86. And when it&#8217;s run on x64, it becomes a prefix to the NOP instruction, which doesn&#8217;t do anything anyway. And hence, EAX stays zero. Just a final note for the inexperienced that in x64, operations on 32 bit registers are automatically promoted to 64 bit registers, so RAX is also 0.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I needed a multi-arch shellcode for both x86 and x64 in the same code. Suppose you want to attack a platform, which can either be x86 or x64 where you don&#8217;t know in advance which it is. The problem is which version you really need to use at runtime then, right? This is a tiny [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[5,17,18],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-64","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/376"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=376"}],"version-history":[{"count":3,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions"}],"predecessor-version":[{"id":379,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions\/379"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}