Login to my D-link ADSL Router with a Python script

  Kiến thức lập trình

I’m trying to access the router’s admin page programmatically to read and modify some of parameters like the wireless pre-shared key with python but the problem is in the GET part, see after it POSTs the credentials it redirects me to the login page again.

import requests
    def login_to_router(username, password, router_ip):
    # Define the URL of the login page
    login_url = f'http://{router_ip}/cgi-bin/webproc'

    # Define the payload (username and password)
    payload = {
        'getpage': 'html/index.html',
        'errorpage': 'html/main.html',
        'var:menu': 'setup',
        'var:page': 'wizard',
        'obj-action': 'auth',
        ':username': username,
        ':password': password,
        ':action': 'login',
        ':sessionid': '4255ef3a'
    }

    # Define the custom headers
    headers = {
        'Host': router_ip,
        'Connection': 'keep-alive',
        'Content-Length': str(len(payload)),
        'Cache-Control': 'max-age=0',
        'Upgrade-Insecure-Requests': '1',
        'Origin': f'http://{router_ip}',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Referer': f'http://{router_ip}/cgi-bin/webproc',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'en-US,en;q=0.9,fr;q=0.8,ar;q=0.7',
        'Cookie': 'language=en_us; sys_UserName=admin; sessionid=1f915a6a'
    }

    # Send the POST request
    session = requests.Session()
    response = session.post(login_url, data=payload, headers=headers,allow_redirects = True)
    #print(response.url)#print
    # Check if login was successful
    if 'Login failed' in response.text:
        print("Login failed")
        return None
    else:
        print("Login successful")
        return session

def send_get_request(session, router_ip):
    # Define the URL of the target page
    target_url = f'http://{router_ip}/cgi-bin/webproc?getpage=html/index.html&errorpage=html/main.html&var:language=en_us&var:menu=setup&var:page=wizard'

    # Define the custom headers
    headers = {
        'Host': router_ip,
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Referer': f'http://{router_ip}/cgi-bin/webproc',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'en-US,en;q=0.9,fr;q=0.8,ar;q=0.7',
        'Cookie': 'sessionid=1f915a6a; language=en_us; sys_UserName=admin; sessionid=1f915a6a'
    }

    # Send the GET request
    response = session.get(target_url, headers=headers,allow_redirects = True)
    
    # Print the response content
    print(response.text)

router_ip = '192.168.1.1'
username = 'admin'
password = 'admin'

# Login to the router
session = login_to_router(username, password, router_ip)
if session:
    # Send the GET request
    send_get_request(session, router_ip)

This was the http POST and GET when i captured the network packets:Http Post request sent from the python script
Http Get request sent from the python script

LEAVE A COMMENT