Home > Software engineering >  Error: Initialization with '{...}' expected for aggregate object
Error: Initialization with '{...}' expected for aggregate object

Time:05-30

below is my code which processes the payload[] array and store it's result on myFinalShellcode[] array.

#include <windows.h>
#include <stdio.h>

unsigned char payload[] = { 0xf0,0xe8,0xc8,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,0x51,0x56,0x48,0x31 };
constexpr int length = 891;
constexpr int number_of_chunks = 5;
constexpr int chunk_size = length / number_of_chunks;
constexpr int remaining_bytes = length % number_of_chunks;
constexpr int size_after = length * 2;

unsigned char* restore_original(unsigned char* high_ent_payload)
{
    constexpr int payload_size = (size_after   1) / 2;
    unsigned char low_entropy_payload_holder[size_after] = { 0 };
    memcpy_s(low_entropy_payload_holder, sizeof low_entropy_payload_holder, high_ent_payload, size_after);
    unsigned char restored_payload[payload_size] = { 0 };
    int offset_payload_after = 0;
    int offset_payload = 0;
    for (size_t i = 0; i < number_of_chunks; i  )
    {
        for (size_t j = 0; j < chunk_size; j  )
        {
            restored_payload[offset_payload] = low_entropy_payload_holder[offset_payload_after];
            offset_payload_after  ;
            offset_payload  ;
        }
        for (size_t k = 0; k < chunk_size; k  )
        {
            offset_payload_after  ;
        }
    }
    if (remaining_bytes)
    {
        for (size_t i = 0; i < sizeof remaining_bytes; i  )
        {
            restored_payload[offset_payload  ] = high_ent_payload[offset_payload_after  ];
        }
    }
    return restored_payload;
}

int main() {
    unsigned char shellcode[] = restore_original(payload);
}

I get the following error on the last code line (inside main function):
Error: Initialization with '{...}' expected for aggregate object

I tried to change anything on the array itself (seems like they might be the problem). I would highly appreciate your help as this is a part of my personal research :)

CodePudding user response:

In order to initialize an array defined with [], you must supply a list of values enclosed with {}, exactly as the error message says.
E.g.:

unsigned char shellcode[] = {1,2,3};

You can change shellcode to be a pointer if you want to assign it the output from restore_original:

unsigned char* shellcode = restore_original(payload);

Update:
As you can see in @heapunderrun's comment, there is another problem in your code. restore_original returns a pointer to a local variable, which is not valid when the function returns (a dangling pointer). In order to fix this, restore_original should allocate memory on the heap using new. This allocation has to be freed eventually, when you are done with shellcode.

However - although you can make it work this way, I highly recomend you to use std::vector for dynamic arrays allocated on the heap. It will save you the need to manually manage the memory allocations/deallocations, as well as other advantages.

CodePudding user response:

You can't assign a char * to a char []. You can probably do something with constexpr but I'm suspecting an XY problem here.

  • Related