kdnakt blog

hello there.

M1 MacBook Proにpwntoolsをインストールする

以前参加したCTFで、他の人が書いたWriteupを見ていて、Pythonでpwn系の問題を解くのにpwntoolsというライブラリが便利そうだったのでインストールしてみた。

kdnakt.hatenablog.com

[エラーが出る]

環境はM1 MacBook Pro。公式サイトを見ながらインストールしていく。

pypi.org

が、インストールコマンドを実行してみるとエラー。

$ python3 -m pip install --upgrade pwntools
Collecting pwntools
  Obtaining dependency information for pwntools from https://files.pythonhosted.org/packages/f0/e2/c6ea911a06994dac94ae868b15550b0d1b4b5b6326ddfd947280aeef682b/pwntools-4.10.0-py2.py3-none-any.whl.metadata
  Downloading pwntools-4.10.0-py2.py3-none-any.whl.metadata (5.5 kB)
(略)
Building wheels for collected packages: unicorn
  Building wheel for unicorn (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [1 lines of output]
      error: [Errno 2] No such file or directory: 'cmake'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for unicorn
  Running setup.py clean for unicorn
Failed to build unicorn
ERROR: Could not build wheels for unicorn, which is required to install pyproject.toml-based projects

[依存関係を解消]

どうやらunicornというライブラリのインストールに失敗するらしい。ググってみると、unicornだけインストールすれば解消するとの記事が。

新款MBP下的Python3安装pwntools包报错问题解决_preparing metadata (setup.py) ... done_卡尔哥哥好棒的博客-CSDN博客

試しにunicornだけインストールしようとしても、同じエラーが起きた。

$ python3 -m pip install --upgrade unicorn 
Collecting unicorn
  Using cached unicorn-2.0.1.post1.tar.gz (2.8 MB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: unicorn
  Building wheel for unicorn (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [1 lines of output]
      error: [Errno 2] No such file or directory: 'cmake'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for unicorn
  Running setup.py clean for unicorn
Failed to build unicorn
ERROR: Could not build wheels for unicorn, which is required to install pyproject.toml-based projects

[Errno 2] No such file or directory: 'cmake'とあるので、cmakeをインストールすればいけそう?と思い調べてみるとそれっぽいイシューが。

github.com

というわけで、cmakeをインストールしてみる。

$ brew install cmake
==> Downloading https://formulae.brew.sh/api/formula.jws.json
##O=-#     #                                                                                                                                                                         
==> Downloading https://formulae.brew.sh/api/cask.jws.json
##O=-#     #                                                                                                                                                                         
Warning: Treating cmake as a formula. For the cask, use homebrew/cask/cmake
==> Fetching cmake
==> Downloading https://ghcr.io/v2/homebrew/core/cmake/manifests/3.27.0
(略)
==> Summary
🍺  /opt/homebrew/Cellar/cmake/3.27.0: 3,284 files, 56.1MB
==> Running `brew cleanup cmake`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

無事インストール成功。unicorn単体のインストールを試してみる。

% python3 -m pip install unicorn
Collecting unicorn
  Using cached unicorn-2.0.1.post1.tar.gz (2.8 MB)
  Preparing metadata (setup.py) ... done
(略)
Successfully built unicorn
Installing collected packages: unicorn
Successfully installed unicorn-2.0.1.post1

こちらも無事成功。改めてpwntoolsをインストールする。

% python3 -m pip install --upgrade pwntools
Collecting pwntools
  Obtaining dependency information for pwntools from https://files.pythonhosted.org/packages/f0/e2/c6ea911a06994dac94ae868b15550b0d1b4b5b6326ddfd947280aeef682b/pwntools-4.10.0-py2.py3-none-any.whl.metadata
(略)
Installing collected packages: sortedcontainers, pyserial, pyelftools, urllib3, six, pysocks, pygments, pycparser, psutil, plumbum, packaging, MarkupSafe, intervaltree, idna, charset-normalizer, certifi, capstone, bcrypt, rpyc, ropgadget, requests, python-dateutil, mako, colored-traceback, cffi, pynacl, cryptography, paramiko, pwntools
Successfully installed MarkupSafe-2.1.3 bcrypt-4.0.1 capstone-5.0.0.post1 certifi-2023.7.22 cffi-1.15.1 charset-normalizer-3.2.0 colored-traceback-0.3.0 cryptography-41.0.2 idna-3.4 intervaltree-3.1.0 mako-1.2.4 packaging-23.1 paramiko-3.2.0 plumbum-1.8.2 psutil-5.9.5 pwntools-4.10.0 pycparser-2.21 pyelftools-0.29 pygments-2.15.1 pynacl-1.5.0 pyserial-3.5 pysocks-1.7.1 python-dateutil-2.8.2 requests-2.31.0 ropgadget-7.3 rpyc-5.3.1 six-1.16.0 sortedcontainers-2.4.0 urllib3-2.0.4

無事pwntoolsをインストールできた。

[使ってみる]

pwntoolsをインストールできたので、早速動かしてみる。

# solve.py
from pwn import *

context.arch = 'amd64'
# サーバ名は適当...
io = remote('challenge.example.com', 1337)

# バイナリやソースから推測できた値を送信
a = b'A' * 68
a += pack(0x40117a)
io.sendline(a)

$ python3 solve.pyのように実行すると、フラグを出力してくれるはず...だったのだが、自分の理解が足りないのかうまくいかず。無念...

[まとめ]

  • M1 MacBook Proにpwntoolsをインストールした
  • 依存関係としてcmakeをインストールしておく必要があった
  • 早速実践投入してみたが問題は解けなかった