From f97eb0f12750bf3ae96288376285bd796da5ba55 Mon Sep 17 00:00:00 2001 From: Jonathan Salwan Date: Thu, 20 Oct 2011 13:43:39 +0200 Subject: [PATCH] update and and xfunc --- Makefile | 3 +- includes/ropgadget.h | 7 +++ src/check_asm_mode.c | 12 ++--- src/check_filter_mode.c | 4 +- src/check_importsc_mode.c | 2 +- src/check_only_mode.c | 4 +- src/check_opcode_mode.c | 2 +- src/makecode.c | 24 +++------- src/makecode_importsc.c | 2 +- src/real_string_stringmode.c | 8 +--- src/return_maps_exec.c | 4 +- src/return_maps_read.c | 4 +- src/save_bin_data.c | 20 +++------ src/save_octet.c | 4 +- src/varop.c | 8 ++-- src/xfunc.c | 86 ++++++++++++++++++++++++++++++++++++ 16 files changed, 123 insertions(+), 71 deletions(-) create mode 100644 src/xfunc.c diff --git a/Makefile b/Makefile index 10d5a0e..80c8004 100644 --- a/Makefile +++ b/Makefile @@ -75,7 +75,8 @@ SRC = $(SRC_DIR)/main.c \ $(SRC_DIR)/check_limit_mode.c \ $(SRC_DIR)/no_filtered.c \ $(SRC_DIR)/varop.c \ - $(SRC_DIR)/onlymode.c + $(SRC_DIR)/onlymode.c \ + $(SRC_DIR)/xfunc.c OBJ = $(SRC:.c=.o) diff --git a/includes/ropgadget.h b/includes/ropgadget.h index ca96b3a..01ea547 100644 --- a/includes/ropgadget.h +++ b/includes/ropgadget.h @@ -293,5 +293,12 @@ void check_bind_mode(char **); void gadget_x8632(unsigned char *, unsigned int, Elf32_Addr, int, t_maps_exec *); void x8632(unsigned char *, unsigned int, t_maps_exec *, t_maps_read *); +/* xfunc */ +void *xmalloc(size_t); +int xopen(const char *, int, mode_t); +void *xmmap(void *, size_t, int, int, int, off_t); +ssize_t xread(int, void *, size_t); +int xclose(int); + #endif diff --git a/src/check_asm_mode.c b/src/check_asm_mode.c index 30741bd..7d3f34e 100644 --- a/src/check_asm_mode.c +++ b/src/check_asm_mode.c @@ -36,7 +36,7 @@ static void write_source_file(char *str) int i; i = 0; - fd = open(SFILE_WRITE, O_WRONLY | O_CREAT | O_APPEND, 0755); + fd = xopen(SFILE_WRITE, O_WRONLY | O_CREAT | O_APPEND, 0755); while (str[i] != '\0') { if (str[i] == ';') @@ -46,7 +46,7 @@ static void write_source_file(char *str) i++; } write(fd, "\n", 1); - close(fd); + xclose(fd); } Elf32_Off return_info_text(int flag, void *map, Elf32_Ehdr *ElfH, Elf32_Shdr *ElfS) @@ -108,8 +108,8 @@ static void build_code(char *str) if (stat(BFILE_WRITE, &sts) == -1) exit(EXIT_FAILURE); - fd = open(BFILE_WRITE, O_RDONLY); - map = mmap(0, sts.st_size, PROT_READ, MAP_SHARED, fd, 0); + fd = xopen(BFILE_WRITE, O_RDONLY, 0644); + map = xmmap(0, sts.st_size, PROT_READ, MAP_SHARED, fd, 0); aspElf_Header = map; aspElf_Shdr = (Elf32_Shdr *)((char *)map + aspElf_Header->e_shoff); @@ -118,14 +118,14 @@ static void build_code(char *str) size = return_info_text(1, map, aspElf_Header, aspElf_Shdr); asm_mode.size = size; - asm_mode.opcode = malloc((size * sizeof(char)) + 1); + asm_mode.opcode = xmalloc((size * sizeof(char)) + 1); asm_mode.argument = str; memcpy((char *)asm_mode.opcode, (char *)map + offset, asm_mode.size); opcode_mode.flag = 1; opcode_mode.size = asm_mode.size; opcode_mode.opcode = asm_mode.opcode; - close(fd); + xclose(fd); del_files(); } diff --git a/src/check_filter_mode.c b/src/check_filter_mode.c index 3ca3918..7fa8f9c 100644 --- a/src/check_filter_mode.c +++ b/src/check_filter_mode.c @@ -25,9 +25,7 @@ static t_filter_linked *add_element_filter(t_filter_linked *old_element, char *w { t_filter_linked *new_element; - new_element = malloc(sizeof(t_filter_linked)); - if (new_element == NULL) - exit(EXIT_FAILURE); + new_element = xmalloc(sizeof(t_filter_linked)); new_element->word = word; new_element->next = old_element; diff --git a/src/check_importsc_mode.c b/src/check_importsc_mode.c index 52606c3..a3ace41 100644 --- a/src/check_importsc_mode.c +++ b/src/check_importsc_mode.c @@ -42,7 +42,7 @@ static void make_opcode(char *str) size = size_opcode(str); importsc_mode.size = size; - ptr = malloc(size * sizeof(char)); + ptr = xmalloc(size * sizeof(char)); memset(ptr, 0x00, size * sizeof(char)); while (i != size) { diff --git a/src/check_only_mode.c b/src/check_only_mode.c index 85ee3b6..2bb360f 100644 --- a/src/check_only_mode.c +++ b/src/check_only_mode.c @@ -25,9 +25,7 @@ static t_only_linked *add_element_only(t_only_linked *old_element, char *word) { t_only_linked *new_element; - new_element = malloc(sizeof(t_only_linked)); - if (new_element == NULL) - exit(EXIT_FAILURE); + new_element = xmalloc(sizeof(t_only_linked)); new_element->word = word; new_element->next = old_element; diff --git a/src/check_opcode_mode.c b/src/check_opcode_mode.c index c1e6eca..f889300 100644 --- a/src/check_opcode_mode.c +++ b/src/check_opcode_mode.c @@ -61,7 +61,7 @@ static void make_opcode(char *str) size = size_opcode(str); opcode_mode.size = size; - ptr = malloc(size * sizeof(char)); + ptr = xmalloc(size * sizeof(char)); memset(ptr, 0x00, size * sizeof(char)); while (i != size) { diff --git a/src/makecode.c b/src/makecode.c index 4e18a91..d5f0395 100644 --- a/src/makecode.c +++ b/src/makecode.c @@ -37,9 +37,7 @@ t_makecode *add_element(t_makecode *old_element, char *instruction, Elf32_Addr a { t_makecode *new_element; - new_element = malloc(sizeof(t_makecode)); - if (new_element == NULL) - exit(EXIT_FAILURE); + new_element = xmalloc(sizeof(t_makecode)); new_element->addr = addr; new_element->instruction = instruction; new_element->next = old_element; @@ -100,13 +98,7 @@ static char *get_first_reg(char *gadget) { char *p; - p = malloc(4 * sizeof(char)); - if (!p) - { - fprintf(stderr, "Error malloc\n"); - exit(EXIT_FAILURE); - } - + p = xmalloc(4 * sizeof(char)); while (*gadget != '(' && *gadget != '\0') gadget++; @@ -120,13 +112,7 @@ static char *get_second_reg(char *gadget) { char *p; - p = malloc(4 * sizeof(char)); - if (!p) - { - fprintf(stderr, "Error malloc\n"); - exit(EXIT_FAILURE); - } - + p = xmalloc(4 * sizeof(char)); while (*gadget != '(' && *gadget != '\0') gadget++; @@ -218,7 +204,7 @@ static void makepartie1_local(t_makecode *list_ins) xor_gadget = get_gadget_since_addr(addr_xor_gadget); fprintf(stdout, "\t%sPayload%s\n", YELLOW, ENDC); - fprintf(stdout, "\t\t%s# execve /bin/sh generated by RopGadget v3.2%s\n", BLUE, ENDC); + fprintf(stdout, "\t\t%s# execve /bin/sh generated by RopGadget v3.3%s\n", BLUE, ENDC); /*****************\/bin*********************/ fprintf(stdout, "\t\t%sp += pack(\"next != NULL) importsc_mode.poctet = importsc_mode.poctet->next; diff --git a/src/real_string_stringmode.c b/src/real_string_stringmode.c index 0a47f74..23217bb 100644 --- a/src/real_string_stringmode.c +++ b/src/real_string_stringmode.c @@ -74,13 +74,7 @@ char *real_string_stringmode(char *base_string, unsigned char *data) int i = 0; size = (strlen(base_string) + 1); - real_string = malloc(size * sizeof(char)); - if (real_string == NULL) - { - fprintf(stderr, "Error malloc\n"); - exit(EXIT_FAILURE); - } - + real_string = xmalloc(size * sizeof(char)); strncpy(real_string, base_string, size); while (check_var(real_string) == 1) diff --git a/src/return_maps_exec.c b/src/return_maps_exec.c index e6a4243..c9cb630 100644 --- a/src/return_maps_exec.c +++ b/src/return_maps_exec.c @@ -26,9 +26,7 @@ static t_maps_exec *add_maps_exec(t_maps_exec *old_element, Elf32_Addr addr_star { t_maps_exec *new_element; - new_element = malloc(sizeof(t_maps_exec)); - if (new_element == NULL) - exit(EXIT_FAILURE); + new_element = xmalloc(sizeof(t_maps_exec)); new_element->addr_start = addr_start; new_element->addr_end = addr_end; new_element->next = old_element; diff --git a/src/return_maps_read.c b/src/return_maps_read.c index 2ca72b9..ed48116 100644 --- a/src/return_maps_read.c +++ b/src/return_maps_read.c @@ -26,9 +26,7 @@ static t_maps_read *add_maps_read(t_maps_read *old_element, Elf32_Addr addr_star { t_maps_read *new_element; - new_element = malloc(sizeof(t_maps_read)); - if (new_element == NULL) - exit(EXIT_FAILURE); + new_element = xmalloc(sizeof(t_maps_read)); new_element->addr_start = addr_start; new_element->addr_end = addr_end; new_element->next = old_element; diff --git a/src/save_bin_data.c b/src/save_bin_data.c index 655caac..d4625fb 100644 --- a/src/save_bin_data.c +++ b/src/save_bin_data.c @@ -26,21 +26,11 @@ unsigned char *save_bin_data(char *binary, unsigned int size) unsigned char *data; int fd; - fd = open(binary, O_RDONLY); - pMapElf = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); - if (fd == -1) - { - perror("open"); - exit(EXIT_FAILURE); - } - data = malloc(size * sizeof(char)); - if (data == NULL) - { - perror("malloc"); - exit(EXIT_FAILURE); - } - read(fd, data, size); - close(fd); + fd = xopen(binary, O_RDONLY, 0644); + pMapElf = xmmap(0, size, PROT_READ, MAP_SHARED, fd, 0); + data = xmalloc(size * sizeof(char)); + xread(fd, data, size); + xclose(fd); return (data); } diff --git a/src/save_octet.c b/src/save_octet.c index 772df17..1449eeb 100644 --- a/src/save_octet.c +++ b/src/save_octet.c @@ -25,9 +25,7 @@ t_char_importsc *add_char_importsc(t_char_importsc *old_element, char octet, Elf { t_char_importsc *new_element; - new_element = malloc(sizeof(t_char_importsc)); - if (new_element == NULL) - exit(EXIT_FAILURE); + new_element = xmalloc(sizeof(t_char_importsc)); new_element->addr = addr; new_element->octet = octet; new_element->next = old_element; diff --git a/src/varop.c b/src/varop.c index 302b288..a76ae30 100644 --- a/src/varop.c +++ b/src/varop.c @@ -26,9 +26,7 @@ t_varop *add_element_varop(t_varop *old_element, char *instruction, Elf32_Addr o { t_varop *new_element; - new_element = malloc(sizeof(t_varop)); - if (new_element == NULL) - exit(EXIT_FAILURE); + new_element = xmalloc(sizeof(t_varop)); new_element->instruction = instruction; new_element->addr = offset; new_element->next = old_element; @@ -86,7 +84,7 @@ char *ret_instruction_interrogation(Elf32_Addr offset, char *instruction, char * ret = calc_pos_charany(value, size); if (ret == -1) return ("Error instruction with '?'\n"); - gad = malloc((strlen(instruction) + 64) * sizeof(char)); + gad = xmalloc((strlen(instruction) + 64) * sizeof(char)); memset(gad, 0x00, (strlen(instruction) + 64) * sizeof(char)); offset_interrogation = (unsigned char *)(offset + ret); @@ -118,7 +116,7 @@ char *ret_instruction_diese(Elf32_Addr offset, char *instruction, char *value, i ret = calc_pos_charany(value, size); if (ret == -1) return ("Error instruction with '_'\n"); - gad = malloc((strlen(instruction) + 64) * sizeof(char)); + gad = xmalloc((strlen(instruction) + 64) * sizeof(char)); memset(gad, 0x00, (strlen(instruction) + 64) * sizeof(char)); offset_diese = (unsigned char *)(offset + ret); diff --git a/src/xfunc.c b/src/xfunc.c new file mode 100644 index 0000000..336b80c --- /dev/null +++ b/src/xfunc.c @@ -0,0 +1,86 @@ +/* +** 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 +*/ + +#include "ropgadget.h" + +void *xmalloc(size_t size) +{ + char *p; + + p = malloc(size); + if (p == NULL) + { + perror("malloc"); + exit(EXIT_FAILURE); + } + return (p); +} + +int xopen(const char *pathname, int flags, mode_t mode) +{ + int fd; + + fd = open(pathname, flags, mode); + if (fd == -1) + { + perror("open"); + exit(EXIT_FAILURE); + } + return (fd); +} + +void *xmmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) +{ + void *p; + + p = mmap(addr, len, prot, flags, fildes, off); + if (p == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } + + return (p); +} + +ssize_t xread(int fd, void *buf, size_t count) +{ + ssize_t ret; + + ret = read(fd, buf, count); + if (ret == -1) + { + perror("read"); + exit(EXIT_FAILURE); + } + return (ret); +} + +int xclose(int fd) +{ + int ret; + + ret = close(fd); + if (ret == -1) + perror("close"); + + return (ret); +}