Skip to content

Instantly share code, notes, and snippets.

@cauefcr
Forked from magnuspedro/loginUTFPR.py
Last active June 17, 2016 11:26
Show Gist options
  • Save cauefcr/6b58de82833f6554d369 to your computer and use it in GitHub Desktop.
Save cauefcr/6b58de82833f6554d369 to your computer and use it in GitHub Desktop.
Auto Login UTFPR
#!/usr/bin/python3
import requests,re,getpass,sys,base64,logging
logging.captureWarnings(True)
requests.packages.urllib3.disable_warnings()
url_utf = "https://1.1.1.1/login.html"
def login(ra,senha):
global dbg
try:
payload = {'buttonClicked':'4','err_flag':'0','err_msg':'','info_flag':0,'info_msg':'','redirect_url':'','username':ra,'password':senha}
headers = {'Referer': url_utf}
response = requests.post(url_utf, data=payload, headers=headers,verify=False)
data = response.text
result = open("result.html",'w')
if dbg == True:
result.write(data)
if re.search("Login Successful",data):
print ("\033[1;32mLogin \033[0m\033[1;34mfeito com sucesso\033[0m")
try: #ver se já existe o arquivo, ou se a senha mudou
tst = open(".utf_login","r")
inp = tst.read()
tmp = base64.b85decode(inp.encode("ascii"))
rx_comma = re.compile('\d|\w+')
match_obj = rx_comma.findall(tmp.decode("ascii"))
if match_obj[1] != senha or match_obj[0] != ra:
raise NameError("bitch_changed_his_pw")
except (FileNotFoundError, NameError) as e:
if dbg == True:
print(e)
try: #tenta fechar o arquivo aberto pelo teste anterior
tst.close()
except:
pass
ra64 = base64.b85encode((ra+":"+senha).encode("ascii"))
creds = open(".utf_login","w")
creds.write(ra64.decode("ascii"))
creds.close()
else:
print ("\033[1;31mLogin falhou\033[0m")
result.close()
except (ConnectionError,TimeoutError):
print("\033[1;31mErro de conexão\033[0m")
def logout():
try:
url_utf = "https://1.1.1.1/logout.html"
payload = {'userStatus':'1','err_flag':'0','err_msg':''}
headers = {'Referer': url_utf}
response = requests.post(url_utf, data=payload, headers=headers,verify=False)
data = response.text
result = open("result.html",'w')
if re.search("To complete the log off",data):
print("\033[1;31mLogout \033[0m\033[1;34mfeito com sucesso\033[0m")
except SSLError as err:
print ("\033[1;31mErro de SSL:", err, "\033[0m")
def is_ra(stg):
"""retorna 0 se forem só os numeros do ra, 1 se tiver o a, -1 se não for ra"""
get_num_rx = re.compile('[0-9]+')
stg_reg = get_num_rx.findall(str(stg))
stg_num = stg_reg[0]
if len(stg_num) == 7:
if len(stg) == 8:
return 1
else:
if len(stg) == 7:
return 0
else:
return -1
else:
return -1
def get_ra():
"""retorna o RA com o 'a'"""
global error_msg
get_num_re = re.compile('[0-9]+')
print("Digite seu RA: ")
try:
ra_reg = get_num_re.findall(str(input()))
ra = "a" + ra_reg[0]
except:
print(error_msg)
sys.exit()
if(is_ra(ra) == -1):
print(error_msg)
sys.exit()
return ra
def main(argv):
try:
if argv[1] == "-o" or argv[2] == "-o" or argv[3] == "-o":
logout()
except IndexError:
try: #tenta abrir o arquivo que contém a senha antiga, se conseguir, envie-o para o argv e prossiga normalmente
global dbg
dbg = False
creds = open(".utf_login","r")
inp = creds.read()
tmp = base64.b85decode(inp.encode("ascii"))
rx_comma = re.compile('\d|\w+')
match_obj = rx_comma.findall(tmp.decode("ascii"))
login(match_obj[0],match_obj[1])
except FileNotFoundError:
global error_msg
error_msg = "Utilização: ./loginUTFPR.py RA senha (-d debug) (-o logout), sendo todos os parâmetros opcionais"
if len(argv) > 1:
if len(argv) >= 3:
if (argv[2] == '-d'):
dbg = True
argv[2] = str(getpass.getpass())
elif (argv[3] == '-d'):
dbg = True
if (is_ra(argv[1]) == 0):
login("a" + argv[1],argv[2])
elif (is_ra(argv[1]) == 1):
login(argv[1],argv[2])
elif (is_ra(argv[1]) == -1):
print(error_msg)
sys.exit()
else:
if argv[1] == '-d':
dbg = True
argv[1] = get_ra()
if (is_ra(argv[1]) != -1):
pw = str(getpass.getpass())
if (is_ra(argv[1]) == 0):
login("a" + argv[1],pw)
elif (is_ra(argv[1]) == 1):
login(argv[1],pw)
elif (is_ra(argv[1]) == -1):
print(error_msg)
sys.exit()
else:
ra = get_ra()
pw = str(getpass.getpass())
login(ra,pw)
if __name__ == '__main__':
try:
main(sys.argv)
except:
print("\033[1;31m"+"Você está conectado a rede UTFPR WEB?\nConecte-se"+"\033[0m")
@cauefcr
Copy link
Author

cauefcr commented Sep 10, 2015

agora ele pede a senha toda vez

@cauefcr
Copy link
Author

cauefcr commented Sep 10, 2015

agora ele pede a senha uma vez, e salva em um arquivo de texto o login e a senha encodados.

@cauefcr
Copy link
Author

cauefcr commented Sep 10, 2015

agora também faz logout

@cauefcr
Copy link
Author

cauefcr commented Sep 10, 2015

Saidas mais bonitinhas.

@cauefcr
Copy link
Author

cauefcr commented Jun 17, 2016

agora ele apaga o arquivo temporário e os erros foram impedidos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment