1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
import subprocess
import threading
import tempfile
import os
import sys
import select
import signal
from sploit.log import log
class Comm:
def __init__(self, backend):
self.back = backend
def __del__(self):
for line in self.back.stdin:
log(line)
def read(self, size):
data = self.back.stdin.read(size)
log(data)
return data
def readline(self):
data = self.back.stdin.readline()
log(data)
return data
def readuntil(self,pred):
data = b''
while(not pred(data)):
data += self.back.stdin.read(1)
log(data)
return data
def readlineuntil(self,pred):
data = b''
while(not pred(data)):
data = self.back.stdin.readline()
log(data)
return data
def write(self, data):
self.back.stdout.write(data)
self.back.stdout.flush()
def writeline(self, data):
self.write(data + b'\n')
def interact(self):
print("<--Interact Mode-->")
syncstop = threading.Event()
def readloop():
poll = select.poll()
poll.register(self.back.stdin)
def readall():
while(True):
data = self.back.stdin.readline()
if(data == b''):
break
log(data)
while not syncstop.isSet():
readall()
dat = poll.poll(100)
if(len(dat)>0):
if(dat[0][1] & select.POLLIN):
readall()
else:
syncstop.set()
os.set_blocking(self.back.stdin.fileno(),False)
readthread = threading.Thread(target=readloop,daemon=True)
readthread.start()
stdin = sys.stdin.buffer
signal.signal(signal.SIGALRM,lambda: 0)
while not syncstop.isSet():
try:
signal.alarm(1)
data = stdin.readline()
if(data and not syncstop.isSet()):
self.write(data)
else:
break
except TypeError:
pass
except KeyboardInterrupt:
break
signal.alarm(0)
syncstop.set()
readthread.join()
os.set_blocking(self.back.stdin.fileno(),True)
print("<--Interact Mode Done-->")
class Process:
def __init__(self, args):
print(f"Running: {' '.join(args)}")
self.proc = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
preexec_fn=lambda : os.setpgrp())
print(f"PID: {self.proc.pid}")
self.stdin = self.proc.stdout
self.stdout = self.proc.stdin
def __del__(self):
if(self.proc.poll() != None):
return
try:
print("Waiting on Target Program to End...")
print("Press Ctrl+C to Forcefully Kill It...")
self.proc.wait()
except KeyboardInterrupt:
self.proc.kill()
class Pipes:
def __init__(self,tmp=None):
if(tmp == None):
self.dir = tempfile.TemporaryDirectory()
dirname = self.dir.name
else:
if(not os.path.exists(tmp)):
os.mkdir(tmp)
dirname = tmp
self.pathin = os.path.join(dirname,"in")
self.pathout = os.path.join(dirname,"out")
os.mkfifo(self.pathin)
os.mkfifo(self.pathout)
print("Waiting on Target to Connect...")
print("<"+self.pathin+" >"+self.pathout)
self.stdout = open(self.pathin,"wb")
self.stdin = open(self.pathout, "rb")
print("Connected!")
def __del__(self):
try:
if getattr(self,'stdout',None) : self.stdout.close()
if getattr(self,'stdin',None) : self.stdin.close()
except BrokenPipeError:
pass
if getattr(self,'pathin',None) and os.path.exists(self.pathin) : os.unlink(self.pathin)
if getattr(self,'pathout',None) and os.path.exists(self.pathout) : os.unlink(self.pathout)
|