/ encoding / automating.py
automating.py
 1  import socket
 2  import json
 3  from Crypto.Util.number import bytes_to_long, long_to_bytes
 4  #from utils import listener
 5  import base64
 6  import codecs
 7  import random
 8  
 9  def netcat(hostname, port, content):
10      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11      s.connect((hostname, port))
12      s.sendall(content)
13      s.shutdown(socket.SHUT_WR)
14      while 1:
15          data = s.recv(1024)
16          if data == "":
17              break
18          #print "Received:", repr(data)
19      #print "Connection closed."
20      s.close()
21  
22  host = "socket.cryptohack.org" 
23  port = 13377
24  
25  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
26  s.connect((host, port))
27  while 1:
28      data = json.loads(s.recv(1024))
29      if 'flag' in data:
30          print(data['flag'])
31      encoding = data['type']
32      print(encoding)
33  
34      if encoding == "base64":
35          encoded = bytes.decode(base64.b64decode(data['encoded']))
36      elif encoding == "hex":
37          encoded = bytearray.fromhex(data['encoded']).decode()
38      elif encoding == "rot13":
39          encoded = codecs.decode(data['encoded'], 'rot_13')
40          pass
41      elif encoding == "bigint":
42          #print(bytearray.fromhex(data['encoded']).decode())
43          encoded = bytearray.fromhex(data['encoded'][2:]).decode()
44          print(encoded)
45      elif encoding == "utf-8":
46          encoded = bytes.decode(bytes(''.join([chr(b) for b in data['encoded']]), "UTF-8"))
47  
48      jsonEncode = json.JSONEncoder().encode({"decoded": str(encoded)})
49      print(jsonEncode)
50      s.sendall(bytes(jsonEncode, "UTF-8"))