Skip to content

Instantly share code, notes, and snippets.

@alpe12
Last active December 10, 2021 20:25
Show Gist options
  • Save alpe12/ee58362838192f5258b4bf62c2e53678 to your computer and use it in GitHub Desktop.
Save alpe12/ee58362838192f5258b4bf62c2e53678 to your computer and use it in GitHub Desktop.
TP-Link Archer C7 extract login stok token and cookie
#!/usr/bin/env python3
#run sudo apt install firefox-geckodriver && pip3 install selenium
ip = "192.168.1.254"
geckologfile = '/media/tmpfs/geckodriver.log'
password = "YOURPASSWORD"
tokenfile = "/media/tmpfs/token"
cookiesfile = "/media/tmpfs/stats.cookies"
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
import os.path
from subprocess import run, PIPE
import requests
ismain = (True if __name__ == "__main__" else False)
def tryclick(where, maxtries = 10):
tries = 0
while (tries < maxtries):
try:
where.click()
except:
sleep (1)
tries += 1
else:
tries = -1
break
if (tries != -1):
print("click failed")
raise ValueError
def waituntilcomplete (wait = 0):
global driver
if (wait > 0):
sleep(wait)
WebDriverWait(driver, 10).until(lambda d: driver.execute_script("return document.readyState") == "complete")
def attemptlogin():
try:
requests.head(f'http://{ip}/')
except:
return False
global driver
try:
options = Options()
options.headless = True
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.cache.disk.enable", False)
profile.set_preference("browser.tabs.remote.autostart", False)
profile.set_preference("dom.ipc.processCount", 1)
driver = webdriver.Firefox(options=options, service_log_path=geckologfile, firefox_profile = profile)
driver.get(f"http://{ip}/webpages/login.html")
waituntilcomplete()
driver.execute_script(f'document.getElementsByClassName("password-text")[6].value = "{password}";')
tryclick(driver.find_element_by_id('login-btn'))
waituntilcomplete(1)
try:
driver.find_element_by_id('user-conflict-msg-container')
except:
pass
else:
if not ismain:
print("user conflict")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "btn-msg-ok"))
)
finally:
driver.execute_script('document.querySelector(".btn-msg-ok.btn-prompt").click();')
waituntilcomplete(1)
token = driver.execute_script("return localStorage.getItem('token')")
if (len(token) != 32):
raise ValueError
driver.get(f"http://{ip}/cgi-bin/luci/")
waituntilcomplete()
cookie = driver.execute_script("return document.cookie")
if (len(cookie) != 40):
raise ValueError
if not ismain:
print('token=' + token)
print(cookie)
print("success")
with open(tokenfile, "w") as text_file:
text_file.write(token)
with open(cookiesfile, "w") as text_file:
text_file.write(f'{ip} FALSE /cgi-bin/luci FALSE 0 sysauth ' + cookie[8:] + "\n")
except:
driver.quit()
return False
else:
driver.quit()
return True
def smartsleep(sleepfor = 0, sleeptime = 15):
sleeptime = min(max(sleeptime,1),15)
while (sleepfor == 'forever' or sleepfor > 0):
if shutdown:
break
if not os.path.isfile(cookiesfile):
break
sleep(sleeptime)
if (sleepfor != 'forever'):
sleepfor -= sleeptime
shutdown = False
try:
if ismain:
while not shutdown:
if(attemptlogin() == True):
print("login ok")
//monitor "cookiesfile". if it gets deleted, login again.
smartsleep('forever')
else:
print("login fail")
sleep(60)
else:
attemptlogin()
except (KeyboardInterrupt, SystemExit):
try:
driver.quit()
except:
pass
shutdown = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment