From 5c9b93479b7fd2fbf7e2708fe3178584acd68322 Mon Sep 17 00:00:00 2001 From: JoYo Date: Wed, 20 Jul 2016 21:30:45 -0400 Subject: [PATCH] cli pic loading to move everything else into python --- generation.c | 126 +++++++++++++++++++++++++++ scrap.asm => seed.asm | 0 sins.c | 193 ------------------------------------------ wscript | 10 +-- 4 files changed, 130 insertions(+), 199 deletions(-) create mode 100644 generation.c rename scrap.asm => seed.asm (100%) delete mode 100644 sins.c diff --git a/generation.c b/generation.c new file mode 100644 index 0000000..5326cea --- /dev/null +++ b/generation.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include + +int reproduce(unsigned char *pic_address, int pic_size) +{ + int return_value = 0; + + for (int iter = 0; iter < pic_size; iter++) + { + printf("%02X", pic_address[iter]); + } + + printf("\n"); + + return_value = 1; +CLONE_CLEANUP: + if (NULL != pic_address) + { + munmap(pic_address, pic_size); + } + + exit(0); +} + +int hex_ascii_to_bin(char *hex_string, int hex_len, unsigned char *hex_bin) +{ + if (hex_bin == NULL || hex_string == NULL || hex_len % 2 != 0) + { + return 0; + } + + char *position = hex_string; + + for (int index = 0; index - 1 < hex_len; index++) + { + if (*position == NULL) break; + + for (int offset = 0; offset < 2; offset++, position++) + { + unsigned char hex_sort = *position | 0x20; + + if (hex_sort >= '0' && hex_sort <= '9') + { + hex_sort -= 0x30; + } + else if (hex_sort <= 'f' && hex_sort >= 'a') + { + hex_sort -= 0x57; + } + else + { + return 0; + } + if (!offset) + { + hex_bin[index] = (hex_sort << 4); + } + else + { + hex_bin[index] |= hex_sort; + } + } + } + + return 1; +} + +int generation(char *parent_hex, int rand_offset, char rand_flip) +{ + int return_value = 0; + int parent_hex_len = strlen(parent_hex); + int parent_bin_len = parent_hex_len / 2; + + int prot = (PROT_READ | PROT_WRITE | PROT_EXEC); + int flags = (MAP_ANON | MAP_PRIVATE); + unsigned char *pic_buffer = mmap(NULL, parent_bin_len, prot, flags, -1, 0); + if (MAP_FAILED == pic_buffer) + { + return_value = errno; + goto GEN_CLEANUP; + } + + hex_ascii_to_bin(parent_hex, parent_hex_len, pic_buffer); + + pic_buffer[rand_offset] = pic_buffer[rand_offset] ^ rand_flip; + + int (*reproduce_function)(unsigned char *, int) = reproduce; + void (*pic_function)(void *, int, void *) = pic_buffer; + + pic_function(pic_buffer, parent_bin_len, reproduce_function); + + return_value = 1; +GEN_CLEANUP: + if (NULL != pic_buffer) + { + munmap(pic_buffer, parent_bin_len); + } + + return return_value; +} + +int main(int argc, const char **argv) +{ + if (3 > argc || argv[1] == NULL || argv[2] == NULL || argv[3] == NULL) + { + exit(1); + } + + char *hex_string = argv[1]; + int rand_offset = atoi(argv[2]); + char rand_flip = atoi(argv[3]); + + int return_value = 0; + + return_value = generation(hex_string, rand_offset, rand_flip); + if (!return_value) + { + exit(1); + } + + return 0; +} diff --git a/scrap.asm b/seed.asm similarity index 100% rename from scrap.asm rename to seed.asm diff --git a/sins.c b/sins.c deleted file mode 100644 index 2c041dc..0000000 --- a/sins.c +++ /dev/null @@ -1,193 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "list.h" - -#pragma comment(lib, "openssl/sha.lib") -#define SHA_SUM_LENGTH (SHA_DIGEST_LENGTH + SHA_DIGEST_LENGTH + 1) - -int generation(char *seed_path); -int reproduce(void *pic_address, size_t pic_size); - -struct process_list -{ - struct list_head list; - pid_t process_id; -}; - -char seed_path[SHA_SUM_LENGTH]; -FILE *seed_handle = NULL; -void *pic_buffer = NULL; - -int main(int argc, const char **argv) -{ - struct process_list scraps; - struct process_list *iter = NULL; - pid_t process_id; - int status = 0; - int count = 0; - int return_value = 0; - - INIT_LIST_HEAD(&scraps.list); - - strncpy(seed_path, argv[1], SHA_SUM_LENGTH); - - while (1) - { - process_id = fork(); - iter = (struct process_list *)malloc(sizeof(struct process_list)); - iter->process_id = process_id; - list_add_tail(&(iter->list), &(scraps.list)); - - if (process_id == 0) - { - generation(seed_path); - break; - } - - if (process_id < 0) - { - count = 0; - list_for_each_entry(iter, &scraps.list, list) - { - count++; /* @todo clean up pids */ - return_value = waitpid(iter->process_id, &status, 0); - return_value = WEXITSTATUS(return_value); - } - printf("%d", count); - break; - } - } - - return count; -} - -int generation(char *seed_path) -{ - int return_value = 0; - long int mutation_value; - struct stat pic_statistics; - unsigned int mutation_offset = 0; - unsigned char pic_mutated = 0; - struct drand48_data drand_data; - - seed_handle = fopen(seed_path, "rb"); - if (NULL == seed_handle) - { - return_value = errno; - goto GEN_CLEANUP; - } - - return_value = fstat(fileno(seed_handle), &pic_statistics); - if (-1 == return_value) - { - return_value = errno; - goto GEN_CLEANUP; - } - - int prot = (PROT_READ | PROT_WRITE | PROT_EXEC); - int flags = (MAP_ANON | MAP_PRIVATE); - pic_buffer = mmap(NULL, pic_statistics.st_size, prot, flags, -1, 0); - if (MAP_FAILED == pic_buffer) - { - return_value = errno; - goto GEN_CLEANUP; - } - - return_value = fread(pic_buffer, 1, pic_statistics.st_size, seed_handle); - if (return_value != pic_statistics.st_size) - { - return_value = errno; - goto GEN_CLEANUP; - } - - if (NULL != seed_handle) - { - fclose(seed_handle); - seed_handle = NULL; - } - - srand48_r(time(NULL), &drand_data); - lrand48_r(&drand_data, &mutation_value); - - mutation_offset = (mutation_value % (pic_statistics.st_size + 1)); - pic_mutated = - ((unsigned char *)pic_buffer)[mutation_offset] & (mutation_value % 2); - ((char *)pic_buffer)[mutation_offset] = pic_mutated; - - int (*reproduce_function)(void *, size_t) = reproduce; - void (*pic_function)(void *, size_t, void *) = pic_buffer; - - pic_function(pic_buffer, pic_statistics.st_size, reproduce_function); - -GEN_CLEANUP: - if (NULL != pic_buffer) - { - munmap(pic_buffer, pic_statistics.st_size); - } - if (NULL != seed_handle) - { - fclose(seed_handle); - seed_handle = NULL; - } - - return return_value; -} - -int reproduce(void *pic_address, size_t pic_size) -{ - int return_value = 0; - unsigned char digest[SHA_DIGEST_LENGTH]; - struct stat pic_statistics; - - memset(seed_path, 0, SHA_SUM_LENGTH); - SHA1((const unsigned char *)pic_address, pic_size, digest); - - for (int iter = 0; iter < SHA_DIGEST_LENGTH; iter++) - { - sprintf(&seed_path[iter * 2], "%02x", digest[iter]); - } - - FILE *survived_store = fopen(seed_path, "w+"); - if (NULL == survived_store) - { - return_value = errno; - goto CLONE_CLEANUP; - } - - return_value = fwrite(pic_address, 1, pic_size, survived_store); - if (return_value != pic_size) - { - return_value = errno; - goto CLONE_CLEANUP; - } - - return_value = 1; -CLONE_CLEANUP: - if (survived_store) - { - fclose(survived_store); - } - fstat(fileno(seed_handle), &pic_statistics); - if (NULL != pic_buffer) - { - munmap(pic_buffer, pic_statistics.st_size); - } - if (NULL != seed_handle) - { - fclose(seed_handle); - seed_handle = NULL; - } - - return generation(seed_path); -} diff --git a/wscript b/wscript index 86b9225..d677c1a 100644 --- a/wscript +++ b/wscript @@ -11,14 +11,12 @@ def options(opt): def configure(conf): conf.load('nasm') conf.load('compiler_c') - conf.check_cc(header_name='openssl/sha.h') def build(bld): bld.program( - source='sins.c', - target='sins', - cflags=['-std=gnu11'], - lib=['ssl', 'crypto'] + source='generation.c', + target='generation', + cflags=['-g', '-std=gnu11'] ) - bld(features='asm', source='scrap.asm', target='scrap') + bld(features='asm', source='seed.asm', target='seed')