-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect.py
163 lines (139 loc) · 4.42 KB
/
collect.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
#!/usr/bin/python
# Imports
import random
import select
import pip
import sys
import os
#### SETTINGS ####
class Settings:
def __init__(self):
self.network = [
Gateway("gateway.example.com", [
Worker("worker1.example.com"),
Worker("worker2.example.com")
]),
Worker("worker.other.example.com")
]
self.username = "yourSSHusername"
self.remote_payload_folder = lambda domain, index: "/folder/on/remote/machine"
self.payload_ready_predicate = lambda domain, index: "test -f /folder/on/remote/machine/some_file"
self.delete_payload_after_download = True
self.local_dest_folder = "/folder/on/local/machine"
##################
def install_and_import(package):
import importlib
try:
importlib.import_module(package)
except ImportError:
import pip
pip.main(['install', package])
finally:
globals()[package] = importlib.import_module(package)
install_and_import('paramiko')
class Worker:
def __init__(self, domain):
self.domain = domain
self.gateway = None
pass
def collect(self, channel=None):
idx = self.get_index()
print "Connecting to worker "+str(idx)+": " + self.domain
session = paramiko.SSHClient()
session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if channel == None:
session.connect(self.domain, username=settings.username, compress = True)
else:
session.connect(self.domain, sock=channel, username=settings.username, compress = True)
print "Checking payload status"
(stdin, stdout, stderr) = session.exec_command(settings.payload_ready_predicate(self.domain, idx))
while not stdout.channel.exit_status_ready():
# Only print data if there is data to read in the channel
if stdout.channel.recv_ready():
rl, wl, xl = select.select([ stdout.channel ], [ ], [ ], 0.0)
if len(rl) > 0:
tmp = stdout.channel.recv(1024)
output = tmp.decode()
print output
if stderr.channel.recv_ready():
rl, wl, xl = select.select([ stderr.channel ], [ ], [ ], 0.0)
if len(rl) > 0:
tmp = stderr.channel.recv(1024)
output = tmp.decode()
print output
exitcode = stdout.channel.recv_exit_status()
if exitcode == 0:
print "✅ Payload ready, downloading"
ftp = session.open_sftp()
local_target_folder = os.path.join(settings.local_dest_folder, str(idx))
remote_folder = settings.remote_payload_folder(self.domain, idx)
ftp_download_folder(ftp, remote_folder, local_target_folder, settings.delete_payload_after_download)
ftp.close()
else:
print "Payload not ready yet"
print "Closing connection"
session.close()
def get_index(self):
i = 0
i_found = False
for device in settings.network:
if i_found:
break
if isinstance(device, Gateway):
if device == self.gateway:
for subdevice in device.workers:
if subdevice == self:
i_found = True
break
else:
i += 1
else:
i += len(device.workers)
else:
if device == self:
i_found = True
break
else:
i += 1
return i
class Gateway:
def __init__(self, domain, workers):
self.domain = domain
self.workers = workers
for worker in workers:
worker.gateway = self
pass
def collect(self):
# Open SSH session to main server
print "Connecting to gateway: " + self.domain
gateway = paramiko.SSHClient()
gateway.set_missing_host_key_policy(paramiko.AutoAddPolicy())
gateway.connect(self.domain, username=settings.username, compress=True)
for worker in self.workers:
subsession_channel = gateway.get_transport().open_channel('direct-tcpip', (worker.domain, 22), ('127.0.0.1', 0))
worker.collect(subsession_channel)
gateway.close()
settings = Settings()
def collect():
if not os.path.exists(settings.local_dest_folder):
os.makedirs(settings.local_dest_folder)
for device in settings.network:
device.collect()
def ftp_download_folder(ftp, remote_folder, local_folder, delete_after_download):
if not os.path.exists(local_folder):
os.makedirs(local_folder)
files = ftp.listdir(remote_folder)
for file in files:
remote_path = os.path.join(remote_folder, file)
local_path = os.path.join(local_folder, file)
is_directory = ftp.stat(remote_path).st_mode & 0040000
if is_directory:
ftp_download_folder(ftp, remote_path, local_path, delete_after_download)
else:
print " " + remote_path
ftp.get(remote_path, local_path)
if delete_after_download:
ftp.remove(remote_path)
if delete_after_download:
ftp.rmdir(remote_folder)
collect()