crypto-hack/encoding/automating.py
2020-04-24 16:04:28 +10:00

51 lines
1.5 KiB
Python

import socket
import json
from Crypto.Util.number import bytes_to_long, long_to_bytes
#from utils import listener
import base64
import codecs
import random
def netcat(hostname, port, content):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostname, port))
s.sendall(content)
s.shutdown(socket.SHUT_WR)
while 1:
data = s.recv(1024)
if data == "":
break
#print "Received:", repr(data)
#print "Connection closed."
s.close()
host = "socket.cryptohack.org"
port = 13377
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
while 1:
data = json.loads(s.recv(1024))
if 'flag' in data:
print(data['flag'])
encoding = data['type']
print(encoding)
if encoding == "base64":
encoded = bytes.decode(base64.b64decode(data['encoded']))
elif encoding == "hex":
encoded = bytearray.fromhex(data['encoded']).decode()
elif encoding == "rot13":
encoded = codecs.decode(data['encoded'], 'rot_13')
pass
elif encoding == "bigint":
#print(bytearray.fromhex(data['encoded']).decode())
encoded = bytearray.fromhex(data['encoded'][2:]).decode()
print(encoded)
elif encoding == "utf-8":
encoded = bytes.decode(bytes(''.join([chr(b) for b in data['encoded']]), "UTF-8"))
jsonEncode = json.JSONEncoder().encode({"decoded": str(encoded)})
print(jsonEncode)
s.sendall(bytes(jsonEncode, "UTF-8"))