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
143
144
145
146
147
148
149
150
151
152
153
154
155
  | 
import subprocess
import tempfile
import os
import sys
import select
from sploit.log import *
from sploit.until import bind
class Comm:
    logonread = True
    logonwrite = False
    flushonwrite = True
    def __init__(self, backend):
        self.back = backend
    def read(self, size):
        data = os.read(self.back.stdin.fileno(), size)
        if(data == b''):
            raise BrokenPipeError('Tried to read on broken pipe')
        if self.logonread : ilog(data, file=sys.stdout, color=NORMAL)
        return data
    def readline(self):
        data = self.back.stdin.readline()
        if(data == b''):
            raise BrokenPipeError('Tried to read on broken pipe')
        if self.logonread : ilog(data, file=sys.stdout, color=NORMAL)
        return data
    def readall(self):
        data = b''
        for line in self.back.stdin:
            if self.logonread : ilog(line, file=sys.stdout, color=NORMAL)
            data += line
        return data
    def readuntil(self, pred, /, *args, **kwargs):
        data = b''
        pred = bind(pred, *args, **kwargs)
        l = self.logonread
        self.logonread = False
        while(True):
            data += self.read(1)
            if(pred(data)):
                break
        self.logonread = l
        if self.logonread : ilog(data, file=sys.stdout, color=NORMAL)
        return data
    def readlineuntil(self, pred, /, *args, **kwargs):
        dataarr = []
        pred = bind(pred, *args, **kwargs)
        while(True):
            dataarr.append(self.readline())
            if(pred(dataarr)):
                break
        return dataarr
    def write(self, data):
        self.back.stdout.write(data)
        if self.flushonwrite : self.back.stdout.flush()
        if self.logonwrite : ilog(data, file=sys.stdout, color=ALT)
    def writeline(self, data):
        self.write(data + b'\n')
    def interact(self):
        ilog("<--Interact Mode-->")
        stdin = sys.stdin.buffer
        os.set_blocking(self.back.stdin.fileno(), False)
        os.set_blocking(stdin.fileno(), False)
        poll = select.poll()
        poll.register(self.back.stdin, select.POLLIN)
        poll.register(stdin, select.POLLIN)
        brk = False
        def readall(read, write):
            while(True):
                data = read()
                if(data == b''):
                    break
                write(data)
        def writeinput(write):
            ilog(write, file=sys.stdout, color=NORMAL)
        readtable = {
                stdin.fileno() : lambda : readall(stdin.readline, self.write),
                self.back.stdin.fileno() : lambda : readall(self.back.stdin.readline, writeinput)
        }
        readtable[self.back.stdin.fileno()]()
        while(not brk):
            try:
                ioevents = poll.poll(100)
                for ev in ioevents:
                    if(ev[1] & select.POLLIN):
                        readtable[ev[0]]()
                    else:
                        brk = True
                        break
            except KeyboardInterrupt:
                break
        os.set_blocking(self.back.stdin.fileno(), True)
        os.set_blocking(stdin.fileno(), True)
        ilog("<--Interact Mode Done-->")
class Process:
    def __init__(self, args):
        ilog(f"Running: {' '.join(args)}")
        self.proc = subprocess.Popen(args,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                preexec_fn=lambda : os.setpgrp())
        ilog(f"PID: {self.proc.pid}")
        self.stdin = self.proc.stdout
        self.stdout = self.proc.stdin
    def __del__(self):
        if getattr(self, 'proc', None) == None : return
        if(self.proc.poll() != None):
            return
        try:
            ilog("Waiting on Target Program to End...")
            ilog("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)
        ilog("Waiting on Target to Connect...", file=sys.stdout)
        ilog(f"<{self.pathin} >{self.pathout}", file=sys.stdout)
        self.stdout = open(self.pathin, "wb")
        self.stdin = open(self.pathout, "rb")
        ilog("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)
  |