wifipass.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. # curl --stderr - --trace - -v -L -A '' http://foobar.com | tee step0001
  3. # # -> the href in there, including the mac address
  4. # curl --stderr - --trace - -v -L -A '' -c kookie.jar "https://passman01.wifipass.org/w2p/login-url-real.php?id=BLABLA&domain=controleur.wifipass.org&mac=XX-XX-XX-XX-XX-XX&page=http%3A%2F%foobar.com%2F" | tee step0002
  5. # # -> the _token value in there
  6. # curl --stderr - --trace - -v -L -A '' -c kookie.jar 'https://passman01.wifipass.org/w2p/formulaire_fin.php?id=BLABLA&domain=controleur.wifipass.org' -F "registration[id1]=1" -F "registration[id2]=243" -F "registration[newsLetterType]=FOOFOO" -F "registration[email]=foo@bar.com" -F "_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -F "accept=true" --post301 --post302 --post303 | tee step0003
  7. # # took this path again, ended in a cycle:
  8. # # curl --stderr - --trace - -v -L -A '' -c kookie.jar 'https://passman01.wifipass.org/w2p/formulaire_fin.php?id=BLABLA&domain=controleur.wifipass.org' -F "username=ZZZZZZZZZZ" -F "password=WWWWWW" -F "_token=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" --post301 --post302 --post303 | tee step0004
  9. # # => a different token value, go figure:
  10. # curl --stderr - --trace - -v -L -A '' -c kookie.jar 'https://controleur.wifipass.org/goform/HtmlLoginRequest' -F "username=ZZZZZZZZZZ" -F "password=WWWWWW" -F "_token=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" --post301 --post302 --post303 | tee step0005
  11. import subprocess
  12. import re
  13. import lxml
  14. from lxml import html
  15. import urllib.request
  16. import urllib3
  17. headers = {}
  18. headers['User-Agent'] = 'Mozilla/5.0'
  19. pool = urllib3.PoolManager()
  20. dummy='google.com'
  21. f1 = pool.request(method='GET', url=dummy, headers=headers)
  22. print("%s", f1.getheaders())
  23. htmr = f1.data
  24. hdoc = html.document_fromstring(htmr)
  25. a = hdoc.xpath('//a[@href]')[0]
  26. href = a.attrib.get('href')
  27. f0 = pool.request(method='GET', url=href, headers=headers)
  28. print("%s", f0.getheaders())
  29. rd = f0.data
  30. hdoc2 = html.document_fromstring(rd)
  31. inputs = hdoc2.xpath('//input')
  32. kvs = [(elt.attrib.get('name'), elt.attrib.get('value')) for elt in inputs if elt.attrib.get('type') != 'checkbox']
  33. chk = [(elt.attrib.get('id'), elt.attrib.get('checked')) for elt in inputs if elt.attrib.get('type') == 'checkbox']
  34. print(kvs, chk)
  35. fields = dict(kvs + chk)
  36. rem = []
  37. for k in fields:
  38. if fields.get(k) == None:
  39. if re.search('email', k):
  40. # this appears to be required
  41. fields[k] = "foo@bar.com"
  42. else:
  43. rem += [k]
  44. # also works: fields[k] = '0'
  45. for k in rem:
  46. fields.pop(k)
  47. forms = hdoc2.xpath('//form')
  48. pr = urllib.request.urlparse(href)
  49. actions = [f.action for f in forms]
  50. newreqs = []
  51. for a in actions:
  52. pf = urllib.request.urlparse(a)
  53. if pf.scheme == '' and pf.netloc == '':
  54. newreq = pr.scheme + '://' + pr.netloc + '/' + \
  55. re.match('(.*?)/[^/]*', pr.path)[0] + '/' + \
  56. pf.path + '?' + pf.query
  57. newreqs += [newreq]
  58. else:
  59. newreqs += [pf.scheme + '://' + pf.netloc + pf.path]
  60. a2s = hdoc2.xpath('//a[@href]')
  61. loginx = [a2.attrib.get('href') for a2 in a2s][0]
  62. loginrequest = newreqs[0]
  63. print(loginrequest, fields)
  64. f3a = pool.request_encode_body(method='POST', url=newreqs[1], headers=headers, fields=fields)
  65. print(f3a.getheaders())
  66. print(f3a.data)
  67. hdoc3a = html.document_fromstring(f3a.data)
  68. inputs3a = hdoc3a.xpath('//input')
  69. kvs3a = [(elt.attrib.get('name'), elt.attrib.get('value')) for elt in inputs3a if elt.attrib.get('type') != 'checkbox']
  70. fields3a = dict(kvs3a)
  71. rem = []
  72. for k in fields3a:
  73. if fields3a.get(k) == None:
  74. rem += [k]
  75. for k in rem:
  76. fields3a.pop(k)
  77. f3 = pool.request_encode_body(method='POST', url=loginrequest, headers=headers, fields=fields3a)
  78. print(f3.getheaders())
  79. print(f3.data)
  80. # hdoc3 = html.document_fromstring(f3.data)
  81. # we don't need the contents, except optionally to make sure that it worked.
  82. # print(f3.data)