{"id":1513,"date":"2018-12-18T02:00:19","date_gmt":"2018-12-18T02:00:19","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=1513"},"modified":"2018-12-18T02:00:19","modified_gmt":"2018-12-18T02:00:19","slug":"metasploit-addition-two-electric-boogaloo","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2018\/12\/metasploit-addition-two-electric-boogaloo\/","title":{"rendered":"Metasploit Addition Two &#8211; Electric Boogaloo"},"content":{"rendered":"<p>What about Linux? Why all the love for Windows? What is this crap?<\/p>\n<p>The code for linux is a bit different in MSF. They use a modified ELF template that puts the shellcode directly into the end of the file which is built by nasm. Example:<\/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: #008800; font-style: italic;\">; build with:<\/span>\n<span style=\"color: #008800; font-style: italic;\">;   nasm elf_x86_template.s -f bin -o template_x86_linux.bin<\/span>\n\n<span style=\"color: #000080; font-weight: bold;\">BITS<\/span> <span style=\"color: #0000ff;\">32<\/span>\n\n<span style=\"color: #000080; font-weight: bold;\">org<\/span> <span style=\"color: #0000ff;\">0x08048000<\/span>\n\nehdr:                            <span style=\"color: #008800; font-style: italic;\">; Elf32_Ehdr<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">db<\/span>    <span style=\"color: #0000ff;\">0x7F<\/span>, <span style=\"color: #0000ff;\">\"ELF\"<\/span>, <span style=\"color: #0000ff;\">1<\/span>, <span style=\"color: #0000ff;\">1<\/span>, <span style=\"color: #0000ff;\">1<\/span>, <span style=\"color: #0000ff;\">0<\/span>  <span style=\"color: #008800; font-style: italic;\">;   e_ident<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">db<\/span>    <span style=\"color: #0000ff;\">0<\/span>, <span style=\"color: #0000ff;\">0<\/span>, <span style=\"color: #0000ff;\">0<\/span>, <span style=\"color: #0000ff;\">0<\/span>,  <span style=\"color: #0000ff;\">0<\/span>, <span style=\"color: #0000ff;\">0<\/span>, <span style=\"color: #0000ff;\">0<\/span>, <span style=\"color: #0000ff;\">0<\/span>  <span style=\"color: #008800; font-style: italic;\">;<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    <span style=\"color: #0000ff;\">2<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_type       = ET_EXEC for an executable<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    <span style=\"color: #0000ff;\">3<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_machine<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">1<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_version<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    _start                   <span style=\"color: #008800; font-style: italic;\">;   e_entry<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    phdr - <span style=\"color: #000080; font-weight: bold;\">$<\/span>                <span style=\"color: #008800; font-style: italic;\">;   e_phoff<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">0<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_shoff<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">0<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_flags<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    ehdrsize                 <span style=\"color: #008800; font-style: italic;\">;   e_ehsize<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    phdrsize                 <span style=\"color: #008800; font-style: italic;\">;   e_phentsize<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    <span style=\"color: #0000ff;\">1<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_phnum<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    <span style=\"color: #0000ff;\">0<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_shentsize<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    <span style=\"color: #0000ff;\">0<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_shnum<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dw<\/span>    <span style=\"color: #0000ff;\">0<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   e_shstrndx<\/span>\n\nehdrsize<span style=\"color: #000080; font-weight: bold;\"> equ<\/span>  <span style=\"color: #000080; font-weight: bold;\">$<\/span> - ehdr\n\nphdr:                            <span style=\"color: #008800; font-style: italic;\">; Elf32_Phdr<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">1<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   p_type       = PT_LOAD<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">0<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   p_offset<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #000080; font-weight: bold;\">$<\/span>                       <span style=\"color: #008800; font-style: italic;\">;   p_vaddr<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #000080; font-weight: bold;\">$<\/span>                       <span style=\"color: #008800; font-style: italic;\">;   p_paddr<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">0xDEADBEEF<\/span>               <span style=\"color: #008800; font-style: italic;\">;   p_filesz<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">0xDEADBEEF<\/span>               <span style=\"color: #008800; font-style: italic;\">;   p_memsz<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">7<\/span>                        <span style=\"color: #008800; font-style: italic;\">;   p_flags      = rwx<\/span>\n  <span style=\"color: #000080; font-weight: bold;\">dd<\/span>    <span style=\"color: #0000ff;\">0x1000<\/span>                   <span style=\"color: #008800; font-style: italic;\">;   p_align<\/span>\n\nphdrsize<span style=\"color: #000080; font-weight: bold;\"> equ<\/span>  <span style=\"color: #000080; font-weight: bold;\">$<\/span> - phdr\n\n<span style=\"color: #000080; font-weight: bold;\">global<\/span> _start\n\n_start:\n<span style=\"color: #008800; font-style: italic;\">; MSF puts the shellcode here<\/span>\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>We would have to generate a new elf template file each time with our date check called prior to shellcode execution. From there, we would have to call our system call functions. Modification of this code however isn&#8217;t going very well. Metasploit uses a core class for setting these elf files and modifying the entire thing just to fit this one feature is&#8230;hard to say the least. I had considered first writing my stuff in C, then assembly, but this can all be made simpler in Linux with syscalls. The following code piece does the same as we did with windows, except its much smaller \/ easier to implement. Before we show off the assemblers, let&#8217;s go over syscalls. Everything in Linux is done via syscalls &#8211; everything from opening files to resetting the computer. Think of them as like interrupts for DOS (god I am OLD). As per the Linux man pages: &#8220;The system call is the fundamental interface between an application and the Linux kernel.&#8221;. Any time a program has to do anything special like read a read or write a file, or fork a new process, a syscall is made. Each architecture on linux will have the same system call table. You can find the syscall table for each architecture on github or the net. I used <a href=\"http:\/\/blog.rchapman.org\/posts\/Linux_System_Call_Table_for_x86_64\/ target=\">this one<\/a> for x64 intel. For x86, I used the <a href=\"http:\/\/shell-storm.org\/shellcode\/files\/syscalls.html\" target=\"_blank\" rel=\"noopener\">table here<\/a>. The sys_time system call is used for obtaining the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unix_time\" target=\"_blank\" rel=\"noopener\">Unix epoch<\/a> which is the number of seconds elapsed since 1970. An example would be 1545094801 in decimal or 0x\u202d5C184691\u202c in hex. This result is stored in a register EAX on x86 or RAX on x64 intel. Our code calls his syscall and compares it against the time we have stored which is supposed to be one months from now or plus an additional 2592000 seconds. Finally we have an old assembly friend for crashing &#8211; EB FE &#8211; jump two bytes backward from the current position while also moving two spaces forward. An infinite loop in two instructions. And now without further ado, the code:<\/p>\n<!-- HTML generated using hilite.me -->\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;\">section<\/span> <span style=\"color: #996633;\">.text<\/span>\n<span style=\"color: #008800; font-weight: bold;\">global<\/span> <span style=\"color: #996633;\">_start<\/span>\n\n<span style=\"color: #997700; font-weight: bold;\">_start:<\/span>\n\n<span style=\"color: #888888;\">; use syscall for epoch, compare it 30 days in future, check if older, crash<\/span>\n<span style=\"color: #888888;\">;------------------------------------------------------------------------------<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">mov<\/span> <span style=\"color: #007020;\">al<\/span>,<span style=\"color: #005588; font-weight: bold;\">0x0D<\/span> <span style=\"color: #888888;\">;sys_time is 13 vvvv current epoch plus one month<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">int<\/span> <span style=\"color: #005588; font-weight: bold;\">0x80<\/span> <span style=\"color: #888888;\">; only x64 uses syscall instruction, use int 0x80 instead<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">mov<\/span> <span style=\"color: #007020;\">ebx<\/span>, <span style=\"color: #005588; font-weight: bold;\">0x58D15D5F<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">cmp<\/span> <span style=\"color: #007020;\">eax<\/span>, <span style=\"color: #007020;\">ebx<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">jge<\/span> <span style=\"color: #996633;\">rockit<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">mov<\/span> <span style=\"color: #007020;\">al<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #888888;\">;exit<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">int<\/span> <span style=\"color: #005588; font-weight: bold;\">0x80<\/span>\n<span style=\"color: #997700; font-weight: bold;\">rockit:<\/span>\n<span style=\"color: #008800; font-weight: bold;\">db<\/span> <span style=\"color: #005588; font-weight: bold;\">0xEB<\/span>\n<span style=\"color: #008800; font-weight: bold;\">db<\/span> <span style=\"color: #005588; font-weight: bold;\">0xFE<\/span>\n<\/pre>\n<\/div>\n<p>All in all, I have the thing down to 18 bytes with no null bytes.<\/p>\n<!-- HTML generated using hilite.me -->\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: #333399; font-weight: bold;\">char<\/span> <span style=\"color: #333333;\">*<\/span>scode <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\xb0\\x0d\\xcd\\x80\\xbb\\x5f\\x5d\\xd1\\x58\\x39\\xd8\\x7d\\x04\\xb0\\x01\\xcd\\x80\\xeb\\xfe<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>;\n<\/pre>\n<\/div>\n<p>Here I have the same thing except with x64 code. Not much is different other than the use of the mnemonic &#8216;syscall&#8217; instead of &#8216;int 0x80&#8217; and the number of the syscall.<\/p>\n<!-- HTML generated using hilite.me -->\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;\">section<\/span> <span style=\"color: #996633;\">.text<\/span>\n<span style=\"color: #008800; font-weight: bold;\">global<\/span> <span style=\"color: #996633;\">_start<\/span>\n\n<span style=\"color: #997700; font-weight: bold;\">_start:<\/span>\n\n<span style=\"color: #888888;\">; use syscall for epoch, compare it 30 days in future, check if older, crash<\/span>\n<span style=\"color: #888888;\">;------------------------------------------------------------------------------<\/span>\n\n<span style=\"color: #0066bb; font-weight: bold;\">mov<\/span> <span style=\"color: #007020;\">al<\/span>,<span style=\"color: #005588; font-weight: bold;\">0xc9<\/span> <span style=\"color: #888888;\">;sys_time 201 vvvv current epoch plus one month<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">syscall<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">mov<\/span> <span style=\"color: #007020;\">ebx<\/span>, <span style=\"color: #005588; font-weight: bold;\">0x58D15D5F<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">cmp<\/span> <span style=\"color: #007020;\">eax<\/span>, <span style=\"color: #007020;\">ebx<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">jge<\/span> <span style=\"color: #996633;\">rockit<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">mov<\/span> <span style=\"color: #007020;\">al<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #888888;\">;exit<\/span>\n<span style=\"color: #0066bb; font-weight: bold;\">syscall<\/span>\n<span style=\"color: #997700; font-weight: bold;\">rockit:<\/span>\n<span style=\"color: #008800; font-weight: bold;\">db<\/span> <span style=\"color: #005588; font-weight: bold;\">0xEB<\/span>\n<span style=\"color: #008800; font-weight: bold;\">db<\/span> <span style=\"color: #005588; font-weight: bold;\">0xFE<\/span>\n<\/pre>\n<\/div>\n<p>And here we have our shellcode again:<\/p>\n<!-- HTML generated using hilite.me -->\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: #333399; font-weight: bold;\">char<\/span> scoder<span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\xb0\\x0d\\xcd\\x80\\xbb\\x5f\\x5d\\xd1\\x58\\x39\\xd8\\x7d\\x04\\xb0\\x01\\xcd\\x80\\xeb\\xfe<\/span><span style=\"background-color: #fff0f0;\">\"<\/span>;\n<\/pre>\n<\/div>\n<p>Someone once asked me how the heck do you code assembly without the nulls and I said &#8220;use the smaller registers so there is no padding&#8221;. I could spend a whole blog post on that alone.<\/p>\n<p>\u00a0<\/p>\n<p>So how the hell do we take this and implement it in metasploit? We could always modify the template files by adding our check to the bottom just below &#8216;start&#8217;, and that would work for all files, but it would have to be done for EVERY SINGLE TEMPLATE, so its becomes a pain in the ass. Then someone (HD Moore) told me about the &#8216;<span class=\"pl-s\">PrependEncoder<\/span>&#8216; option present in Metasploit when writing exploits.<\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/PIC12.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1581\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/PIC12.png\" alt=\"\" width=\"694\" height=\"542\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/PIC12.png 694w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/PIC12-300x234.png 300w\" sizes=\"(max-width: 694px) 100vw, 694px\" \/><\/a><\/p>\n<p>Basically it allows us to prepend shellcode to the payload that is executed first. In this case, we are adjusting the size of the stack, but it doesn&#8217;t have to be that. Why not add our date check there instead? &#8220;\\xb0\\x0d\\xcd\\x80\\xbb\\x5f\\x5d\\xd1\\x58\\x39\\xd8\\x7d\\x04\\xb0\\x01\\xcd\\x80\\xeb\\xfe&#8221;; would fit great right there.<\/p>\n<p>This is basically where I am at with this. I need to figure out how to programmatically add my code via command line argument to new exploits and payloads. And before you ask, I did think about how I would dynamically add the epoch in hex to the assembly in ruby:<\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1584\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic3.png\" alt=\"\" width=\"429\" height=\"204\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic3.png 429w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic3-300x143.png 300w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<p>My commit is presently in limbo while I try and figure out how to make use of the PrependEncoder option programmatically so until then, this technique is in my posession and not the world&#8217;s.<\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-1585\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic4-1024x519.png\" alt=\"\" width=\"640\" height=\"324\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic4-1024x519.png 1024w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic4-300x152.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic4-768x389.png 768w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/pic4.png 1191w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<p>Until next time, happy hacking!<\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/1540310387862.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1586\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/1540310387862.png\" alt=\"\" width=\"702\" height=\"666\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/1540310387862.png 702w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2018\/12\/1540310387862-300x285.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What about Linux? Why all the love for Windows? What is this crap? The code for linux is a bit different in MSF. They use a modified ELF template that puts the shellcode directly into the end of the file which is built by nasm. Example: ; build with: ; nasm elf_x86_template.s -f bin -o [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,6],"tags":[118,117],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1513"}],"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=1513"}],"version-history":[{"count":4,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1513\/revisions"}],"predecessor-version":[{"id":1587,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1513\/revisions\/1587"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=1513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=1513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=1513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}