-
Notifications
You must be signed in to change notification settings - Fork 4
/
nqtg.py
executable file
·113 lines (101 loc) · 12.8 KB
/
nqtg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python3.5
import socketserver
import sys
import time
import random
import socket
import os
import itertools
import binascii
C, S = object(), object()
EXCHANGE = (
(C, '6f5e761900a6ce2b2181af41025d81f94dcbc7b169d26551c3fa3b27c75c4f9b67f48c60dd5044863b60ad7a7e888b4d8c30f26d99e31ac2b98474dd49cf2396a4477aeb21d12b75b378c9c64126c12a3f45e43371cd6b19a5141f9fa8a4dc9f4e8fbcb47dc03f979c'),
(S, '37a8730ff3facf47b039a974c2a7fce8787afcf55da88046ec3c9ea6c39b08330458c278422fd70c9ee82977534322b8a1cc59381f2b2437150f58abb29dc51559d49e29585b50c7c5ad3ead3dee8f1d4f4801d740'),
(C, '51e4fa1929b45ac307cd79582339a0bef0414854ccfbcbc91a759b432065a678edfe728eed059dd94c59a469152541325e317fbced2e1ad9dbc4e452cc8e6b9776a43a92db7af2e144b6b5fbbacdb1c57a6a0b72654af8a943f0ed68a88748911192d38c7826faa6b65c62a706ddface7d8bddfc86f830b1989cd0d63fbebddff6eaa50baf18f079a258dd1dd0438df5cf0fd545cb161a4fefeae37e72410c43d693b77ef6a4c96464efbf1dcadfee10c7cbe21ebdfd0f6518f54250355589e6934478179bd1c0a5b0af6b34d4b16e78a2547d5b5e44edfa5a'),
(S, '462ca4ed5b545b581681742f53581ebf06b26d8735bc9ef763aa1e91b9e98f996c497b66fa06f679a503509ebd9a14173da1eaee85b18c5b259acc706850b884286824dd39b57d4adcf23505cb67dba7499d5c07b8f138f6555100c95ba65edd7c9bab63d359e64b37da9c4a60d29ac9964888aa6576c3ad7a97faef2e8b6110cb825049017327bc43b193c433c81517d74648ac72f31213987506f1cff335c9beb5d3682e6f21670fbe1f308ab1e7cdd0e92108a4557e82ccc7fe80b1ad90e3fe99fc46a37a2904d3ca39568e69c46917d8e1bde9f49cb6052eaf35bd76200079d40191fd612cacc785760d14a0653048d06db1ef7370622b6c9d7821cc69b8e9b2c5cd3ed99dcab60fe3c00460be1183a2618d11bb65bea913861035371e41926482335ea79b78711144149f9d3332d44cedb9cf2c7ac487bd8708c9aa40e8be47c77c7a42cd0e49d9583c4aebc9ff12d1205cbb4f4347beb3c24a4b9ade6233e91d70465e983290bfd94aa39197dbefcc83bbd68ee970ec6d305015ea43cd79c74dd8cb0f50e4e351562396d435d06cf04d78267f31c35717c51b84f8213b05270266bbe7d08e6ebe8345f28fe2387d95f36148a344e11b96212c73fce4b397c81f423bc575140fa3ddef2b2fe53f463a73fb9646c076d97d57538d6bc092f71c53d5fa798ce95969bfe2d1ae4dd94cc2cd87cd8b899d5cd37d6f91b201f8960ce88e8d2945882a74de3db83dcd2b9f0077660ac7e645b9ad64d1c6d7f3e3407f72749f99e235b30eb1a108ed8552a876a52abaaaa5ac4dfbf3d1ebd16bab481ce77544caab09f25508afd66cc0dd155a3dc85ae3c354438f9c628ff86f52b2126b157188ed5b27de90c9680b130c4f6eb7236c20538932f785c86d70b7b3877b65b647764c1774f4652c0973e2f4439e213174f37b69728da0e006d8a95de23f74ca8266db8cc5d56c653a4dd550b9889f5c1ef24e4ce42845f469658659538b8c680267708f42c1eb11d90bd005280937d4b949f815a15d9547abd59252d778b61e2b0bbf24bb918ca21987252972432b95ba01ab987f140c491b3a594f839607113968fc88484bad77d7b7765b7275eb885ce3331fac24a03df89b10d294ba292bd11875ac9d4c180198b0be9be0c5f8f19177d8b70047312b5f99fd59d90988347fe346f40301f033e3940f8294e5511e0cf62f7337c067bf1099cfe8d0e8132af2e0183f971e98d659b7a6b7af0a00fa6840c69e7a8fde33a2df8e822353bd2570b7482699cc62945e23c8e262ec1749e3c87c7a51dd92c7ff95575eceb8ef2fcad51af37c8d1eef005f4ecc2af19a610f48333162357c25a5b2c72505baef36a8b505ad1f83af8751834d48ccd24beaf28c15f440d2f4aabe73970d30b0fa91d3614e2681ef514bb6097fa7d80ef2b7c7cd9027810bec45cafd820c06e4a6c07500267c5fabb6f7bb5d2d359f9980bfa31f9f2c27a546a00e30059f2058a9875a2083628d93b977dc42e659523b40fdc6fb2a92a68654864e383cd708749cdb7dcfabbe560a9482bb91e9b320330395f56f9236faa4a73f7d795f888ce1e9ce055681298946b20d5404512d67d159839d376cd8a2ebc72675528481009ff48a78d8e789836ec6c34f28d04ef46dc1e96336e0f48d70537e191c76700b69cde40618f114fdc7261bd0a0333a263d87290748543df5c36cef128c7a2103e6ad891e87792e2cf6222b2e6ff0eccc9b879a55c5ab6c40a4c6dad0454dabafcf0a16a7f458fe37eabc8f8a5db372b70469bd748efae6954c2fbff28a562a04d0b49abd376377d798224086a310058ed8d70866d3bca90a271d8f53924f3acecef3ac6b0f95dfa3cbd447ea4d8bd3003272d0c8a75d77f2668de2b47c3f241bd188614722d0bdda13075a6dd192a0d0c16c13556a53cf0d4d706f28c48fd90f94aaaca771e4786400'
'9c73d7e8b09e53f51931e5f62509396c3d6e44f68be1b325d68d21ee58cffd64f7379e9bfc10b5e8fb198c1394724cb9ccb1e7edc4efaa197a415da62bc7a5efa09b0a8e23b2b876ce1e1daa5d42c748a7cee70e2d60fe44bc2f1fdfd6c710e8b69941d4e65ad5574d20846bc547a0276209751a8679410802e66056f00345c3419e69fd5e1e3891bf30225592e57716a1e2c773d486236cf523acebb0f9c521bde785c93c76c69c6764988a497d0a1a9567547ea3bedb260a1ba10fc071257ce392570b47bbd489f30130c23de00842858b9cdd5eff7b467edd46f5d2c1ca204582b7075dad87e50b205281bf17140b0a4ad68175550b2f72498fc3a045f9c55c0a0f161eaaffc8f6ee020ef585cbb52a9a51c7eb4ad0ac52aca82757919a7c7fba38fcbc8f28160fb92da9da2b639317ee06526fe0b083f62b0fb92ff24767b5fe970fe466b2369570c5c70f23c6d1e45d4b6166071998582c07b88220c2f95fce351699bc74755d400e2ba550c8bd5398dcbc9b7920924311cf9e0e7c622c9a00dd7e8f039c6ba3b41cae6c474151f175502be64ecfc9c466ff0774faace87e8fc2cae3c80ab0ac7de0a6a5c5fd60bd9bb15e87c1528262e8040c5a896a462dac3799d6ef0582b08cb2fd849711f65b7dd91f7aafca5a81cb8f476cdcf9d42d30572a90f2551cb13527573849b85e8ff6c3b5064ef037d91bab3aeaf12ad336ba5d5d08b37ccd26e55a8a99da6011cbdabd5491965e5e549065bfd411fba0723f0eb25800cc7478fa2deec81fe034abf4bf6c041c1570eadaf910b50665666012fdeb7d36ebb5b14cd9af98b5bd843d3b45ac82cd165098d51218dd4ca72a6c681e3c946f65f20efb35fdd131533a500b36cd1cf2013e5617fdbd41cb6e20c2c23a19438fc4a5cbd229c2eb1518bc35927bddba6e4e618f3538ab9db462f31b8e7be2cd85c70eab7667c05f2ab0177da77f4beaff425a254e2672f534377abef49d9f847d08cbaa35184dc86853ca3dd443faed947e306f7b345e3a7fa2727bbafeafe19c7d6912a1de114cf0b154c9ce2e4b79de54454684afc1c9a5f786fc0753f43f2903256cfa5f2db8a7581949a9a3d8bb56f1ff2c4319cadd8f3952694327283b432d2b304058384736c9010bed850e6d3d287146087733a5ad37bdf8a46da21847d2485889a0fdc6fb17feaa0786b90141d40b7543c9e8e8a917f3f9f6dc5574e54b67fbb2c86d7423ba10e46e0989114a182b07553a3459f7b0b778d615124fb4df01c78d8417873c787d2f927da915cc82e2bc7a641d503b8355bf831bee3de93fea03f87008a9e0d9582448635d39fdf19ccadf8e9341e0f7baab0e889ed1cde9189084b307fec45f3e02ed1afb026790d3646c75b6f757b514c4af6cac799289330f50cffa72552973b1eff70f2209890dd8d54f161d54ed0de51f01c925c84a483d5e61f5416711fe2480e63a96506ed16aded5'),
(C, '59c0d74b3e1893e56a6b6413ae86153cd110d21ca364ad3927654d841793a14bce159ff6b4d1f6d39c6de116ed0e8c6b92b25ff4bf3488f52c0feec1d2219cba52d2edc78e351213fe79e756a7dfeb6aa36a91a58ce70fdf3d5d15cdaa5e79da70a2e348bffea4ea1d2420a801dcf0788e148d0c5bbb49e83300aa3ade2747182f458885c72678f2c53b143c7eb0c01bb348380ecab2fb8212310de7ae4b00195f0a224dd2ce621570b6dfcc798ab6c3214e71a3d0991ad7ab18bee048d36434b5e7de2a0acc65d8cd510928eb623e163251d77bc5ebc529c5e86c06a7cbd108b22133550531d86fdfc08d23645fad2e6de3f2bb5a3fa05dc1a3de74df685a556527daf4c632c1b56c8f3bbaeabccd846aea1b8441cd4ff88ece901ac0562d96cd4e9d5e311d897e0ded8bbeb74efa121e2dd96174d82dded70ee10962c025f87103b169e0029c7005e2f8ceb19c4895ee72f83a98cbd70c071f2ee5af5de49a5c6c85e7234aa16cedc2b97f8b270d9ebe30b3eb031fbbdbbd55ac4eccda858c5011703d5af400ed672d07cc8bf84e3dbe7c6e67effb565c64'),
(S, 'b43dc890bfef88be5ddef29e462a4dd1b9c0a024dbe54815e32c76ae8973f654bbb1eea69476ae454534014e2aa91e96bdbd8d9fc77fbf12adea06b29c4da74dd2d90722e540ff6a358a668c6770a2ccdfae8d595331bcee75'),
(C, 'c4efdea0b3304fa179e67a5409d6c53852c64512b604a6e03a0d30c22a517d1776c96e9ee15691f70e7e9f840f8be553056d1a07b886e28638d2c4fdb816056be01ec30d256cee1f0b868ec8cdd835c0215f61e5a5a1fb106aaebbe8a17eb6245c5f1e9a74ab79ad98f576ca805fccd3b79c2d0c9866bdc3b583ffe97ae58de198fbfc095cf8a4bf527ea01e8ca195572798210167464ace3ef2a226fe410a425b10bd81c9a8efd1496fd455874c3622923d46dceaae1c6133ceb9fbeb8745ffed98a82b3274d65d12cd42eee52e925eebedd3f24ff90f042ffc9744d3e04e5c375a'),
(S, '44327d5dd98447c33a3a8e9df6363eefe56cce2a1ed1f3d511c6f85949427f12a23799ff35316a3675b56d1fd978d99935badc1a611098d899f3e77a265cdd3073428cf7474b5cfc4594a30c332860412af3294e913491be887dea38ecc7d6799ea5b8b9ab341e02f30cbf2b3eb3dcb72c4875c9cc1612f01b9724264e44a91a6efa4e9cc8dfd4d87ce0b7f5c3578136053be99a3a3ec3dde632808830028498873d09958c8d53600fd6799f78db4c46bf05dac2cbc8618e7e387068c5e1f76eee42bc975b1524159dbc0ac8cfe617b245223cd5a5f8a710dc034102a0a253726410407cf56e385c1b3c896b91e3f45df688'),
(C, '1239a31334684835de4904cc226405e64db2f349316deea31a2566294cd2b549371fc884cba87d2b2135ebbef4fceb28773dde7580b5311fccfc27689f4e5ee169298d6a2c8b4219e4ac7ef514b0b971a0d240d61aaca71642d2c1d24f1eba4855d3303f7eb77bb76635dcd0d887344f08a8eaa335d36abbd2552c0b017332407da08dd0de9c1dbd47159f8f192eaea00eb703273d097e682a2e7e0efe06e6d4193fec0130cdf05e8c'),
(S, 'c35ffd8df343db22973d573207b2dc41e8d20b96c6625b08695239ae9960c6a70b1cc8873a0c2f8cfcbc048cda1c157161cbe08f3fab520701272c840704b25fab7b3d0d2516386578411170f6ddad2dee3e35c5d19849b5df'),
(C, 'd91d3f4dd4f0446f9f8e01e377cce0640961d04ec1a113f6baff787d32a80c0338dc38e19d6bf3651ca8100880098d1c9502183c63841358723e7771b487b3439476d60b9a291278a73c4bf9b4b5d6f5f5dca0599fc8a52e6cc810ece38bf6fb296dce2f457beafaa1a021909dee231b7aeb01af09a88d7dbda7ceb0384391d896edb7cc0a2e26bd8a258a4e49e1c3cd7be795ca9df9a557e1'),
(S, 'caac1f6a6c7400beb85c6ca988819857fe1f06b5f3176a0453a433155e498bb2b1e78cb59e40ab2a9f56b2e88d40309a62e0f3e5e666d39e8a131cbbf1a019936d04e71c09e26d3ba017825eb4940526a41801a7aec095a4ff42faa101f2debd4f63921826f2b669aa66ed6decf1827c6f6691a5da7be6a16e539e681a663aecc793042afcf8c22f78a55e085af4dcc5804ea2262f9b1aabf9f82e044ac3a4ad937d5a353c5b383c313879a933dff089e61ff24af5fca18349133d7cec3e0ae3ff3d'),
(C, 'def822278144ad0ab29a764079b162bdeff04a9fb1a0b9a3816d77dce6811b01bdf22c9bdd637b8bc3e9f3b352e187576bec701104ba0f7a85d95041878ccd7b97e0fcf9a4d7be3a92179bf1b4e505c8b09b5aecc95b28593528c616fdf20bfb0e4cfce06138166e1e722214367580b077629732939b31b0a17e16c5e07c0f8438927b0406a734d99cc320da0c62fd454fb4ad1e77a2312d514fe7cb6f5322f56756ff95e647a13685'),
(S, 'f82064df7939136b16e35e703ae67c09f7971ec60748d9e927f5588923c05ef7c6fa14cad1b6b94f07eef5605a08ee66c2919d5f654476577c551afd3303bb8c2c081d0f66f67129ba335b4b0ff0785050d093b5198c9c5b6b'),
(C, 'fc2f2599f7fd66df2cd4d0e76c2d591888d9c210dccf39af7e1b00b64cc043859e852d29acce1d13c70c4a7e96a5b385d98c7d55922b72d41171e22ee672932ce19a000c46230bdafddc3bd604c785be1973037078c6b9aca423e1d325cf895ef51ef8e5f4bdab9ee6212d77a8b08422802c010651f4abd461d14d4045d5dc0deab513794a9bc45e97e60dc54aff063193a101b0ed6d7b69e3'),
)
EXCHANGE = [(end, bytearray(bytes.fromhex(s))) for end, s in EXCHANGE]
SEED = object()
MAGIC_SEED = 1693627364
SLICE_SERVER = True
SLICE_CLIENT = False
CLIENT_WAIT_SRV = False
CLIENT_BLOB_XOR8 = False
def fdskip(fd, nbytes):
while nbytes > 0:
ans = fd.recv(nbytes)
if len(ans) == 0:
raise RuntimeError('Unexpected EOF')
nbytes -= len(ans)
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
srvgen = random.Random(x=SEED)
cligen = random.Random(x=(SEED^MAGIC_SEED))
print('Connected from {}'.format(self.client_address))
for ex in itertools.count():
print('Pass #{:d}'.format(ex))
mask = bytearray(os.urandom(1))
for end, blob in EXCHANGE:
if end is C:
nbytes = cligen.randrange(len(blob)) if SLICE_CLIENT else len(blob)
fdskip(self.request, nbytes)
elif end is S:
for i in range(len(blob)):
blob[i] ^= mask[0]
nbytes = srvgen.randrange(len(blob)) if SLICE_SERVER else len(blob)
self.request.sendall(blob[0:nbytes])
else:
assert False
def tgcli(host, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as fd:
fd.connect((host, port))
srvgen = random.Random(x=SEED)
cligen = random.Random(x=(SEED^MAGIC_SEED))
print('Connected to {}'.format((host, port)))
for ex in itertools.count():
print('Pass #{:d}'.format(ex))
mask = bytearray(os.urandom(1))
for end, blob in EXCHANGE:
if end is C:
nbytes = cligen.randrange(len(blob)) if SLICE_CLIENT else len(blob)
if CLIENT_BLOB_XOR8:
for i in range(len(blob)):
blob[i] ^= mask[0]
else:
blob = os.urandom(nbytes)
fd.sendall(blob[0:nbytes])
elif end is S:
if CLIENT_WAIT_SRV:
nbytes = srvgen.randrange(len(blob)) if SLICE_SERVER else len(blob)
fdskip(fd, nbytes)
else:
time.sleep(srvgen.uniform(0.5, 1.0))
else:
assert False
def main():
nargs = len(sys.argv)
global SEED
if nargs == 1:
host, port, SEED = '0.0.0.0', random.randint(1024, 32767), int(binascii.hexlify(os.urandom(4)).decode('ascii'), 16)
print('Serving with following port/seed: {:d} {:d}'.format(port, SEED))
server = socketserver.TCPServer((host, port), MyTCPHandler)
server.serve_forever()
elif nargs == 4:
host, port, SEED = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
tgcli(host, port)
else:
print('Usage: nqtg.py [host port seed]')
if __name__ == '__main__':
main()