How load content from file to char array[]?

FLASHCODER 21 Reputation points
2020-10-06T22:16:12.917+00:00

I have a char array[] and is like following:

// MessageBox
   char xcode[] = "\x31\xc9\x64\x8b\x41\x30\x8b\x40\xc\x8b\x70\x14\xad\x96\xad\x8b\x58\x10\x8b\x53\x3c\x1\xda\x8b\x52\x78\x1\xda\x8b\x72\x20\x1\xde\x31\xc9\x41\xad\x1\xd8\x81\x38\x47\x65\x74\x50\x75\xf4\x81\x78\x4\x72\x6f\x63\x41\x75\xeb\x81\x78\x8\x64\x64\x72\x65\x75\xe2\x8b\x72\x24\x1\xde\x66\x8b\xc\x4e\x49\x8b\x72\x1c\x1\xde\x8b\x14\x8e\x1\xda\x31\xc9\x53\x52\x51\x68\x61\x72\x79\x41\x68\x4c\x69\x62\x72\x68\x4c\x6f\x61\x64\x54\x53\xff\xd2\x83\xc4\xc\x59\x50\x51\x66\xb9\x6c\x6c\x51\x68\x33\x32\x2e\x64\x68\x75\x73\x65\x72\x54\xff\xd0\x83\xc4\x10\x8b\x54\x24\x4\xb9\x6f\x78\x41\x0\x51\x68\x61\x67\x65\x42\x68\x4d\x65\x73\x73\x54\x50\xff\xd2\x83\xc4\x10\x68\x61\x62\x63\x64\x83\x6c\x24\x3\x64\x89\xe6\x31\xc9\x51\x56\x56\x51\xff\xd0";

Then i had inserted all this content of variable above into a txt file (file with ANSI format and content without the "") and tried load this way:

    ifstream infile;

    infile.open("shellcode.bin", std::ios::in | std::ios::binary);
    infile.seekg(0, std::ios::end);

    size_t file_size_in_byte = infile.tellg();
    char* xcode = (char*)malloc(sizeof(char) * file_size_in_byte);

    infile.seekg(0, std::ios::beg);
    infile.read(xcode, file_size_in_byte);

    if (infile.eof()) {
        size_t bytes_really_read = infile.gcount();
    }
    else if (infile.fail()) {
    }

    infile.close();

Then when i tested the program with getting through first method (without loading from file), all worked fine, already when tested loading from file, without success.

Then i ask:

What's equivalent code (to load this content from file) that can generate the same result like if already initialize the char array[] with content (first method)?

Developer technologies C++
{count} votes

Accepted answer
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2020-10-07T02:07:31.58+00:00

    Hi,

    I suggest you could try to assign the last value of the array to'\0'.

    Here is my code:

    char* xcode = (char*)malloc(sizeof(char) * file_size_in_byte);  
    	xcode[file_size_in_byte] = '\0';  
    

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2020-10-07T01:54:45.24+00:00

    The %s format specifier with printf says that you are passing it a
    nul-terminated character array - a C string. When your data does not
    end with a nul then printf will keep printing until it finds one
    somewhere in memory following your array.

    • Wayne
    0 comments No comments

  2. WayneAKing 4,931 Reputation points
    2020-10-07T02:11:46.157+00:00

    You can tell printf to limit the number of characters to display when using
    %s like this:

    char str[] = "1234567890";
    int len = 6;
    printf("%.*s", len, str);
    

    So you can use the length of your array as the limit:

    printf("%.*s\n", file_size_in_byte, xcode); 
    
    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.