{"id":101,"date":"2009-09-25T11:15:53","date_gmt":"2009-09-25T13:15:53","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=101"},"modified":"2009-09-25T11:20:57","modified_gmt":"2009-09-25T13:20:57","slug":"branching-to-absolute-addresses","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=101","title":{"rendered":"Branching to Absolute Addresses"},"content":{"rendered":"<p>The x86-64 architecture is very annoying sometimes. Especially when you want to hook functions or do some magical assembly tricks :)<\/p>\n<p>Suppose you want to hook a function, you will have to change its first instruction to a jump instruction to your code, so the next time the function is being called, the control will be transferred to your stub. Everybody knows this technique, basically you will need 5 bytes, for the opcode E9 (JMP relative) and another DWORD for the relative offset itself. Then you are able to jump backward or forward by 2GB. Sometimes, it&#8217;s not enough, just because you have <em>some<\/em> limitations.<br \/>\nNow the problem with the processor is that there&#8217;s no instruction that can branch to an absolute address which is given straight to the instruction as an immediate operand. Try to think of a MOV EAX, 0x12345678,<br \/>\nbut instead, JMPABS 0x12345678. What a shame, really. But no worries, of course it&#8217;s possible to work it out, the cost is a few more bytes. Sometimes you are short on bytes. However, just to open your appetite you can sometimes even hook a 1 byte function that only RETs in an efficient way. That&#8217;s worth another blog post sometime.<\/p>\n<p>Anyway, what you can do is changing the code a bit and do something like this:<\/p>\n<pre lang=\"asm\">\r\nMOV EAX, <Absolute Address>\r\nJMP EAX ; <--- Suddenly the processor supports absolute addresses, ah ha!\r\n<\/pre>\n<p>But now, this code takes 7 bytes and it also screws up one of the registers. There are two possible fixes, either store and then reload EAX at the callee site (BAHH!!) or change the code again, which is trivial:<\/p>\n<pre lang=\"asm\">\r\nPUSH <Absolute Address>\r\nRET\r\n<\/pre>\n<p>This is very nice one, although, technically, I am not sure whether it's slower than a simple JMP. The algorithm for the RET instruction is quite impressive (to branch predict the caller(\/return) address...), mind you. Now the code takes only 6 bytes, plus, you can access the whole address space.<\/p>\n<p>Let's move to another similar issue, suppose you want to CALL an absolute address, what you gonna do now?<\/p>\n<pre lang=\"asm\">\r\nPUSH EBX\r\nCALL $ + 5\r\nHERE:\r\nPOP EBX\r\nLEA EBX, [EBX + NEXT-HERE]\r\nPUSH EBP\r\nMOV EBP, ESP\r\nXCHG EBX, [EBP + 4] ; 1337\r\nPOP EBP\r\nPUSH <Absolute Address>\r\nRET\r\nNEXT:\r\n<\/pre>\n<p>I will leave it to you to figure it out, hint - it doesn't screw any register.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The x86-64 architecture is very annoying sometimes. Especially when you want to hook functions or do some magical assembly tricks :) Suppose you want to hook a function, you will have to change its first instruction to a jump instruction to your code, so the next time the function is being called, the control will [&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],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-1D","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/101"}],"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=101"}],"version-history":[{"count":5,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}