Last active
April 7, 2022 19:50
-
-
Save ffernandoalves/3f7ee051cffbfe990bf0cf0db3be28d6 to your computer and use it in GitHub Desktop.
Atualização desse código https://gist.github.com/shinyquagsire23/0d6a5119ee7fb40de2fcfb9088168d63 para python3.7 em resposta para essa pergunta https://pt.stackoverflow.com/q/493676/196624
This file contains 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
from Crypto.Cipher import AES | |
from Crypto import Random | |
import base64 | |
import requests | |
import xml.etree.ElementTree as ET | |
block_size = AES.block_size | |
unpad = lambda s : s[0:-ord(s[-1])] | |
lg_deckey = b"wleldpadptmdkdnt" | |
lg_enckey = b"qlxnTldlsvntzl!#" | |
def pad(plain_text): | |
""" | |
func to pad cleartext to be multiples of 8-byte blocks. | |
If you want to encrypt a text message that is not multiples of 8-byte blocks, | |
the text message must be padded with additional bytes to make the text message to be multiples of 8-byte blocks. | |
""" | |
number_of_bytes_to_pad = block_size - len(plain_text) % block_size | |
ascii_string = chr(number_of_bytes_to_pad) | |
padding_str = number_of_bytes_to_pad * ascii_string | |
padded_plain_text = plain_text + padding_str | |
return padded_plain_text | |
def lg_encrypt(string): | |
key=lg_enckey | |
plain = pad(string) | |
iv = b"\x00"*AES.block_size | |
cipher = AES.new(key, AES.MODE_CBC, iv) | |
encrypted_text = cipher.encrypt(plain.encode("utf8")) | |
return base64.b64encode(encrypted_text).replace(b"+", b"m").replace(b"/", b"f") | |
def lg_decrypt(string): | |
if len(string) == 0 or string == "\n": | |
return "" | |
key=lg_deckey | |
crypted = base64.b64decode(string) | |
cipher = AES.new(key, AES.MODE_ECB) | |
decrypted_text = cipher.decrypt(crypted) | |
return unpad(decrypted_text.decode("utf8")) | |
imei="lol" | |
esn = lg_encrypt(imei) | |
data = {'esn':esn} | |
r = requests.post(url = "https://csmg.lgmobile.com:49002/csmg/nb2c/gn_auth_model_check2.jsp", data=data) | |
def xml_recurse(node): | |
dec = lg_decrypt(node.text) | |
if (node.tag == "sw_url"): | |
dec = "http://tool.lime.gdms.lge.com/dn/downloader.dev?" + dec.split("?")[1] | |
print (node.tag, node.attrib, dec) | |
for child in node: | |
xml_recurse(child) | |
root = ET.fromstring(r.text) | |
xml_recurse(root) |
Amigo novamente muito obrigado, graças a sua ajuda ao consertar o código, consegui entender o funcionamento do script, ainda não rescrevi o código em c#, mas já consegui rescrever em PHP que é a linguagens que tenho mais conhecimento. O mais complicado foi entender essa criptografia, mas ela é "aes-128-cbc" 128 bits e nessa função o key precisa ser de 32 bits, mas a api tem um key com 16 então tem que ser adicionado mais 16 bits que no python é feito na linha 30 e 31 e os dados é enviado via post e o retorno da api é criptografado também, para descriptografar é usado "aes-128-ecb".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para falar a verdade usei essa lib somente uma vez por curiosidade, não sei muito bem como é que funciona a criptografia dela.
Mas nessa parte (
AES.new(key, AES.MODE_CBC, iv)
), é instanciado um objeto com a chave de encriptação, sinaliza que é para usar o modo CBC e um 'initialization vector' (iv), esse obj vai possuir dois metódos encrypt() e decrypt(), neles você passa a sua mensagem (plain_text ou crypted_text).