diff options
Diffstat (limited to 'kpov_judge/tasks')
-rw-r--r-- | kpov_judge/tasks/nat_port_forward/task.py | 126 |
1 files changed, 112 insertions, 14 deletions
diff --git a/kpov_judge/tasks/nat_port_forward/task.py b/kpov_judge/tasks/nat_port_forward/task.py index d7dde03..a3fc740 100644 --- a/kpov_judge/tasks/nat_port_forward/task.py +++ b/kpov_judge/tasks/nat_port_forward/task.py @@ -63,37 +63,135 @@ networks = { 'net1': {'public': False}, 'test-net': {'public': True} } params_meta = { 'IP_TEST_CLIENT': {'descriptions': {'si': 'Naslov TestClient'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False}, + 'IP_NAT': {'descriptions': {'si': 'Naslov NATServer, dostopen s TestClient'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False}, 'PORT_OUTER': {'descriptions': {'si': 'Zunanja vrata'}, 'w': False, 'public':True, 'type': 'port', 'generated': True}, 'PORT_INNER': {'descriptions': {'si': 'Notranja vrata'}, 'w': False, 'public': True, 'type': 'port', 'generated': True}, 'NET': {'descriptions': {'si': u'Področje naslovov med SimpleArbiter in TestClient'}, 'w': False, 'public': True, 'type': 'NET', 'generated': True}, } -def task(IP_TEST_CLIENT, PORT_OUTER, PORT_INNER, NET): +def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET): import pexpect - + import pxssh + import re + import random + import time + # return None + peer_user = 'student' + peer_password = 'vaje' + results = dict() + tcpdump = pexpect.spawn('sudo /usr/sbin/tcpdump src host {} and dst port {}'.format(IP_TEST_CLIENT, PORT_INNER)) + sshconn = pxssh.pxssh() + sshconn.login(IP_TEST_CLIENT, peer_user, peer_password) + r = random.Random() + k = r.randint(10, 15) + results['pre_nc'] = str(k) + results['nc_pre'] = "" + for i in xrange(k): + sshconn.sendline("nc {} {}".format(IP_NAT, PORT_OUTER)) + sshconn.sendline() + sshconn.sendintr() + sshconn.prompt() + results['nc_pre'] += sshconn.before + nc = pexpect.spawn('nc -l -p {}'.format(PORT_INNER)) + sshconn.sendline() + sshconn.prompt() + sshconn.sendline("nc {} {}".format(IP_NAT, PORT_OUTER)) + results['post_nc'] = "".join([r.choice("abcd\n") for i in xrange(100)]) + sshconn.sendline(results['post_nc']) + time.sleep(1) + sshconn.sendintr() + nc.expect(pexpect.EOF) + results['nc_ret'] = nc.before + results['route'] = pexpect.run('/sbin/ip route list 0/0') + results['traceroute'] = pexpect.run('traceroute {}'.format(IP_TEST_CLIENT)) + # wait for traceroute + time.sleep(10) + tcpdump.sendintr() + tcpdump.expect(pexpect.EOF) + results['tcpdump'] = tcpdump.before + sshconn.prompt() + results['nc_post'] = sshconn.before + sshconn.close() + # nc.expect(pexpect.EOF) return results def gen_params(user_id, params_meta): params = dict() r = random.Random(user_id) # IP_NM, DNS_NM, IP_static, DNS_static) - params['PORT_INNER'] = r.randint(6000, 10000) - params['PORT_OUTER'] = r.randint(10001, 15000) - params['NET'] = kpov_random_helpers.IPv4_subnet_gen("10.36.0.0/14", 24) + params['PORT_INNER'] = str(r.randint(6000, 10000)) + params['PORT_OUTER'] = str(r.randint(10001, 15000)) + params['NET'] = kpov_random_helpers.IPv4_subnet_gen(r, "10.36.0.0/14", 24) return params def task_check(results, params): import re - score = -9 - if results['NM_nslookup'].find('Server:\t\t{0}\r'.format(params['DNS_NM'])) > -1: - score += 3 - if results['static_nslookup'].find('Server:\t\t{0}\r'.format(params['DNS_static'])) > -1: - score += 3 - if re.search(r'eth0 +802-.*connected', results['NM_nmcli']): - score += 2 - if not re.search(r'eth0 +802-.*connected', results['static_nmcli']): - score += 2 + import cPickle + if results is None: + with open('bla.pickle') as f: + results = cPickle.load(f) + else: + with open('bla.pickle', 'w') as f: + cPickle.dump(results, f) score = 0 + local_net = params['NET'][:params['NET'].rfind('.')] + s = "default via ({}\\.[0-9]+)".format(re.escape(local_net)) + res = re.search(s, results['route']) + if res: + ip_nat_internal = res.groups(1)[0] + score += 1 + else: + ip_nat_internal = 'abrakadabra' + # print (s, results['route'],) + s = "traceroute to {ip_test} \\({ip_test}\\), 30 hops max, 60 byte packets\ +\r\n 1 {ip_nat} \\({ip_nat}\\) [0-9.]+ ms [0-9.]+ ms [0-9.]+ ms\ +.*{ip_test} \\({ip_test}\\)".format( + ip_nat = re.escape(ip_nat_internal), + ip_test = re.escape(params['IP_TEST_CLIENT']) + ) + if re.search(s, results['traceroute'], re.DOTALL): + score += 1 + else: + print (s, results['traceroute'],) + if len(results['post_nc']) == 100: + score += 1 + else: + print (results['post_nc'], len(results['post_nc'])) + if results['nc_ret'] == (results['post_nc'] + '\n').replace('\n', '\r\n'): + score += 1 + else: + print (results['post_nc'], results['nc_ret'],) + s = "Connection refused" + res = re.findall(s, results['nc_pre']) + if len(res) >= 2: + score += 3 + else: + print (s, results['nc_pre'],) + s = "\r\n" + if re.search(s, results['nc_post']): + score += 1 + else: + print (s, results['nc_post'],) + rejected_count = int(results['pre_nc']) + accepted_count = results['nc_ret'].count('\r\n') + s = ".*verbose output suppressed.*listening on.*dropped by kernel.*" + if re.match(s, results['tcpdump'], re.DOTALL): + score += 1 + else: + print (s, results['tcpdump'],) + res = re.findall("length .*\r\n", results['tcpdump']) + total_len = 0 + n_empty = 0 + for i in res: + k = int(i[len("length "):].strip()) + total_len += k + if k == 0: + n_empty += 1 + # print total_len, rejected_count, n_empty + if total_len == 101 and rejected_count <= n_empty: + score += 1 + else: + print (s, results['tcpdump'],) return score def prepare_disks(templates, params): |