|
| 1 | +# everything is comming from https://github.com/ly4k/SMBGhost |
| 2 | +# credit to @ly4k_ |
| 3 | +# module by : @r4vanan |
| 4 | +import socket |
| 5 | +import struct |
| 6 | + |
| 7 | +# Constants |
| 8 | +MAX_ATTEMPTS = 2000 # False negative chance: 0.04% |
| 9 | + |
| 10 | +# SMBGhost Packet |
| 11 | +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' |
| 12 | + |
| 13 | +class NXCModule: |
| 14 | + name = "smbghost" |
| 15 | + description = "Module to check for the SMB dialect 3.1.1 and compression capability of the host, which is an indicator for the SMBGhost vulnerability (CVE-2020-0796)." |
| 16 | + supported_protocols = ["smb"] |
| 17 | + opsec_safe = True |
| 18 | + multiple_hosts = True |
| 19 | + |
| 20 | + def __init__(self, context=None, module_options=None): |
| 21 | + self.context = context |
| 22 | + self.module_options = module_options |
| 23 | + |
| 24 | + def options(self, context, module_options): |
| 25 | + # Define options if needed |
| 26 | + pass |
| 27 | + |
| 28 | + def on_login(self, context, connection): |
| 29 | + self.context = context |
| 30 | + if self.perform_attack(connection.host): |
| 31 | + self.context.log.highlight("Potentially vulnerable to SMBGhost (CVE-2020-0796)") |
| 32 | + |
| 33 | + def perform_attack(self, target_ip): |
| 34 | + self.context.log.debug("Performing SMBGhost check...") |
| 35 | + try: |
| 36 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 37 | + sock.settimeout(5) |
| 38 | + sock.connect((target_ip, 445)) |
| 39 | + sock.send(SMBGHOST_PKT) |
| 40 | + |
| 41 | + # Receive the first 4 bytes for length |
| 42 | + nb_data = sock.recv(4) |
| 43 | + if len(nb_data) < 4: |
| 44 | + self.context.log.debug(f"{target_ip} Connection closed unexpectedly.") |
| 45 | + return False |
| 46 | + |
| 47 | + nb, = struct.unpack(">I", nb_data) |
| 48 | + res = sock.recv(nb) |
| 49 | + |
| 50 | + # Check response for vulnerability |
| 51 | + if res[68:70] == b"\x11\x03" and res[70:72] == b"\x02\x00": |
| 52 | + return True |
| 53 | + else: |
| 54 | + self.context.log.debug(f"{target_ip} Not vulnerable.") |
| 55 | + return False |
| 56 | + except Exception as e: |
| 57 | + self.context.log.fail(f"Error while connecting to host: {e}") |
| 58 | + return False |
0 commit comments