Search code examples
c++stringshellcode

How to get length of shellcode string containing NULL characters


I have string of shellcode with NULL characters in between them and i am unable to detect its length, i tried std::string.length() method but it only counts till NULL character after that it doesn't count.

Here is sample code .

   std::string shell_str = "\x55\x48\x89\x00\x00\x00\x00\xC3\x90";
   std::cout << "shell : " << shell_str << std::endl;     
   std::cout << "shell length : " << shell_str.length() << std::endl;

Output : shell : UH� shell length : 3

But length of string is 9 and i tried to copy it to vector also but still doesn't get the desired output .

Full code snippet is posted here


Solution

  • The problem isn't with the calculation of the length of shell_str, the problem is with the literal string you use to initialize shell_str. The constructor of std::string will stop at the "terminator".

    You need to use another std::string constructor, to explicitly tell it the actual length of the string:

    std::string shell_str("\x55\x48\x89\x00\x00\x00\x00\xC3\x90", 9);
    

    Also, since the "string" contains arbitrary data, you can't print it as a string either.

    And if you want a "string" of arbitrary bytes I suggest you use std::vector<uint8_t> instead.