Skip to content

Instantly share code, notes, and snippets.

@nyx-rattapoom
Created August 1, 2019 19:11
Show Gist options
  • Save nyx-rattapoom/d36b9317694f805cae54d3533273de43 to your computer and use it in GitHub Desktop.
Save nyx-rattapoom/d36b9317694f805cae54d3533273de43 to your computer and use it in GitHub Desktop.
Mikrotik login
import logging
import re
from hashlib import md5
from os import getenv
import requests
logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s - %(message)s',
datefmt='%d-%b-%Y %H:%M:%S')
logger = logging.getLogger(__name__)
session = requests.Session()
session.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}
IP = getenv('IP')
USERNAME = getenv('USERNAME')
PASSWORD = getenv('PASSWORD')
url = 'http://' + IP + '/login'
def main():
response = session.get(url)
if is_logged_in(response.text):
logger.info("Logged in")
else:
doLogin()
def doLogin():
success = False
response = session.get('http://' + IP + '/login')
regex = re.search(r"hexMD5\(\'(.+?)\' \+ document.login.password.value \+ \'(.+?)\'\);", response.text)
if regex:
password_salt = bytes(regex.group(1) + PASSWORD + regex.group(2), 'utf-8').decode("unicode_escape")
hex_hash_password = md5(password_salt.encode('latin-1')).hexdigest()
payload = {'username': USERNAME, 'password': hex_hash_password, 'dst': '', 'popup': 'true'}
response = session.post(url, data=payload)
if is_logged_in(response.text):
success = True
if success:
logger.info("Login Success !")
else:
logger.error("Unable to login !!!")
def is_logged_in(html):
return 'You are logged in' in html
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment