├── LICENSE ├── README.md ├── hello_world.b ├── hello_world.m ├── m2elf.pl └── spawnshell.m /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | m2elf 2 | ===== 3 | 4 | Converts Machine Code to x86 (32-bit) Linux executable (auto-wrapping with ELF headers). This script comes included in Remnux v6 5 | 6 | Command Usage 7 | ===== 8 | Options:
9 | --in (input file)
10 | --out (output file)
11 | --binary (if the input is already binary form)
12 | --mem (bytes)
13 | --entry (This changes the entry point, it adds to the offset whichever decimal number is provided)
14 | --writeover - changes the r-x of the .text to rwx; now you can have self modifying codes 15 | 16 | m2elf --in source_file.m --out executable_file --mem 100
17 | 18 | Example Source Files 19 | ===== 20 | * hello.m - written in machine code, prints hello world. Use --mem 16 21 | * hello.b - same as above, but written in pure 1's and 0's :) 22 | * Spawnshell.m - shellcode from: http://phrack.org/issues/49/14.html#article, the difference with this .m file is that each instruction is commented. To get this to actually run, the --writeover argument must be used. 23 | 24 | Source File Syntax 25 | ===== 26 | The source file can include ascii hex and binary byte by byte. Spacing between hex bytes not a requirment, but could reduce bugs (4 hex bytes could look like binary to m2elf if the hex is only 1's and 0's). In order for a binary byte to be interpreted as such, it needs to be a series of 8 1's and 0's preceded by an underscore, i.e. _01011000 27 | 28 | Valid comments include #, //, ', and -- 29 | -------------------------------------------------------------------------------- /hello_world.b: -------------------------------------------------------------------------------- 1 | 2 | _10111000 _00100001 _00001010 _00000000 _00000000 3 | _10100011 _00001100 _00010000 _00000000 _00000110 4 | _10111000 _01101111 _01110010 _01101100 _01100100 5 | _10100011 _00001000 _00010000 _00000000 _00000110 6 | _10111000 _01101111 _00101100 _00100000 _01010111 7 | _10100011 _00000100 _00010000 _00000000 _00000110 8 | _10111000 _01001000 _01100101 _01101100 _01101100 9 | _10100011 _00000000 _00010000 _00000000 _00000110 10 | _10111001 _00000000 _00010000 _00000000 _00000110 11 | _10111010 _00010000 _00000000 _00000000 _00000000 12 | _10111011 _00000001 _00000000 _00000000 _00000000 13 | _10111000 _00000100 _00000000 _00000000 _00000000 14 | _11001101 _10000000 15 | 16 | _10111000 _00000001 _00000000 _00000000 _00000000 17 | _11001101 _10000000 18 | -------------------------------------------------------------------------------- /hello_world.m: -------------------------------------------------------------------------------- 1 | b8 21 0a 00 00 #moving "!\n" into eax 2 | a3 0c 10 00 06 #moving eax into first memory location 3 | b8 6f 72 6c 64 #moving "orld" into eax 4 | a3 08 10 00 06 #moving eax into next memory location 5 | b8 6f 2c 20 57 #moving "o, W" into eax 6 | a3 04 10 00 06 #moving eax into next memory location 7 | b8 48 65 6c 6c #moving "Hell" into eax 8 | a3 00 10 00 06 #moving eax into next memory location 9 | 10 | b9 00 10 00 06 #moving pointer to start of memory location into ecx 11 | ba 10 00 00 00 #moving string size into edx 12 | bb 01 00 00 00 #moving "stdout" number to ebx 13 | b8 04 00 00 00 #moving "print out" syscall number to eax 14 | cd 80 #calling the linux kernel to execute our print to stdout 15 | 16 | b8 01 00 00 00 #moving "sys_exit" call number to eax 17 | cd 80 #executing it via linux sys_call 18 | -------------------------------------------------------------------------------- /m2elf.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | #ELF construction based on reversing done by Ange Albertini from corkami.com (great infographic), 3 | #http://man7.org/linux/man-pages/man5/elf.5.html, and just assembling stuff with nasm and comparing results. 4 | #These files generated may not be 'proper,' but they seem execute pretty well. 5 | use warnings; 6 | use strict; 7 | use Getopt::Long; 8 | use Term::ANSIColor; 9 | 10 | my ($in, $binary, $hex, $code); 11 | my $out = "out"; 12 | my $temp_data; 13 | my $memory_size = 0; 14 | my $entry = 0; 15 | my $writeover = 0; 16 | my $help = 0; 17 | my $interactive = 0; 18 | my $flavor = "intel"; 19 | my $colorize = 0; 20 | 21 | sub colorize($); 22 | 23 | GetOptions('in=s' => \$in, 24 | 'out=s' => \$out, 25 | 'binary' => \$binary, 26 | 'mem=s' => \$memory_size, 27 | 'entry=s' => \$entry, 28 | 'writeover' => \$writeover, 29 | 'interactive' => \$interactive, 30 | 'flavor=s' => \$flavor, 31 | 'colorize' => \$colorize, 32 | 'help' => \$help); 33 | 34 | if ($help eq 1){ 35 | help(); 36 | } 37 | 38 | if ($interactive eq 1){ 39 | my $result; 40 | qx{which objdump 2>&1}; 41 | if ($? > 0) { 42 | print "This system needs 'objdump' installed in order to run 'interactive' mode\n"; 43 | } else { 44 | $code = ''; 45 | $out = 'tmp'; 46 | while (1) { 47 | print "m2elf > "; 48 | $code = ; 49 | next if ($code =~ /^$/); 50 | last if ($code =~ /(exit|q(?:uit)*)/i); 51 | convert(); 52 | payload(); 53 | $result = `objdump -M $flavor -d tmp`; 54 | $result =~ s/^.+?<>:\n(.+)\s\.\.\..+$/$1/s; 55 | if ($colorize) { 56 | print colorize($result), "\n"; 57 | } else { 58 | print "$result\n"; 59 | } 60 | } 61 | system('rm tmp') 62 | } 63 | } 64 | 65 | #--------------------------Code/Strings/Sections------------------------------------ 66 | if ($in) { 67 | 68 | $/ = undef; 69 | open IN, "$in" or die "Couldn't open $in, $!\n"; 70 | $code = ; 71 | $/ = "\n"; 72 | 73 | if ($binary) { 74 | } else { 75 | convert(); 76 | } 77 | } else { 78 | $code = "\x90\x90\x90\x90\x90\x90\xb8\x01\x00\x00\x00\xcd\x80"; 79 | } 80 | 81 | sub payload { 82 | 83 | #Fix padding of code; pad code to be divisible by 16 bytes 84 | $code .= "\x00" x (16 - (length($code) % 16)) if ((length($code) % 16) != 0); 85 | 86 | #Section Names 87 | my $shstrtab_name = "\x00\x2e\x73\x68\x73\x74\x72\x74\x61\x62\x00"; #null record followed by ".shstrtab" 88 | my $text_name = "\x2e\x74\x65\x78\x74\x00"; #".text" 89 | my $bss_name = ''; #Does not exist unless --mem is declared with a value 90 | my $section_names = ''; #just an init of our section names header 91 | if ($memory_size > 0) { #If allocating memory 92 | $bss_name = "\x2e\x62\x73\x73\x00"; #".bss" 93 | $section_names = $shstrtab_name . $text_name . $bss_name . ("\x00" x 10); #section names is null+.shstrtab+.text+.bss+10bytes_padding 94 | } else { #Otherwise, build without .bss 95 | $section_names = $shstrtab_name . $text_name . ("\x00" x 15); #So null+.shstrtab+.text and pad 15 bytes 96 | } 97 | 98 | 99 | #Section Header Table 100 | #Null Section 101 | my $null = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; 102 | 103 | #Text Section 104 | #name at offset 0b, type 01, flags 0x06 (allocated & executable), start at addr 0x8000060 / offset 0x60 105 | my $text = "\x0b\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x60\x00\x00\x08\x60\x00\x00\x00"; 106 | if ($memory_size > 0) { #if we allocated memory, change the start at addr to 0x8000080 / offset 0x80 107 | $text = "\x0b\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x80\x00\x00\x08\x80\x00\x00\x00"; 108 | } 109 | my $offset = printhex_32(length($code)); #get length of padded code and represent as intel-endian 4-byte structure 110 | #concatenate headers so far, possible different offset due to memory alloc, structured size, and pretty much null-pad out 111 | $text .= $offset . "\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00"; 112 | 113 | #Section Names section, but I'll just call this Shitter Tab. 114 | my $shrtrtab = ''; #init 115 | my $bss_header = ''; #init 116 | my $sections = ''; #init 117 | if ($memory_size > 0) { #if there's memory, 118 | #BSS Header now... 119 | $bss_header = "\x11\x00\x00\x00" . "\x08\x00\x00\x00" . "\x03\x00\x00\x00"; #name header starts at 0x11, type 8, flag 3 120 | $offset = printhex_32(100663296); #0x06000000 121 | $bss_header .= $offset; #add the start addr 122 | $offset = printhex_32(4096); #offset 0x1000 123 | $bss_header .= $offset; #add the offset 124 | $offset = printhex_32($memory_size); #add intel-endian bytes for size of bss section 125 | #Finally, null pad this header out 126 | $bss_header .= $offset . "\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00"; 127 | 128 | #Now to proceed with the Shitter Tab 129 | #name offset starts at 0x01, it's type 3 (string table), no flags (null), no addr 130 | $shrtrtab = "\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; 131 | $offset = printhex_32(128 + length($code)); #intel-endian offset + 0x80 132 | #size is always 0x19 bytes, then null-pad out 133 | $shrtrtab .= $offset . "\x19\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"; 134 | 135 | #Glue the .bss version of section header table together 136 | $sections = $null . $text . $bss_header . $shrtrtab; 137 | } else { 138 | #Without .bss, the shitter table has an offset of 0xa0 139 | $shrtrtab = "\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa0\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"; 140 | #Glue the non .bss version of the section header table together 141 | $sections = $null . $text . $shrtrtab; 142 | } 143 | 144 | 145 | #--------------------------Program Header Table setup------------------------------ 146 | #This is all for the .text segment 147 | my $p_type = "\x01\x00\x00\x00"; #The segment should be loaded into memory 148 | my $p_offset = "\x00\x00\x00\x00"; #Offset where it should be read 149 | my $p_addr = "\x00\x00\x00\x08"; #Virtual address where it should be loaded 150 | my $p_paddr = "\x00\x00\x00\x08"; #Physical address where it should be loaded 151 | my $p_flags = "\x05\x00\x00\x00"; #Readable and eXecutable (by default) 152 | 153 | #If we LOVE self modifying code, put your hands up! 154 | if ($writeover eq 1) { 155 | $p_flags = "\x06\x00\x00\x00"; 156 | } 157 | 158 | #Give a little extra mem for now, until I actually try and figure this out, but I don't care 159 | my $p_filesz = printhex_32(length($code) + 160); #Size on file 160 | my $p_memsz = $p_filesz; #Size in memory 161 | 162 | #Build the header up 163 | my $program_header_table = $p_type . $p_offset . $p_addr . $p_paddr . $p_filesz . $p_memsz . $p_flags . "\x00\x10\x00\x00"; 164 | 165 | #If we want memeory, the below code builds up the .bss segment 166 | if ($memory_size > 0) { 167 | #Similar headers as .text, but starts at 0x6000000, and flags are rwx by default on this one 168 | $program_header_table .= "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x06\x00\x10\x00\x06"; 169 | $program_header_table .= printhex_32($memory_size) . printhex_32($memory_size); 170 | $program_header_table .= "\x06\x00\x00\x00\x00\x10\x00\x00"; 171 | } 172 | 173 | #--------------------------ELF Header setup--------------------------------------- 174 | 175 | my $e_ident_EI_MAG = "\x7f\x45\x4c\x46"; #constant signature (ELF) 176 | my $e_ident_EI_CLASS_DATA = "\x01\x01"; #32 bits, Little-Endian 177 | my $e_ident_EI_VERSION = "\x01\x00\x00\x00"; #Always 1 178 | my $e_type = "\x02\x00"; #Executable 179 | my $e_machine = "\x03\x00"; #Intel 386 (and later) 180 | my $e_version = "\x01\x00\x00\x00"; #Always 1 181 | my $e_entry = "\x60\x00\x00\x08"; #Entry Point 182 | my $e_phoff = "\x40\x00\x00\x00"; #Program Headers' offset 183 | my $e_shoff = "\x00\x00\x00\x00"; #Section Header's offset, 0'd out for now, calculated later 184 | my $e_ehsize = "\x34\x00"; #ELF header's size 185 | my $e_phentsize = "\x20\x00"; #Size of a single Program Header 186 | my $e_phnum = "\x01\x00"; #Count of Program Headers 187 | my $e_shentsize = "\x28\x00"; #Size of a single Section Header (probably static) 188 | my $e_shnum = "\x03\x00"; #Count of Section Headers 189 | my $e_shstrndx = "\x02\x00"; #Index of the names' section in the table 190 | #A few values need changing if we allocate memory with .bss segment (offsets and stuff) 191 | if ($memory_size > 0) { 192 | $e_shnum = "\x04\x00"; 193 | $e_shstrndx = "\x03\x00"; 194 | $e_phnum = "\x02\x00"; 195 | $e_entry = "\x80\x00\x00\x08"; 196 | } 197 | 198 | #Change entry point if user wants this, it is not a specific address; it's a number of bytes to add to the current entry offset 199 | if ($entry > 0) { 200 | $entry += 134217824; 201 | $e_entry = printhex_32($entry); 202 | } 203 | 204 | #Calculate e_shoff size (Section Header offset) 205 | $e_shoff = length($code . $section_names) + 96; #section names + code + 96 bytes of fixed size ELF headers 206 | if ($memory_size > 0) { 207 | $e_shoff += 32; #32 bytes of extra header info if we have .bss 208 | } 209 | $e_shoff = printhex_32($e_shoff); #format this number to intel-endian 4-byte value 210 | 211 | #Build ELF Header 212 | my $ELF_header = $e_ident_EI_MAG . $e_ident_EI_CLASS_DATA . $e_ident_EI_VERSION . ("\x00" x 6) . $e_type . $e_machine . $e_version . 213 | $e_entry . $e_phoff . $e_shoff . ("\x00" x 4) . $e_ehsize . $e_phentsize . $e_phnum . $e_shentsize . $e_shnum . $e_shstrndx . ("\x00" x 12); 214 | 215 | #-------------------------combine everything-------------------------------------- 216 | my $output = $ELF_header . $program_header_table . $code . $section_names . $sections; 217 | 218 | #write all of this to our file 219 | open FILE, ">$out" or die "Couldn't open $out, $!\n"; 220 | print FILE $output; #send it out 221 | close FILE; 222 | chmod(0755, $out) or die "Couldn't change the permission to $out: $!"; 223 | 224 | } 225 | 226 | payload(); 227 | 228 | #This sub takes an integer and converts it into it's 32-bit intel-endian form 229 | sub printhex_32 { 230 | my $value = shift; #get the value passed to it 231 | my $return; #make a return variable 232 | $value = sprintf("%.8X\n", $value); #get an "ASCII HEX" version of the value 233 | if ($value =~ /(.)(.)(.)(.)(.)(.)(.)(.)/) { #parse out each character 234 | $return = pack("C*", map { $_ ? hex($_) :() } $7.$8) . pack("C*", map { $_ ? hex($_) :() } $5.$6) . 235 | pack("C*", map { $_ ? hex($_) :() } $3.$4) . pack("C*", map { $_ ? hex($_) :() } $1.$2); #unpack it 236 | } 237 | return $return; #return the hex data 238 | } 239 | 240 | sub convert { 241 | my $temp_code = ''; 242 | $code =~ s/(.*)(#|\/\/|'|\-\-).*/$1/g; #remove comments 243 | #Find 8-bit binary strings and convert to ascii-hex 244 | while ($code =~ /_([01]{8})[^01]/) { 245 | my $replacement = sprintf('%X', oct("0b$1")); 246 | if (length($replacement) == 1) { 247 | $replacement = "0" . $replacement; 248 | } 249 | $code =~ s/_[01]{8}([^01])/$replacement $1/; 250 | } 251 | $code =~ s/\s//g; #remove spaces 252 | 253 | #Has pure ascii-hex, convert to binary data 254 | while ($code =~ /(..)/) { #Get the matching hex into $1 255 | $temp_code .= pack("C*", map { $_ ? hex($_) :() } $1); 256 | $code =~ s/^..//; 257 | } 258 | $code = $temp_code; 259 | } 260 | 261 | sub colorize($) { 262 | my $code = shift || return undef; 263 | my($addr, $opcode, $inst, $operands) = $code =~ m/^[\s\t]*(\d+)\:[\s\t]+((?:[\da-f]+\s)+)[\s\t]+([a-z]+)\s*(.*)$/gi; 264 | my($green, $yellow, $blue, $red, $reset) = (color('green'), color('yellow'), color('blue'), color('red'), color('reset')); 265 | 266 | return undef unless (defined $addr); 267 | $code =~ s/$addr/$green$addr$reset/; 268 | $code =~ s/$opcode/$yellow$opcode$reset/; 269 | $code =~ s/$inst/$blue$inst$reset/; 270 | if (defined $operands) { 271 | $operands =~ s/([\[\]\(\)+\-*\$])/\\$1/g; 272 | $code =~ s/$operands/$red$operands$reset/; 273 | $code =~ s/\\//g; 274 | } 275 | 276 | return $code; 277 | } 278 | 279 | sub help { 280 | print "NAME\n"; 281 | print "\tm2elf - Machine Code to ELF wrapped executable binary\n\n"; 282 | print "SYNOPSIS\n"; 283 | print "\tm2elf.pl --in inputfile --out outputfile\n\n"; 284 | print "DESCRIPTION\n"; 285 | print "\tThis script takes ascii-hex, space delimited chunks of 8 1' and 0's, or a binary file as input and crafts an appropriate ELF header to make this machine code executable on Linux (32-bit).\n\n"; 286 | print "OPTIONS\n"; 287 | print "\t--in: specify your source file after this\n"; 288 | print "\t--out: specify the name you want your executable file to be\n"; 289 | print "\t--interactive: use interactive mode\n"; 290 | print "\t--binary: if your file is raw (binary file with unprintables; already machine code), then supply this option. This option is great for extractions from pcaps\n"; 291 | print "\t--mem: specify how many bytes of memory you want after this option, it will map starting at offset 0x06000000\n"; 292 | print "\t--entry: you can change the entry point. The default is 0x08000060. Whichever number you specify, will add that amount of bytes to the default offset. It will not shift the beginning of your code to that offset, however. For example, if you supply --entry 16, add 16 NOPs to the begginning of your original code and it will function as if you didn't add the --entry 16 and the NOPs\n"; 293 | print "\t--writeover: changes the r-x of the .text to rwx; now you can have self modifying codes\n"; 294 | print "\t--flavor: flavor of output, default is 'intel' syntax (man objdump for a list of syntax flavors)\n"; 295 | print "\t--colorize: colorize output\n\n"; 296 | print "EXAMPLES\n"; 297 | print "\tSOURCE:\n"; 298 | print "\tb8\t21 0a 00 00\t#moving '!\\n' into eax\n"; 299 | print "\ta3\t0c 10 00 06\t#moving eax into first memory location\n"; 300 | print "\tb8\t6f 72 6c 64\t#moving 'orld' into eax\n"; 301 | print "\ta3\t08 10 00 06\t#moving eax into next memory location\n"; 302 | print "\tb8\t6f 2c 20 57\t#moving 'o, W' into eax\n"; 303 | print "\ta3\t04 10 00 06\t#moving eax into next memory location\n"; 304 | print "\tb8\t48 65 6c 6c\t#moving 'Hell' into eax\n"; 305 | print "\ta3\t00 10 00 06\t#moving eax into next memory location\n"; 306 | print "\tb9\t00 10 00 06\t#moving pointer to start of memory location into ecx\n"; 307 | print "\tba\t10 00 00 00\t#moving string size into edx\n"; 308 | print "\tbb\t01 00 00 00\t#moving 'stdout' number to ebx\n"; 309 | print "\tb8\t04 00 00 00\t#moving 'print out' syscall number to eax\n"; 310 | print "\tcd\t80\t\t#calling the linux kernel to execute our print to stdout\n"; 311 | print "\tb8\t01 00 00 00\t#moving 'sys_exit' call number to eax\n"; 312 | print "\tcd\t80\t\t#executing it via linux sys_call\n\n"; 313 | print "\tAssemble with: m2elf.pl --in inputfile --out outputfile --mem 16\n\n"; 314 | exit; 315 | } 316 | -------------------------------------------------------------------------------- /spawnshell.m: -------------------------------------------------------------------------------- 1 | eb 2a #Jump down to The "CALL" op below 2 | 5e #Pop ESI (contains pointer to start of /bin/sh text) 3 | 89 76 08 #mov [esi+8], esi (8 bytes after /bin/sh text holds pointer to beginning of /bin/sh) 4 | c6 46 07 00 #mov [esi+7], 0 (moving 0 into 7 bytes after the /bin/sh [wich null terminates]) 5 | c7 46 0c 00 00 00 00 #mov [esi+12,] 0 (zero out some bytes offset 12 from /bin/sh text) 6 | b8 0b 00 00 00 #mov eax, 11 (syscall number for execing stuff) 7 | 89 f3 #mov ebx, esi (ebx is pointer to command to run, esi contains that pointer) 8 | 8d 4e 08 #lea ecx, [esi +8] (value of esi + 8 gets loaded into eax, which is pointing to after /bin/sh) 9 | 8d 56 0c #lea edx, [esi+12] (value of esi + 12 gets loaded into eax, which is pointing to a few bytes after /bin/sh) 10 | cd 80 #actually execute /bin/sh 11 | b8 01 00 00 00 #syscall for exiting program 12 | bb 00 00 00 00 #return value 13 | cd 80 #actually exit 14 | e8 d1 ff ff ff #Call to 2nd instruction, this also indirectly gets the address of the data below onto the stack. 15 | 2f 62 69 6e 2f 73 68 00 89 ec 5d c3 #/bin/sh 16 | --------------------------------------------------------------------------------