|
| 1 | +# everything is comming from https://github.com/ly4k/SMBGhost |
| 2 | +# credit to @ly4k_ |
| 3 | +# module by : @r4vanan |
| 4 | +import sys |
| 5 | +import socket |
| 6 | +import struct |
| 7 | +from nxc.logger import nxc_logger |
| 8 | + |
| 9 | +# Constants |
| 10 | +MAX_ATTEMPTS = 2000 # False negative chance: 0.04% |
| 11 | + |
| 12 | +# SMBGhost Packet |
| 13 | +SMBGHOST_PKT = b'\x00\x00\x00\xc0\xfeSMB@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$\x00\x08\x00\x01\x00\x00\x00\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00x\x00\x00\x00\x02\x00\x00\x00\x02\x02\x10\x02"\x02$\x02\x00\x03\x02\x03\x10\x03\x11\x03\x00\x00\x00\x00\x01\x00&\x00\x00\x00\x00\x00\x01\x00 \x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\n\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00' |
| 14 | + |
| 15 | +class NXCModule: |
| 16 | + name = "smbghost" |
| 17 | + description = "Module to check if the machine is vulnerable to SmbGhost" |
| 18 | + supported_protocols = ["smb"] |
| 19 | + opsec_safe = True |
| 20 | + multiple_hosts = True |
| 21 | + |
| 22 | + def __init__(self, context=None, module_options=None): |
| 23 | + self.context = context |
| 24 | + self.module_options = module_options |
| 25 | + |
| 26 | + def options(self, context, module_options): |
| 27 | + # Define options if needed |
| 28 | + pass |
| 29 | + |
| 30 | + def on_login(self, context, connection): |
| 31 | + self.context = context |
| 32 | + if self.perform_attack(connection.host): |
| 33 | + self.context.log.highlight("VULNERABLE to SMBGhost (CVE-2020-0796)") |
| 34 | + |
| 35 | + def perform_attack(self, target_ip): |
| 36 | + self.context.log.debug("Performing SMBGhost check...") |
| 37 | + try: |
| 38 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 39 | + sock.settimeout(5) |
| 40 | + sock.connect((target_ip, 445)) |
| 41 | + sock.send(SMBGHOST_PKT) |
| 42 | + |
| 43 | + # Receive the first 4 bytes for length |
| 44 | + nb_data = sock.recv(4) |
| 45 | + if len(nb_data) < 4: |
| 46 | + self.context.log.debug(f"{target_ip} Connection closed unexpectedly.") |
| 47 | + return False |
| 48 | + |
| 49 | + nb, = struct.unpack(">I", nb_data) |
| 50 | + res = sock.recv(nb) |
| 51 | + |
| 52 | + # Check response for vulnerability |
| 53 | + if res[68:70] == b"\x11\x03" and res[70:72] == b"\x02\x00": |
| 54 | + return True |
| 55 | + else: |
| 56 | + self.context.log.debug(f"{target_ip} Not vulnerable.") |
| 57 | + return False |
| 58 | + except Exception as e: |
| 59 | + self.context.log.fail(f"Error while connecting to host: {e}") |
| 60 | + return False |
| 61 | + |
| 62 | +def fail(msg): |
| 63 | + nxc_logger.debug(msg) |
| 64 | + nxc_logger.fail("This might have been caused by invalid arguments or network issues.") |
| 65 | + sys.exit(2) |
0 commit comments