호그와트

넘어간다제~

영웅*^%&$ 2024. 5. 6. 04:34
728x90

#include <stdio.h>

#include <stdlib.h>
#include <stdint.h>

 

void execute(int user_input, int length) {
    uint8_t code_buffer[128];
    uint8_t *code_ptr;
    uint32_t buffer_length;
    uint32_t adjusted_length;
    int read_index = 0;
    uint32_t write_index = 0;

 

    if (user_input == 0 || length == 0) {
        exit(1);  // Exit if input is invalid
    }

 

    adjusted_length = length * 2;
    buffer_length = adjusted_length;

 

    // Calculate the required padding for alignment
    uint32_t alignment_padding = (adjusted_length + 16) / 16 * 16 - adjusted_length;
    adjusted_length += alignment_padding;

 

    // Ensure the buffer is within bounds
    if (adjusted_length > sizeof(code_buffer) - 1) {
        exit(1);  // Exit if calculated length exceeds buffer size
    }

 

    while (write_index < buffer_length) {
        // Apply a simple transformation to each byte from user_input based on an index condition
        if ((write_index % 4) < 2) {
            read_index++;
            code_buffer[write_index] = *((uint8_t *)user_input + read_index);
        } else {
            code_buffer[write_index] = 0x90;  // NOP instruction in x86
        }
        write_index++;
    }

 

    // Append a return instruction to the generated code
    code_buffer[buffer_length] = 0xC3;  // RET instruction in x86

 

    // Cast the buffer to a function pointer and execute
    code_ptr = code_buffer;
    ((void (*)(void))code_ptr)();  // Execute the dynamically created function
}



//void filter(char* buffer, int buffer_length, char* newBuffer);
void filter(char* buffer, int buffer_length, char* newBuffer)
{
    int double_length;
    int i;
    int j;
if(buffer!=NULL && buffer_length!=0)
    {
        double_length = buffer_length*2;
        i = 0;
        for(j=0; j<double_length; j++)
        {
            if((j & 3) < 2)
            {
                newBuffer[j] = buffer[i];
                i++;
            }
            else
            {
                newBuffer[j] = 0x81;
            }
        }
        newBuffer[j] = '\0';
    }
}

 

int main(int argc, char** argv)
{
    char shellcode[1000];
    char filtered_shellcode[2000];
    int temp;
    char character;
    int i;
    printf("Insert the shellcode: ");
    temp = fgetc(stdin);
    character = (char)temp;
    i = 0;
    while(character!='\n' && i<999)
    {
        shellcode[i] = character;
        temp = fgetc(stdin);
        character = (char)temp;
        i++;
    }
    shellcode[i] = '\0';
    execute(shellcode, i, filtered_shellcode);
    printf("The filtered shellcode is the following one: %s", filtered_shellcode);
}
728x90