执行 argc=0 的进程

发布于 2024-12-15 00:22:19 字数 117 浏览 7 评论 0原文

是否可以执行 argc = 0 的进程?我需要执行一个程序,但它的 argc 等于 0 非常重要。有没有办法做到这一点? 我尝试在命令行中放入 2^32 个参数,这样看起来就好像 argc = 0 但参数数量有最大限制。

Is it possible to execute a process whose argc = 0? I need to execute a program but it is extremely important for its argc to be equal to 0. Is there a way to do that?
I tried to put 2^32 arguments in the command line so that it appears as if argc = 0 but there is a maximum limit to the number of arguments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东北女汉子 2024-12-22 00:22:19

你可以编写一个直接调用 exec 的程序;它允许您指定命令行参数(包括程序名称)和缺少命令行参数。

You can write a program that calls exec directly; that allows you to specify the command-line arguments (including the program name) and lack thereof.

梦罢 2024-12-22 00:22:19

您可以使用 Linux 系统调用execve()

int execve(const char *filename, char *const argv[], char *const envp[]);

您可以传递可执行文件的文件名和一个空指针作为argv[]来执行二进制文件,并且argc将为零。

这是我的测试代码:

#include <stdio.h>
#include <unistd.h>

int main(void) {
    char *argv[] = { NULL };
    execv("./target", argv);
    // execv is correct, look up https://linux.die.net/man/3/execv
    return 0;
}

strace 结果是:

execve("./target", [], [/* 20 vars */]) = 0

您可以使用 envp[] 来传递您定义的参数。

此外,您可以使用汇编语言来达到您的目标(argc == 0 但您仍然需要传递参数)。我假设您使用的是 32 位 x86 环境。

其概念是:

  • 0x0b($SYS_execve)存储到%eax中,
  • argv[]的地址存储到%ebx中 将
  • envp[]的地址放入%ecx
  • ,然后使用int 0x80进行系统调用

内存结构如下图所示

+--------------------------------------------------+     
|               +----------------------------------|-----+
v               v               v------------------|-----|-----+
[arg_0][\0][...][arg_1][\0][...][arg_2][\0][...][ptr0][ptr1][ptr2][\0]
                                                ^
                                                |   (argv[] = NULL)
                                                +--- envp

:我想知道你是否在做该课程的实验作业由 Taesoo Kim 教授(GATech)提供。
课程链接:https://tc.gtisc.gatech.edu/cs6265

或者是黑客CTF(接旗竞赛)问题?

You may use linux system call execve().

int execve(const char *filename, char *const argv[], char *const envp[]);

You may pass the filename of executable and a null pointer as the argv[] to execute the binary and the argc will be zero.

It is my test code:

#include <stdio.h>
#include <unistd.h>

int main(void) {
    char *argv[] = { NULL };
    execv("./target", argv);
    // execv is correct, look up https://linux.die.net/man/3/execv
    return 0;
}

And the strace result is:

execve("./target", [], [/* 20 vars */]) = 0

You could use envp[] to pass the arguments you defined anyways.

Furthermore, you could use assembly language to reach your goal (argc == 0 but you still need to pass arguments). I assume that you are using a 32-bits x86 environment.

The concept is that:

  • store 0x0b ($SYS_execve) into %eax
  • put the address of argv[] into %ebx
  • put the address of envp[] into %ecx
  • then use int 0x80 to do a system call

The memory structure is shown below:

+--------------------------------------------------+     
|               +----------------------------------|-----+
v               v               v------------------|-----|-----+
[arg_0][\0][...][arg_1][\0][...][arg_2][\0][...][ptr0][ptr1][ptr2][\0]
                                                ^
                                                |   (argv[] = NULL)
                                                +--- envp

I am wondering that if you were doing the lab assignment of the course provided by Prof. Taesoo Kim (GATech).
Course Link: https://tc.gtisc.gatech.edu/cs6265

Or is it a hacker CTF (catch-the-flag contest) problem?

雨落星ぅ辰 2024-12-22 00:22:19

您可以编写一个 C 程序,在没有 argv 的情况下生成/执行另一个程序,例如:

#include <spawn.h>
#include <stdlib.h>

int main(int argc, char** argv, char** envp)
{
    pid_t pid;
    char* zero_argv[] = {NULL};
    posix_spawn(&pid, "./that_app", NULL, NULL, zero_argv, envp);

    int status;
    waitpid(&pid, &status, NULL);
    return 0;
}

You could write a C program that spawns/execs the other program with no argv, like:

#include <spawn.h>
#include <stdlib.h>

int main(int argc, char** argv, char** envp)
{
    pid_t pid;
    char* zero_argv[] = {NULL};
    posix_spawn(&pid, "./that_app", NULL, NULL, zero_argv, envp);

    int status;
    waitpid(&pid, &status, NULL);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文