Created
December 31, 2020 02:00
-
-
Save rossdavidsmith/7ff9beff53f65e430aee04353706a68b to your computer and use it in GitHub Desktop.
A quick script to login to a Virgin router and download logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
import hashlib | |
router_uri = 'https://192.168.1.1' | |
password = "abcd1234" | |
def hash(cleartext): | |
m = hashlib.md5() | |
m.update(cleartext.encode('utf-8')) | |
return m.hexdigest() | |
def load_login_page(session, router_uri): | |
login_page_response = session.get(router_uri, verify=False) | |
return login_page_response | |
def extract_dm_cookie(html): | |
pattern = r"var dm_cookie='(.*?)'" | |
match = re.search(pattern, html, re.MULTILINE) | |
return match[1] | |
def authenticate(session, router_uri, dm_cookie, password, auth_key): | |
username = 'vodafone' | |
password_hash = hash(password + auth_key) | |
request_text = f''' | |
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> | |
<soapenv:Header> | |
<DMCookie>{dm_cookie}</DMCookie> | |
</soapenv:Header> | |
<soapenv:Body> | |
<cwmp:Login xmlns=""> | |
<ParameterList> | |
<Username>{username}</Username> | |
<Password>{password_hash}</Password> | |
<AllowRelogin>0</AllowRelogin> | |
</ParameterList> | |
</cwmp:Login> | |
</soapenv:Body> | |
</soapenv:Envelope>''' | |
login_response = session.post( | |
f'{router_uri}/data_model.cgi', | |
data = request_text, | |
headers = { | |
'Content-Type': 'text/xml; charset="utf-8"', | |
'SOAPAction': 'cwmp:Login', | |
'SOAPServer': '' | |
}, | |
verify = False | |
) | |
return login_response | |
def logout(session, dm_cookie): | |
request_text = f''' | |
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> | |
<soapenv:Header> | |
<DMCookie>{dm_cookie}</DMCookie> | |
</soapenv:Header> | |
<soapenv:Body> | |
<cwmp:Logout xmlns=""></cwmp:Logout> | |
</soapenv:Body> | |
</soapenv:Envelope>''' | |
logout_response = session.post( | |
'https://192.168.1.1/data_model.cgi', | |
data = request_text, | |
headers = { | |
'Content-Type': 'text/xml; charset="utf-8"', | |
'SOAPAction': 'cwmp:Logout', | |
'SOAPServer': '' | |
}, | |
verify = False | |
) | |
return logout_response | |
def get_log(session): | |
response = session.post( | |
url='https://192.168.1.1/main.cgi', | |
data={ | |
'action': 'getsyslog', | |
'buf_idx': '6', | |
'lang_id':'uk_en', | |
'skipped_idx': '' }, | |
verify=False) | |
if response.headers['Content-Type'] != 'text/plain': | |
return None | |
return response.text | |
def login(session, router_uri, password): | |
login_page_response = load_login_page(session, router_uri) | |
dm_cookie = extract_dm_cookie(login_page_response.text) | |
config_uri = f'{router_uri}/main.cgi?js=rg_config.js' | |
config_response = session.get(config_uri, verify=False) | |
pattern = r"var auth_key = '(\d+)'" | |
match = re.search(pattern, config_response.text, re.MULTILINE) | |
auth_key = match[1] | |
authenticate(session, router_uri, dm_cookie, password, auth_key) | |
return dm_cookie | |
def main(): | |
session = requests.Session() | |
dm_cookie = login(session, router_uri, password) | |
log = get_log(session) | |
logout(session, dm_cookie) | |
from pprint import pprint | |
pprint(log) | |
log_lines = log.split('\r\n') | |
print(len(log_lines)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment