Home > Net >  Network byte order endianness conversion
Network byte order endianness conversion

Time:12-21

I have the following:

#define IPADDR "\xc0\x80\x10\x0a" /* 192.168.1.10 */
#define PORT "\x7a\x69" /* 31337 */

However I can't for the life of me figure out how the hex values would equal the ASCII values. How would I go about changing it to a different IP or port number?

Basically how do I get from the IP to the \xc0\x80\x10\x0a and vice versa?

In short, I am looking at http://shell-storm.org/shellcode/files/shellcode-857.php and would like to know how they arrived ad that long string

Thanks

CodePudding user response:

I'm still not sure exactly what your question is.

As to "how they got the code at the bottom" of https://shell-storm.org/shellcode/files/shellcode-857.php I doubt you'll get a great answer here. That whole site uses some serious black art. It is not a typical way of writing code at all. :-)

That program is a C program that embeds executable code into itself as a unsigned char code[] array, then it creates a pointer to function from the same address and calls the function. The #defines for IPADDR and PORT are concatenated into the code array literally. They are just string representations of the object code that was probably created with some other tool and generated in this form.

CodePudding user response:

There are standard functions nthos/htons/ntohl/htonl to convert 16- and 32-bit values (basically, port numbers and ipv4 addresses) between network and host byte order. So for example:

sockaddr_in  addr;

addr.sin_addr = htonl(INADDR_LOOPBACK);  // loopback address converted to network order
addr.sin_port = htons(31337);  // convert port to network order

CodePudding user response:

It's not about getting there, it's just a different way of representing the same thing:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define IPADDR "\xc0\x80\x10\x0a" /* not 192.168.1.10  */
                                  /* but 192.128.16.10 */

#define PORT "\x7a\x69"           /* not 31337 */
                                  /* but 27002 */

#define c2ui(a) ((unsigned int)(unsigned char)a)

int main()
{
    printf("\n%u.%u.%u.%u\n", c2ui(IPADDR[0]),
                              c2ui(IPADDR[1]),
                              c2ui(IPADDR[2]),
                              c2ui(IPADDR[3]));

    printf("\n\\xx\\xx\\xx\\xx\n", c2ui(IPADDR[0]),
                                               c2ui(IPADDR[1]),
                                               c2ui(IPADDR[2]),
                                               c2ui(IPADDR[3]));
    union
    {
        uint8_t s_port[3];
        uint16_t v_port;
    } v;
    memcpy(v.s_port, (uint8_t *)&PORT[0], sizeof(PORT));
    printf("\nport: %u\n", v.v_port);
}

Apart from the fact that there are errors in the descriptions that I have pointed out in the example code. In the case of PORT instead there is a problem because the fact that that number is correct depends on the type of system on which that program will run. On my system, for example, to get the value as in the description I would have had to invert the two characters. For this reason it is not a good idea to use that type of format for values. It would have been better if it had written:

#define PORT 31337                /* 31337 */
  • Related