remove old files
This commit is contained in:
parent
58d7f47df9
commit
7ea9ce635c
BIN
tests/bfd_file
BIN
tests/bfd_file
Binary file not shown.
121
tests/bfd_file.c
121
tests/bfd_file.c
@ -1,121 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <bfd.h>
|
|
||||||
#include <dis-asm.h>
|
|
||||||
|
|
||||||
// apt install binutils binutils-dev
|
|
||||||
// https://sourceware.org/binutils/docs/bfd/BFD-Index.html
|
|
||||||
// gcc bfd_file.c -o bfd_file -lopcodes -lbfd && ./bfd_file
|
|
||||||
// https://ftp.gnu.org/old-gnu/Manuals/bfd-2.9.1/html_chapter/bfd_1.html
|
|
||||||
|
|
||||||
static bfd_boolean disassemble;
|
|
||||||
struct data {
|
|
||||||
char *data;
|
|
||||||
};
|
|
||||||
|
|
||||||
static int dump_asm(void *stream, const char *fmt, ...){
|
|
||||||
struct data *sData = (struct data*)stream;
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
char str[64];
|
|
||||||
vsprintf(str, fmt, args);
|
|
||||||
//vsnprintf(str, sizeof(str), fmt, args);
|
|
||||||
//asprintf(str, "%s\n", args);
|
|
||||||
|
|
||||||
va_end(args);
|
|
||||||
char tmp[64];
|
|
||||||
memset(tmp, 0, 64);
|
|
||||||
//printf("%d\n", sData->offset);
|
|
||||||
if (strlen(sData->data) > 0){
|
|
||||||
memcpy(tmp, sData->data, 64);
|
|
||||||
//printf("TMP: %s %d\n", tmp, strlen(tmp));
|
|
||||||
strcat(tmp, str);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
memcpy(tmp, str, strlen(str));
|
|
||||||
memcpy(sData->data, tmp, 64);
|
|
||||||
}
|
|
||||||
static int disass(){
|
|
||||||
struct disassemble_info disasm_info;
|
|
||||||
struct data *sData = (struct data*)malloc(sizeof(struct data));
|
|
||||||
sData->data = (char*)malloc(64);
|
|
||||||
//char filename[256] = "/home/geoffrey/Documents/GIT/cybersecurity/forensic/disassembly/call_function";
|
|
||||||
char filename[256];
|
|
||||||
bfd *abfd = NULL;
|
|
||||||
struct stat s_stat;
|
|
||||||
int fd_r;
|
|
||||||
|
|
||||||
pid_t pid = getpid();
|
|
||||||
sprintf(filename, "/proc/%d/exe", pid);
|
|
||||||
printf("%s\n", filename);
|
|
||||||
if ((fd_r = open(filename, O_RDONLY)) < 0){
|
|
||||||
printf("Failed to read file\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fstat(fd_r, &s_stat);
|
|
||||||
printf("Len: %d\n", s_stat.st_size);
|
|
||||||
|
|
||||||
close(fd_r);
|
|
||||||
|
|
||||||
bfd_init();
|
|
||||||
abfd = bfd_openr(filename, NULL);
|
|
||||||
//set_default_bfd_target();
|
|
||||||
|
|
||||||
if (abfd == NULL){
|
|
||||||
printf("Cannot read bfd file\n");
|
|
||||||
free(sData->data);
|
|
||||||
free(sData);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Defined in https://github.com/redox-os/binutils-gdb/blob/master/include/dis-asm.h
|
|
||||||
//init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
|
|
||||||
init_disassemble_info (&disasm_info, sData, dump_asm);
|
|
||||||
disasm_info.arch = bfd_get_arch(abfd);
|
|
||||||
disasm_info.mach = bfd_get_mach(abfd);
|
|
||||||
/*disasm_info.arch = bfd_arch_i386;
|
|
||||||
disasm_info.mach = bfd_mach_x86_64;
|
|
||||||
disasm_info.read_memory_func = buffer_read_memory;
|
|
||||||
disasm_info.buffer = code;
|
|
||||||
disasm_info.buffer_vma = 0;
|
|
||||||
disasm_info.buffer_length = length;*/
|
|
||||||
disassemble_init_for_target(&disasm_info);
|
|
||||||
printf("%d\n", bfd_get_arch(abfd));
|
|
||||||
printf("%d\n", bfd_get_mach(abfd));
|
|
||||||
printf("%s\n", bfd_printable_arch_mach(
|
|
||||||
bfd_get_arch(abfd),
|
|
||||||
bfd_get_mach(abfd)));
|
|
||||||
|
|
||||||
//disassembler_ftype disas = disassembler(abfd);
|
|
||||||
disassembler_ftype disas = disassembler(bfd_arch_i386, false, bfd_mach_x86_64, NULL);
|
|
||||||
|
|
||||||
if (!disas){
|
|
||||||
printf("Can't disassemble\n");
|
|
||||||
perror("disassembler()");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t i = 0;
|
|
||||||
size_t length = s_stat.st_size;
|
|
||||||
while (i < length){
|
|
||||||
size_t octets = disas(i, &disasm_info);
|
|
||||||
printf("%s\n", sData->data);
|
|
||||||
i += octets;
|
|
||||||
memset(sData->data, 0, 64);
|
|
||||||
};
|
|
||||||
|
|
||||||
free(sData->data);
|
|
||||||
free(sData);
|
|
||||||
bfd_close(abfd);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int main(void){
|
|
||||||
disass();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
BIN
tests/test_bfd
BIN
tests/test_bfd
Binary file not shown.
100
tests/test_bfd.c
100
tests/test_bfd.c
@ -1,100 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <dis-asm.h>
|
|
||||||
#include "bfd.h"
|
|
||||||
|
|
||||||
// apt install binutils binutils-dev
|
|
||||||
// gcc test_bfd.c -o test_bfd -lopcodes && ./test_bfd
|
|
||||||
// https://ftp.gnu.org/old-gnu/Manuals/bfd-2.9.1/html_chapter/bfd_1.html
|
|
||||||
|
|
||||||
static bfd_boolean disassemble;
|
|
||||||
struct data {
|
|
||||||
char *data;
|
|
||||||
};
|
|
||||||
|
|
||||||
static int dump_asm(void *stream, const char *fmt, ...){
|
|
||||||
struct data *sData = (struct data*)stream;
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
char str[64];
|
|
||||||
vsprintf(str, fmt, args);
|
|
||||||
//vsnprintf(str, sizeof(str), fmt, args);
|
|
||||||
//asprintf(str, "%s\n", args);
|
|
||||||
|
|
||||||
va_end(args);
|
|
||||||
char tmp[64];
|
|
||||||
memset(tmp, 0, 64);
|
|
||||||
//printf("%d\n", sData->offset);
|
|
||||||
if (strlen(sData->data) > 0){
|
|
||||||
memcpy(tmp, sData->data, 64);
|
|
||||||
//printf("TMP: %s %d\n", tmp, strlen(tmp));
|
|
||||||
strcat(tmp, str);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
memcpy(tmp, str, strlen(str));
|
|
||||||
memcpy(sData->data, tmp, 64);
|
|
||||||
}
|
|
||||||
static int disas(unsigned char *code, size_t length){
|
|
||||||
struct disassemble_info disasm_info;
|
|
||||||
struct data *sData = (struct data*)malloc(sizeof(struct data));
|
|
||||||
sData->data = (char*)malloc(64);
|
|
||||||
|
|
||||||
disassemble = TRUE;
|
|
||||||
bfd_init();
|
|
||||||
//set_default_bfd_target();
|
|
||||||
|
|
||||||
// Defined in https://github.com/redox-os/binutils-gdb/blob/master/include/dis-asm.h
|
|
||||||
//init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
|
|
||||||
init_disassemble_info (&disasm_info, sData, dump_asm);
|
|
||||||
disasm_info.arch = bfd_arch_i386;
|
|
||||||
disasm_info.mach = bfd_mach_x86_64;
|
|
||||||
disasm_info.read_memory_func = buffer_read_memory;
|
|
||||||
disasm_info.buffer = code;
|
|
||||||
disasm_info.buffer_vma = 0;
|
|
||||||
disasm_info.buffer_length = length;
|
|
||||||
disassemble_init_for_target(&disasm_info);
|
|
||||||
|
|
||||||
disassembler_ftype disas;
|
|
||||||
disas = disassembler(bfd_arch_i386, false, bfd_mach_x86_64, NULL);
|
|
||||||
|
|
||||||
if (!disas){
|
|
||||||
printf("Can't disassemble\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t i = 0;
|
|
||||||
while (i < length){
|
|
||||||
size_t octets = disas(i, &disasm_info);
|
|
||||||
printf("%s\n", sData->data);
|
|
||||||
i += octets;
|
|
||||||
memset(sData->data, 0, 64);
|
|
||||||
};
|
|
||||||
|
|
||||||
free(sData->data);
|
|
||||||
free(sData);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int main(void){
|
|
||||||
unsigned char code[] = {
|
|
||||||
0x55, 0x48, 0x89, 0xE5, 0x48, 0x83, 0xEC, 0x20, 0x89, 0x7D,
|
|
||||||
0xEC, 0x48, 0x8D, 0x4D, 0xF4, 0x8B, 0x45, 0xEC, 0xBA, 0x04,
|
|
||||||
0x00, 0x00, 0x00, 0x48, 0x89, 0xCE, 0x89, 0xC7, 0xE8, 0x42,
|
|
||||||
0xFC, 0xFF, 0xFF, 0x48, 0x89, 0x45, 0xF8, 0x48, 0x83, 0x7D,
|
|
||||||
0xF8, 0xFF, 0x75, 0x07, 0xB8, 0xFF, 0xFF, 0xFF, 0xFF, 0xEB,
|
|
||||||
0x2C, 0x0F, 0xB6, 0x45, 0xF4, 0x3C, 0x7F, 0x75, 0x1F, 0x0F,
|
|
||||||
0xB6, 0x45, 0xF5, 0x3C, 0x45, 0x75, 0x17, 0x0F, 0xB6, 0x45,
|
|
||||||
0xF6, 0x3C, 0x4C, 0x75, 0x0F, 0x0F, 0xB6, 0x45, 0xF7, 0x3C,
|
|
||||||
0x46, 0x75, 0x07, 0xB8, 0x01, 0x00, 0x00, 0x00, 0xEB, 0x05,
|
|
||||||
0xB8, 0x00, 0x00, 0x00, 0x00, 0xC9, 0xC3
|
|
||||||
};
|
|
||||||
size_t length = sizeof(code);
|
|
||||||
|
|
||||||
disas(code, length);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -1,119 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <dis-asm.h>
|
|
||||||
#include "bfd.h"
|
|
||||||
|
|
||||||
// apt install binutils binutils-dev
|
|
||||||
// gcc test_bfd.c -o test_bfd -lopcodes && ./test_bfd
|
|
||||||
// https://ftp.gnu.org/old-gnu/Manuals/bfd-2.9.1/html_chapter/bfd_1.html
|
|
||||||
|
|
||||||
static bfd_boolean disassemble;
|
|
||||||
struct data {
|
|
||||||
char **data;
|
|
||||||
size_t offset;
|
|
||||||
size_t index;
|
|
||||||
};
|
|
||||||
|
|
||||||
static int dump_asm(void *stream, const char *fmt, ...){
|
|
||||||
struct data *sData = (struct data*)stream;
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
char str[64];
|
|
||||||
vsprintf(str, fmt, args);
|
|
||||||
//vsnprintf(str, sizeof(str), fmt, args);
|
|
||||||
//asprintf(str, "%s\n", args);
|
|
||||||
|
|
||||||
va_end(args);
|
|
||||||
//printf("%s\n", str);
|
|
||||||
sData->offset = strlen(str);
|
|
||||||
int index = sData->index;
|
|
||||||
char tmp[64];
|
|
||||||
memset(tmp, 0, 64);
|
|
||||||
//printf("%d\n", sData->offset);
|
|
||||||
if (strlen(sData->data[index]) > 0){
|
|
||||||
memcpy(tmp, sData->data[index], 64);
|
|
||||||
//printf("TMP: %s %d\n", tmp, strlen(tmp));
|
|
||||||
strcat(tmp, str);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
memcpy(tmp, str, strlen(str));
|
|
||||||
memcpy(sData->data[index], tmp, 64);
|
|
||||||
}
|
|
||||||
static int disas(unsigned char *code, size_t length){
|
|
||||||
struct disassemble_info disasm_info;
|
|
||||||
struct data *sData = (struct data*)malloc(sizeof(struct data));
|
|
||||||
sData->data = (char**)malloc(sizeof(char *) * 64);
|
|
||||||
for (int i = 0; i < 64; i++){
|
|
||||||
sData->data[i] = (char*)malloc(64);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 64; i++)
|
|
||||||
memset(sData->data[i], 0, 64);
|
|
||||||
sData->offset = 0;
|
|
||||||
sData->index = 0;
|
|
||||||
|
|
||||||
disassemble = TRUE;
|
|
||||||
bfd_init();
|
|
||||||
//set_default_bfd_target();
|
|
||||||
|
|
||||||
// Defined in https://github.com/redox-os/binutils-gdb/blob/master/include/dis-asm.h
|
|
||||||
//init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
|
|
||||||
init_disassemble_info (&disasm_info, sData, dump_asm);
|
|
||||||
disasm_info.arch = bfd_arch_i386;
|
|
||||||
disasm_info.mach = bfd_mach_x86_64;
|
|
||||||
disasm_info.read_memory_func = buffer_read_memory;
|
|
||||||
disasm_info.buffer = code;
|
|
||||||
disasm_info.buffer_vma = 0;
|
|
||||||
disasm_info.buffer_length = length;
|
|
||||||
disassemble_init_for_target(&disasm_info);
|
|
||||||
|
|
||||||
disassembler_ftype disas;
|
|
||||||
disas = disassembler(bfd_arch_i386, false, bfd_mach_x86_64, NULL);
|
|
||||||
|
|
||||||
if (!disas){
|
|
||||||
printf("Can't disassemble\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t i = 0;
|
|
||||||
while (i < length){
|
|
||||||
size_t octets = disas(i, &disasm_info);
|
|
||||||
printf("%s\n", sData->data[sData->index]);
|
|
||||||
//disasm_info.stream = stdout;
|
|
||||||
//disasm_info.stream = (void*)sData;
|
|
||||||
//struct data *sData = (struct data*)disasm_info.stream;
|
|
||||||
//disasm_info.fprintf_func = (fprintf_ftype)fprintf;
|
|
||||||
//printf("%ld\n", sData->index);
|
|
||||||
i += octets;
|
|
||||||
sData->index += 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++)
|
|
||||||
free(sData->data[i]);
|
|
||||||
free(sData->data);
|
|
||||||
free(sData);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int main(void){
|
|
||||||
unsigned char code[] = {
|
|
||||||
0x55, 0x48, 0x89, 0xE5, 0x48, 0x83, 0xEC, 0x20, 0x89, 0x7D,
|
|
||||||
0xEC, 0x48, 0x8D, 0x4D, 0xF4, 0x8B, 0x45, 0xEC, 0xBA, 0x04,
|
|
||||||
0x00, 0x00, 0x00, 0x48, 0x89, 0xCE, 0x89, 0xC7, 0xE8, 0x42,
|
|
||||||
0xFC, 0xFF, 0xFF, 0x48, 0x89, 0x45, 0xF8, 0x48, 0x83, 0x7D,
|
|
||||||
0xF8, 0xFF, 0x75, 0x07, 0xB8, 0xFF, 0xFF, 0xFF, 0xFF, 0xEB,
|
|
||||||
0x2C, 0x0F, 0xB6, 0x45, 0xF4, 0x3C, 0x7F, 0x75, 0x1F, 0x0F,
|
|
||||||
0xB6, 0x45, 0xF5, 0x3C, 0x45, 0x75, 0x17, 0x0F, 0xB6, 0x45,
|
|
||||||
0xF6, 0x3C, 0x4C, 0x75, 0x0F, 0x0F, 0xB6, 0x45, 0xF7, 0x3C,
|
|
||||||
0x46, 0x75, 0x07, 0xB8, 0x01, 0x00, 0x00, 0x00, 0xEB, 0x05,
|
|
||||||
0xB8, 0x00, 0x00, 0x00, 0x00, 0xC9, 0xC3
|
|
||||||
};
|
|
||||||
size_t length = sizeof(code);
|
|
||||||
|
|
||||||
disas(code, length);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -1,36 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
struct test{
|
|
||||||
char b1;
|
|
||||||
char b2;
|
|
||||||
char b3;
|
|
||||||
};
|
|
||||||
int main(void){
|
|
||||||
char buf[9];
|
|
||||||
int fd_r;
|
|
||||||
//struct test *s_test = malloc(sizeof(struct test));
|
|
||||||
struct test *s_test = NULL;
|
|
||||||
memset(buf, 0, 9);
|
|
||||||
|
|
||||||
if ((fd_r = open("test.txt", O_RDONLY)) == -1){
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
read(fd_r, buf, 9);
|
|
||||||
|
|
||||||
for (int i = 0; i < 9; i++){
|
|
||||||
printf("%c", buf[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
s_test = (struct test*)buf + 2;
|
|
||||||
|
|
||||||
printf("%c %c %c\n", s_test->b1, s_test->b2, s_test->b3);
|
|
||||||
|
|
||||||
close(fd_r);
|
|
||||||
//free(s_test);
|
|
||||||
}
|
|
||||||
Binary file not shown.
@ -1,22 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#define TEST 5
|
|
||||||
|
|
||||||
int main(void){
|
|
||||||
int *foo = (int*)malloc(sizeof(int) * TEST);
|
|
||||||
for(int i = 0; i < TEST; i++)
|
|
||||||
foo[i] = i;
|
|
||||||
|
|
||||||
for(int i = 0; i < TEST; i++)
|
|
||||||
printf("%d\n", foo[i]);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
foo = (int *)realloc(foo, sizeof(int) * (TEST + TEST));
|
|
||||||
for(int i = TEST; i < TEST + TEST; i++)
|
|
||||||
foo[i] = i + 10;
|
|
||||||
|
|
||||||
for(int i = 0; i < TEST + TEST; i++)
|
|
||||||
printf("%d\n", foo[i]);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user