Skip to content

Instantly share code, notes, and snippets.

@r4j0x00
Created January 17, 2021 00:31
Show Gist options
  • Save r4j0x00/242bf23051c850eba6cf3bf6539c0c53 to your computer and use it in GitHub Desktop.
Save r4j0x00/242bf23051c850eba6cf3bf6539c0c53 to your computer and use it in GitHub Desktop.
pwntools like process with no dependencies
from os import pipe, fork, dup2, execve, close, read, write
import sys
import threading
class process:
def __init__(self, path, env={}):
self.inp = input
if sys.version[0] == '2': self.inp = raw_input
self.version = int(sys.version[0])
self.path = path
if type(self.path) == str:
self.path = [self.path]
self.env = env
self.inpipefd = pipe()
self.outpipefd = pipe()
pid = fork()
if not pid:
dup2(self.outpipefd[0], 0)
dup2(self.inpipefd[1], 1)
dup2(self.inpipefd[1], 2)
execve(self.path[0], self.path, self.env)
exit(0)
close(self.inpipefd[1])
close(self.outpipefd[0])
self.stdin = self.outpipefd[1]
self.stdout = self.inpipefd[0]
self.pid = pid
def recv(self, size):
return read(self.stdout, size)
def send(self, buf):
if self.version == 3:
if type(buf) == str: buf = buf.encode()
return write(self.stdin, buf)
def recvuntil(self, _str):
s = b''
if self.version == 3:
if type(_str) == str: _str = _str.encode()
while not s.endswith(_str):
s += self.recv(1)
return s
def recvline(self):
return self.recvuntil(b'\n')
def sendline(self, buf):
if self.version == 3:
if type(buf) == str: buf = buf.encode()
return self.send(buf + b'\n')
def recvloop(self):
while True:
sys.stdout.write(self.recv(1024))
sys.stdout.flush()
def interactive(self):
t = threading.Thread(target=self.recvloop)
t.daemon = True
t.start()
while True:
self.sendline(self.inp('$ '))
# Usage
# p = process('/tmp/prog')
# print p.recvuntil('>> \n')
# p.sendline('1')
# p.interactive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment