【pwnable.kr】 mistake - 运算符优先级

本文深入探讨了一个有趣的编程挑战,通过分析源码中的运算符优先级问题,揭示了如何利用反编译代码理解并解决程序中的错误。文章详细解释了在特定条件下,如何正确输入密码以触发程序输出flag,同时指出了源代码中存在的优先级错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这关很有意思,题目本意是一道源码中运算符优先级导致的问题,如果直接看binary的话,看反编译代码就不会被运算符优先级迷惑。

先看一下反编译代码,main函数。程序判断/home/mistake/password能否打开,再没有别的文件打开的情况下open一般返回的fd是3。之后从stdin read 10byte,再用scanf读取10byte 进行xor运算,之后两者比较,如果相等就输出flag。

IDA pseudocode:

int __cdecl main(int argc, const char **argv, const char **envp)
{
  int result; // eax
  int v4; // eax
  char buf; // [rsp+20h] [rbp-30h]
  char s2; // [rsp+30h] [rbp-20h]
  unsigned __int64 v7; // [rsp+48h] [rbp-8h]

  v7 = __readfsqword(0x28u);
  if ( open("/home/mistake/password", 0) >= 0 )
  {
    puts("do not bruteforce...");
    v4 = time(0LL);
    sleep(v4 % 20);
    if ( (signed int)read(0, &buf, 0xAuLL) > 0 )
    {
      printf("input password : ");
      __isoc99_scanf("%10s", &s2);
      xor((__int64)&s2, 10);
      if ( !strncmp(&buf, &s2, 0xAuLL) )
      {
        puts("Password OK");
        system("/bin/cat flag\n");
      }
      else
      {
        puts("Wrong Password");
      }
      close(0);
      result = 0;
    }
    else
    {
      puts("read error");
      close(0);
      result = 0;
    }
  }
  else
  {
    printf("can't open password %d\n");
    result = 0;
  }
  return result;
}

那么输入1111111111,0000000000即可。

再看一下题目源代码,发现了mistake。fd=open("/home/mistake/password",O_RDONLY,0400) < 0中,判断的优先级要比赋值的高,open返回值是3,那么fd的值就为假,也就是0了。

Last login: Wed May 28 22:57:11 on ttys000 /Users/wangxuguang/.zshrc:3: no such file or directory: /usr/local/bin/brew /Users/wangxuguang/.zshrc:export:3: not valid in this context: wangxuguang@MacBookPro ~ % pipx install enex2notion zsh: command not found: pipx wangxuguang@MacBookPro ~ % pip Usage: pip <command> [options] Commands: install Install packages. lock Generate a lock file. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. inspect Inspect the python environment. list List installed packages. show Show information about installed packages. check Verify installed packages have compatible dependencies. config Manage local and global configuration. search Search PyPI for packages. cache Inspect and manage pip's wheel cache. index Inspect information available from package indexes. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion. debug Show information useful for debugging. help Show help for commands. General Options: -h, --help Show help. --debug Let unhandled exceptions propagate outside the main subroutine, instead of logging them to stderr. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. --require-virtualenv Allow pip to only run in a virtual environment; exit with an error otherwise. --python <python> Run pip with the specified Python interpreter. -v, --verbose Give more output. Option is additive, and can be used up to 3 times. -V, --version Show version and exit. -q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels). --log <path> Path to a verbose appending log. --no-input Disable prompting for input. --keyring-provider <keyring_provider> Enable the credential lookup via the keyring library if user input is allowed. Specify which mechanism to use [auto, disabled, import, subprocess]. (default: auto) --proxy <proxy> Specify a proxy in the form scheme://[user:passwd@]proxy.server:port. --retries <retries> Maximum attempts to establish a new HTTP connection. (default: 5) --timeout <sec> Set the socket timeout (default 15 seconds). --exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort. --trusted-host <hostname> Mark this host or host:port pair as trusted, even though it does not have valid or any HTTPS. --cert <path> Path to PEM-encoded CA certificate bundle. If provided, overrides the default. See 'SSL Certificate Verification' in pip documentation for more information. --client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. --cache-dir <dir> Store the cache data in <dir>. --no-cache-dir Disable the cache. --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index. --no-color Suppress colored output. --use-feature <feature> Enable new functionality, that may be backward incompatible. --use-deprecated <feature> Enable deprecated functionality, that will be removed in the future. --resume-retries <resume_retries> Maximum attempts to resume or restart an incomplete download. (default: 0) wangxuguang@MacBookPro ~ % pip3 install enex2notion error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try brew install xyz, where xyz is the package you are trying to install. If you wish to install a Python library that isn't in Homebrew, use a virtual environment: python3 -m venv path/to/venv source path/to/venv/bin/activate python3 -m pip install xyz If you wish to install a Python application that isn't in Homebrew, it may be easiest to use 'pipx install xyz', which will manage a virtual environment for you. You can install pipx with brew install pipx You may restore the old behavior of pip by passing the '--break-system-packages' flag to pip, or by adding 'break-system-packages = true' to your pip.conf file. The latter will permanently disable this error. If you disable this error, we STRONGLY recommend that you additionally pass the '--user' flag to pip, or set 'user = true' in your pip.conf file. Failure to do this can result in a broken Homebrew installation. Read more about this behavior here: <https://peps.python.org/pep-0668/> note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification. wangxuguang@MacBookPro ~ %
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值