运行存储在动态允许的内存中的shellcode

时间:2015-11-20 15:01:24

标签: c windows memory shellcode

我有以下几个运行shellcode工作正常:

    unsigned char original[] =
            "\xd9\xee\xd9\x74\x24\xf4\x58\xbb\xa6\xfb\x51\x8f\x33\xc9\xb1"
            "\x62\x83\xe8\xfc\x31\x58\x16\x03\x58\x16\xe2\x53\x07\xb9\x0d"
            "\x9b\xf8\x3a\x72\x12\x1d\x0b\xb2\x40\x55\x3c\x02\x03\x3b\xb1"
            "\xe9\x41\xa8\x42\x9f\x4d\xdf\xe3\x2a\xab\xee\xf4\x07\x8f\x71"      
            ;
    void *exec = VirtualAlloc(0, sizeof original, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, original, sizeof original);
    ((void(*)())exec)();

当我尝试运行存储在2个数组中的相同shellcode时,我遇到了访问冲突:

unsigned char part1[] =
        "\xd9\xee\xd9\x74\x24\xf4\x58\xbb\xa6\xfb\x51\x8f\x33\xc9\xb1"
        "\x62\x83\xe8\xfc\x31\x58\x16\x03\x58\x16\xe2\x53\x07\xb9\x0d"
        ;
    unsigned char part2[] = "\x9b\xf8\x3a\x72\x12\x1d\x0b\xb2\x40\x55\x3c\x02\x03\x3b\xb1"
        "\xe9\x41\xa8\x42\x9f\x4d\xdf\xe3\x2a\xab\xee\xf4\x07\x8f\x71";
//build the final shellcode array   
unsigned char * concatenation = (unsigned char*)malloc(sizeof (part1)+sizeof(part2)+1);
    //concatenation
    memcpy(concatenation, part1, sizeof part1);
    memcpy(concatenation + sizeof part1 , part2, sizeof part2);
//allocationg memory and running it
    void *exec = VirtualAlloc(0, sizeof concatenation, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, concatenation, sizeof concatenation);
    ((void(*)())exec)();

我试图让第二个例子有效但我遇到了访问冲突错误。 我究竟做错了什么 ? 感谢。

更新

这是修改后的代码跟随alain和Colonel Thirty Two的建议,我现在得到以下错误:" test.exe触发了一个breakpoin t"

unsigned char part1[] =
            "\xd9\xee\xd9\x74\x24\xf4\x58\xbb\xa6\xfb\x51\x8f\x33\xc9\xb1"
            "\x62\x83\xe8\xfc\x31\x58\x16\x03\x58\x16\xe2\x53\x07\xb9\x0d"
            ;
        unsigned char part2[] = "\x9b\xf8\x3a\x72\x12\x1d\x0b\xb2\x40\x55\x3c\x02\x03\x3b\xb1"
            "\xe9\x41\xa8\x42\x9f\x4d\xdf\xe3\x2a\xab\xee\xf4\x07\x8f\x71";
unsigned char * concatenation = (unsigned char*)malloc(sizeof (part1)+sizeof(part2));

    memcpy(concatenation, part1-1, sizeof part1);
    memcpy(concatenation + sizeof part1 , part2, sizeof part2);
    printf("%d", sizeof(original));
    void *exec = VirtualAlloc(0, sizeof (*concatenation), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, concatenation, sizeof(*concatenation));
    ((void(*)())exec)();

工作代码:

unsigned char * concatenation = (unsigned char*)malloc(sizeof (part1)+sizeof(part2));

    memcpy(concatenation, part1, sizeof part1);
    memcpy(concatenation + sizeof part1-1, part2, sizeof part2);

    void *exec = VirtualAlloc(0, sizeof(part1) + sizeof(part2), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, concatenation, sizeof(part1)+sizeof(part2));
    ((void(*)())exec)();

1 个答案:

答案 0 :(得分:1)

字符串文字以nul结尾,终止的nul字节由sizeof计数。因此,当使用2阵列版本时,在最终数组的中间有一个空字节。

如果你改变了

memcpy(concatenation + sizeof part1 , part2, sizeof part2);

memcpy(concatenation + sizeof part1 - 1, part2, sizeof part2);

我认为它应该有用。

上校Thirty Two指出,sizeof concatenation也有错误。