Search code examples
arrayscpointersdynamic-memory-allocationshellcode

Printing the complete size of char*


I'm working on a C project, the goal is to reach a web server, read the data inside a file (example.com/shellcode.bin for example) and store it inside an array.

Currently, I managed to make the necessary GET requests, i can find my shellcode, insert it into an array (mycode) but when I return it, it sends me the wrong size.

For example, if sizeof(mycode) return 270, sizeof(PE) return 8.

Is it possible to find the total size of the PE variable ?

    size_t size = sizeof(mycode);
    char* PE = (char*)malloc(size);
    for (int i = 0; i < sizeof(mycode); i++) {
        PE[i] = mycode[i];
    }

    printf("Shellcode size before return : %ld\n", sizeof(PE));

    return PE;

I tried different format string outputs (%s with strlen, %d, %ld, %zu ....) all of them returned 8.


Solution

  • One solution is to return a struct containing both a pointer to the buffer and the length.

    // outside the function
    typedef struct {
      char* data;
      size_t size;
    } Buffer;
    
    // in the function
    Buffer buffer;
    buffer.data = PE;
    buffer.size = size;
    return buffer;
    

    And also change the return type to Buffer.