From b5b35faea7f4205f353f57178ddc795b7dce5043 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:02:26 +0200 Subject: Refactor SSH testing code out of individual tasks Catch SSH errors and report failures as hints. Also some cleanups and 2to3 fixes. --- kpov_judge/kpov_util.py | 22 +++ kpov_judge/tasks/basic_network_gcc/task.py | 48 ++--- kpov_judge/tasks/copy_rename_100_files/task.py | 29 ++- .../tasks/copy_rename_20_files_tail_env/task.py | 40 ++-- kpov_judge/tasks/custom_rdate/task.py | 55 ++---- kpov_judge/tasks/dhcp_dns_predefined_ip/task.py | 43 ++--- .../tasks/edit_find_grep_compile_convert/task.py | 57 ++---- kpov_judge/tasks/ldap_import/task.py | 13 -- kpov_judge/tasks/mock_entrance_exam/task.py | 48 ++--- kpov_judge/tasks/nat_port_forward/task.py | 41 +--- kpov_judge/tasks/nat_vlc/task.py | 40 ++-- kpov_judge/tasks/openvpn_multiple_hops/task.py | 208 +++++---------------- kpov_judge/tasks/openvpn_simple_smb/task.py | 110 +++-------- kpov_judge/tasks/openwrt/task.py | 13 -- kpov_judge/tasks/radius_mysql_pam/task.py | 42 ++--- kpov_judge/tasks/rename_grep_network/task.py | 37 +--- kpov_judge/tasks/set_ip_static_dhcp/task.py | 22 +-- kpov_judge/tasks/set_motd/task.py | 15 +- kpov_judge/tasks/vlc_stream_rtp/task.py | 1 - 19 files changed, 249 insertions(+), 635 deletions(-) diff --git a/kpov_judge/kpov_util.py b/kpov_judge/kpov_util.py index 74b1737..9909973 100755 --- a/kpov_judge/kpov_util.py +++ b/kpov_judge/kpov_util.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import collections import itertools import random import string @@ -12,6 +13,27 @@ import subprocess import glob import os +def ssh_test(host, user, password, commands=()): + from pexpect import pxssh + results = collections.defaultdict(str) + try: + s = pxssh.pxssh(encoding='utf-8') + s.login(host, user, password, + original_prompt='~[#$] ', + auto_prompt_reset=False) + results['ssh'] = True + results['motd'] = s.before + s.set_unique_prompt() + for test, command in commands: + s.sendline(command) + s.prompt() + if test: + results[test] = s.before[len(command+'\r\n'):] + s.logout() + except Exception as ex: + results['ssh'] = str(ex) + return results + def alnum_gen(r, l=1): s = "" for i in range(l): diff --git a/kpov_judge/tasks/basic_network_gcc/task.py b/kpov_judge/tasks/basic_network_gcc/task.py index c4f54bd..8f19ae8 100644 --- a/kpov_judge/tasks/basic_network_gcc/task.py +++ b/kpov_judge/tasks/basic_network_gcc/task.py @@ -118,39 +118,22 @@ params_meta = { def task(student_IP, net_prog_name, P_c, P_executable, arg_c, env_c, out_stderr_c, out_stdout_c, P_script, param_gen_seed): - from pexpect import pxssh import random - conn = pxssh.pxssh() - conn.login(student_IP, 'student', 'vaje') - results = dict() + r = random.Random(int(param_gen_seed)) env_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(11)]) - arg_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(13)]) stdin_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(17)]) - conn.sendline('ls -l {}'.format(P_script)) - conn.prompt() - results['script_ls'] = conn.before - conn.sendline('ls -l {}'.format(P_executable)) - conn.prompt() - results['executable_ls'] = conn.before - conn.sendline('export {}={}; {}'.format(env_c, env_val, P_script)) - conn.prompt() - results['script_run'] = conn.before - conn.sendline('cat {}'.format(out_stderr_c)) - conn.prompt() - results['script_stderr'] = conn.before - conn.sendline('cat {}'.format(out_stdout_c)) - conn.prompt() - results['script_stdout'] = conn.before - conn.sendline('echo "{}" | {} "{}" 2> /dev/null'.format(stdin_val, P_executable, arg_val)) - conn.prompt() - results['prog_stdout'] = conn.before - conn.sendline('echo "{}" | {} "{}" > /dev/null'.format(stdin_val, P_executable, arg_val)) - conn.prompt() - results['prog_stderr'] = conn.before - conn.logout() - return results + + return kpov_util.ssh_test(student_IP, 'student', 'vaje', ( + ('script_ls', 'ls -l {}'.format(P_script)), + ('executable_ls', 'ls -l {}'.format(P_executable)), + ('script_run', 'export {}={}; {}'.format(env_c, env_val, P_script)), + ('script_stdout', 'cat {}'.format(out_stdout_c)), + ('script_stderr', 'cat {}'.format(out_stderr_c)), + ('prog_stdout', 'echo "{}" | {} "{}" 2> /dev/null'.format(stdin_val, P_executable, arg_val)), + ('prog_stderr', 'echo "{}" | {} "{}" > /dev/null'.format(stdin_val, P_executable, arg_val)), + )) def gen_params(user_id, params_meta): import random @@ -198,16 +181,13 @@ def task_check(results, params): expected_script_stdout, expected_script_stderr, rval = test_out_gen( params['arg_c'], env_val ) - expected_script_stderr = 'cat {}\r\n'.format(params['out_stderr_c']) + expected_script_stderr - # hints += [expected_script_stderr, results['script_run'], results['script_stderr'], params['arg_c'], env_val] if expected_script_stderr != results['script_stderr']: hints += ['wrong script stderr'] else: score += 2 split_stdout = expected_script_stdout.split('\r\n') expected_script_stdout = "\r\n".join([ i for i in split_stdout if i.find('ma') >= 0]) - expected_script_stdout = 'cat {}\r\n'.format(params['out_stdout_c']) + expected_script_stdout + "\r\n" - if expected_script_stdout != results['script_stdout']: + if expected_script_stdout != results['script_stdout'].strip(): hints += ['wrong script stdout'] else: score += 2 @@ -288,9 +268,9 @@ done; }& destroyed_c_source += 'QX' destroyed_c_source += c d.write(task_params['P_c'], destroyed_c_source) - d.chown(1000, 1000, params['P_c']) + d.chown(1000, 1000, task_params['P_c']) sh_path = r.choice(['/usr/share/doc', '/var/lib', '/usr/local/share', '/etc/alternatives']) - sh_file = sh_path + '/' + params['net_prog_name'] + sh_file = sh_path + '/' + task_params['net_prog_name'] d.write(sh_file, evil_shell_source) d.chmod(0o775, sh_file) d.write("/etc/rc.local", """#!/bin/sh -e diff --git a/kpov_judge/tasks/copy_rename_100_files/task.py b/kpov_judge/tasks/copy_rename_100_files/task.py index c658d27..f7fb3f1 100644 --- a/kpov_judge/tasks/copy_rename_100_files/task.py +++ b/kpov_judge/tasks/copy_rename_100_files/task.py @@ -73,20 +73,10 @@ params_meta = { } def task(host, folder): - import collections - from pexpect import pxssh - results = collections.defaultdict(str) - try: - # ideally, this would be done using a SFTP client instead of pxssh - s = pxssh.pxssh(encoding='utf-8') - s.login(host, 'test', 'test') - results['ssh'] = True - results['files'] = s.run('ls -1').split('\n'), # XXX: file may have newlines - results['contents'] = s.run('cat *'), # XXX: may include other files in $HOME - s.logout() - except Exception as ex: - results['ssh'] = str(ex) - return results + return kpov_util.ssh_test(host, 'test', 'test', ( + ('files', 'ls -1'), # XXX: file may have newlines + ('contents', 'cat *'), # XXX: may include other files in $HOME + )) def gen_params(user_id, params_meta): pass @@ -96,10 +86,13 @@ def task_check(results, params): score = 0 hints = [] + if results['ssh'] is not True: + hints += ['ssh failed: ' + results['ssh']] + matched = 0 files = os.listdir(params['folder']) for fn in files: - if fn in results['files']: + if fn in results['files'].splitlines(): matched += 1 if matched > 0: score = 1 @@ -113,12 +106,12 @@ def task_check(results, params): score += 3 else: hints += ["wrong number of files"] - rl = results['contents'].split('\n') + rl = results['contents'].splitlines() rl.sort() tl = [] for fn in files: - with open(fn) as f: - tl += f.read().upper().split('\n') + with open(os.path.join(params['folder'], fn)) as f: + tl += f.read().upper().splitlines() tl.sort() if rl == tl: score += 4 diff --git a/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py b/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py index d7f0cc6..b7970e7 100644 --- a/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py +++ b/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py @@ -118,9 +118,16 @@ params_meta = { def task(IP_malishell, file_rename_dirname, mv_src_dir, mv_dst_dir, cowsay_string, curl_fname, wc_dirname): import collections import os - from pexpect import pxssh - commands = [ + # TOD: (polz) this has to be changed! Get a move on! + # + # sv: Z primozem lavricem sva skusala nekaj narediti + # Ker gen params ni narejen, sklepam da je "Mapa" na namizju, + # imena datotek pa so: 1,1-,2,2-,3,3-,4,5,6,7,8,9,.mama1,mama2,mama3,mama4,mama5,mojimenik,novi,oce1 + # v mojimenik se nahaja mojimenikfile + # mama2 vsebuje "mama" + #Stirje subt-aski dodani.By Mihec. + results = kpov_util.ssh_test(IP_malishell, 'student', 'vaje', ( ('preimenuj', '/bin/ls -a1 {}'.format(file_rename_dirname)), ('pre_mv_src', '/bin/ls -a1 {}'.format(mv_src_dir)), ('pre_mv_dst', '/bin/ls -a1 {}'.format(mv_dst_dir)), @@ -144,29 +151,7 @@ def task(IP_malishell, file_rename_dirname, mv_src_dir, mv_dst_dir, cowsay_strin ('cowsay', 'cowsay "{}"'.format(cowsay_string)), ('wc_origfile', 'cat {}/count.txt'.format(wc_dirname)), ('wc_lines', 'cat {}/lines.txt'.format(wc_dirname)), - ] - - # TOD: (polz) this has to be changed! Get a move on! - # - # sv: Z primozem lavricem sva skusala nekaj narediti - # Ker gen params ni narejen, sklepam da je "Mapa" na namizju, - # imena datotek pa so: 1,1-,2,2-,3,3-,4,5,6,7,8,9,.mama1,mama2,mama3,mama4,mama5,mojimenik,novi,oce1 - # v mojimenik se nahaja mojimenikfile - # mama2 vsebuje "mama" - #Stirje subt-aski dodani.By Mihec. - results = collections.defaultdict(str) - try: - s = pxssh.pxssh(encoding='utf-8') - s.login(IP_malishell, 'student', 'vaje') - results['ssh'] = True - for test, command in commands: - s.sendline(command) - s.prompt() - if test: - results[test] = s.before - s.logout() - except Exception as ex: - results['ssh'] = str(ex) + )) try: results['curl_env'] = os.environ['images'] @@ -309,11 +294,8 @@ def task_check(results, params): else: hints += ["wrong image count"] task6_ok = True - wc_cat_str = 'cat {}/lines.txt'.format(params['wc_dirname']) - if not wc_cat_str == results['wc_lines'][:len(wc_cat_str)]: - task6_ok = False try: - assert int(results['wc_lines'][len(wc_cat_str):].strip()) == int(params['wc_n_lines']) + assert int(results['wc_lines'].strip()) == int(params['wc_n_lines']) except: task6_ok = False if task6_ok: diff --git a/kpov_judge/tasks/custom_rdate/task.py b/kpov_judge/tasks/custom_rdate/task.py index 51f5baa..da8313c 100644 --- a/kpov_judge/tasks/custom_rdate/task.py +++ b/kpov_judge/tasks/custom_rdate/task.py @@ -39,10 +39,6 @@ computers = { 'disks': [ { 'name': 'student-rdate', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -52,12 +48,7 @@ computers = { 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiterDhcpGWRdate', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}], 'flavor': 'm1.tiny', @@ -75,37 +66,26 @@ params_meta = { } def task(IP_RDATECLIENT, PROGRAM_FILENAME): - import pexpect - import paramiko - from paramiko import SSHClient + import collections + import base64 import random import struct - import base64 - results = dict() - peer_user = 'test' - peer_passwd = 'test' - client = SSHClient() - client.load_system_host_keys() - client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - client.connect(IP_RDATECLIENT, username=peer_user, password=peer_passwd) - #client.login(IP_RDATECLIENT, peer_user, peer_passwd) + import pexpect + r = random.Random(PROGRAM_FILENAME) + results = collections.defaultdict(str) + tests = [] for i in range(10): - stdin, stdout, stderr = client.exec_command( - '~/{}'.format(PROGRAM_FILENAME)) data = struct.pack("!I", r.randint(0, 2**32)) - stdin.write(data) - results['in_' + str(i)] = base64.encodestring(data) - results['out_' + str(i)] = stdout.read() - stdin, stdout, stderr = client.exec_command( - 'date -u +"%s"') - #client.sendline('date -u "%s"') - #client.prompt() - results['date'] = stdout.read() + results['in_'+str(i)] = data_ascii = base64.encodestring(data) + tests += [('out_'+str(i), 'echo "{}" | base64 -d | ~/{}'.format(data_ascii.decode().strip(), PROGRAM_FILENAME))] + tests += [('date', 'date -u +"%s"')] + + results.update( + kpov_util.ssh_test(IP_RDATECLIENT, 'test', 'test', tests)) results['ldate'] = pexpect.run('date -u +"%s"') - client.close() - return results + return results #Dolocil sem tri parametre in sicer jih lahko vidite v prams_meta, #zato prosim da jih upostevate v naslednih nalogah. @@ -124,7 +104,7 @@ def task_check(results, params): import random import struct import base64 - # print results + r = random.Random(params['PROGRAM_FILENAME']) score = 0.0 hints = [] @@ -139,7 +119,7 @@ def task_check(results, params): score += 0.5 else: hints += ["wrong convert"] - # print str(struct.unpack("!I", data)[0]), res.strip() + if abs(int(results['ldate']) + \ int(params['RDATE_OFFSET']) - int(results['date'])) < 3: score += 5 @@ -148,9 +128,6 @@ def task_check(results, params): return score, hints def prepare_disks(templates, task_params, global_params): -# d = templates['simpleArbiterDhcp'] -# print templates -# print params d = templates['simpleArbiterDhcpGWRdate'] s1 = """service "time_tcp" {{ enabled yes; @@ -170,7 +147,7 @@ import time import sys offset = int(sys.argv[1]) -t = time.time() + 2208988800 + offset +t = time.time() + offset # used to have + 2208988800 sys.stdout.write(struct.pack("!I", int(t))) """ d.write('/usr/local/bin/kpovrdate', s2) diff --git a/kpov_judge/tasks/dhcp_dns_predefined_ip/task.py b/kpov_judge/tasks/dhcp_dns_predefined_ip/task.py index 2559507..9d98ea4 100644 --- a/kpov_judge/tasks/dhcp_dns_predefined_ip/task.py +++ b/kpov_judge/tasks/dhcp_dns_predefined_ip/task.py @@ -65,37 +65,19 @@ params_meta = { } def task(IP_server, IP_client, MAC_client, HOSTNAME_X): - from pexpect import pxssh - #import pexpect - results = dict() - peer_user = 'student' - peer_passwd = 'vaje' - sA = pxssh.pxssh() - sB = pxssh.pxssh() - #prijavimo se na streznik (IP_NM je IP nalov streznika) - sA.login(IP_server, peer_user, peer_passwd) - #prijavimo se na klienta (IP_static je IP naslov klienta) - sB.login(IP_client, peer_user, peer_passwd) - sA.sendline('sudo ps -A') - sA.sendline(peer_passwd) - sA.prompt() - results['dhcp_proces'] = sA.before - # tukaj dobimo podatke, ce je na IP_NM naslovu res postavljen DHCP streznik - sB.sendline('sudo dhcping -s {} -h {} -c {}'.format(IP_server, MAC_client, IP_client)) - sB.sendline(peer_passwd) - sB.prompt() - results['dhcp'] = sB.before - # tukaj pa dobimo podatek, ce je IP, ki ga vraca DNS streznik za HOSTNAME_X pravilen - sB.sendline('nslookup {}'.format(HOSTNAME_X)) - sB.prompt() - results['dns_hostname'] = sB.before - sB.sendline('/sbin/ifconfig') - sB.prompt() - results['client_IP'] = sB.before - sA.logout() - sB.logout() - return results + tests = { + IP_server: ( + ('dhcp_proces', 'sudo ps -A')), + IP_client: ( + ('dhcp', 'sudo dhcping -s {} -h {} -c {}'.format(IP_server, MAC_client, IP_client)), + ('dns_hostname', 'nslookup {}'.format(HOSTNAME_X)), + ('client_IP', '/sbin/ifconfig')), + } + results = collections.defaultdict(str) + for host, host_tests in tests.items(): + results.update(kpov_util.ssh_test(host, 'student', 'vaje', host_tests)) + return results def gen_params(user_id, params_meta): params = dict() @@ -105,7 +87,6 @@ def gen_params(user_id, params_meta): params['HOSTNAME_X'] = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ') for i in range(5)]) return params - def task_check(results, params): import re score = 0 diff --git a/kpov_judge/tasks/edit_find_grep_compile_convert/task.py b/kpov_judge/tasks/edit_find_grep_compile_convert/task.py index a2f1ed0..23825c5 100644 --- a/kpov_judge/tasks/edit_find_grep_compile_convert/task.py +++ b/kpov_judge/tasks/edit_find_grep_compile_convert/task.py @@ -191,35 +191,24 @@ params_meta = { } def task(testip, testuser, passwd, magicstr, dstfile, progname, pubseed): - from pexpect import pxssh import random - N_TITA = 40 - target = pxssh.pxssh() - target.login(testip, testuser, passwd) - target.sendline('ls -l ~/{}'.format(dstfile)) - target.prompt() - dst_ls = target.before - target.sendline('cat ~/{}'.format(dstfile)) - target.prompt() - dst_file_contents = target.before - target.sendline('ls ~/'.format(dstfile)) - target.prompt() - home_ls = target.before - results = { - 'dst_ls': dst_ls, - 'dst_file_contents': dst_file_contents, - 'home_ls': home_ls, - } - tita_return = "" + r = random.Random(pubseed) + tests = [ + ('dst_ls', 'ls -l ~/{}'.format(dstfile)), + ('dst_file_contents', 'cat ~/{}'.format(dstfile)), + ('home_ls', 'ls ~/'.format(dstfile)), + ] + + N_TITA = 40 for i in range(N_TITA): b = r.randint(0, 7) x = oct(r.randint(37, 127)).replace('o', '') - target.sendline('echo -e "\\{}" | ~/{} {}'.format(x, progname, b)) - target.prompt() - tita_return += target.before - results['tita_return'] = tita_return - target.logout() + tests += [('tita-{:02}'.format(i), 'echo -e "\\{}" | ~/{} {}'.format(x, progname, b))] + + results = kpov_util.ssh_test(testip, testuser, passwd, tests) + results['tita_return'] = ''.join(val for key, val in results.items() if key.startswith('tita-')) + return results def gen_params(user_id, params_meta): @@ -258,11 +247,6 @@ def task_check(results, params): x = start + mid + end hidden_contents += x + "\r\n" expected_contents = hidden_contents - #expected_contents = re.sub(r"^po.*lz\r$", - # r"pokakalz\r", - # hidden_contents, - # re.MULTILINE) - expected_contents = "cat ~/{}\r\n".format(params['dstfile']) + expected_contents if results["dst_file_contents"] == expected_contents: score += 3 else: @@ -275,7 +259,7 @@ def task_check(results, params): break hints += ["wrong file contents\n" + str(diff_pos[1])] #hints += ["wrong file contents"] - expected_ls = "ls -l ~/{dstfile}\r\n-rw--w---- 1 {testuser} bilbo .*{dstfile}.*\r\n".format(**params) + expected_ls = "-rw--w---- 1 {testuser} bilbo .*{dstfile}.*\r\n".format(**params) if re.match(expected_ls, results["dst_ls"]): score += 3 else: @@ -290,7 +274,6 @@ def task_check(results, params): b = r.randint(0, 7) x_i = r.randint(37, 127) x = oct(x_i).replace('o', '') - expected_tita += 'echo -e "\\{}" | ~/{} {}\r\n'.format(x, params['progname'], b) if x_i & (1 << b): expected_tita += "ta\r\n" else: @@ -298,7 +281,6 @@ def task_check(results, params): if results["tita_return"] == expected_tita: score += 2 else: - #hints += [u"program output incorrect\n" + results["tita_return"] + expected_tita] hints += ["program output incorrect\n" + results["tita_return"]] return score, hints @@ -306,10 +288,7 @@ def task_check(results, params): def prepare_disks(templates, task_params, global_params): import random import os - #print "Haha!" - #print params - #print templates - d = templates['student-entrance2'] + # first create the file contents to make it easyer to check. hidden_contents = task_params['magicstr'] r = random.Random(task_params['rndseed']) @@ -318,12 +297,15 @@ def prepare_disks(templates, task_params, global_params): x += "".join([r.choice("uiasdfghjkyxcvbnm1234567890ASDFGHJKYZXCVBNM") for i in range(60)]) x += r.choice(["lz", "1z", "Iz", "iz", "l2", "I2", "12"]) hidden_contents += x + "\n" + # create hidden file dir_list = ['Qlipper', 'Thunar', 'blender', 'autostart', 'kazam', 'mc', 'netsurf', 'pulse', 'qupzilla', 'radare2', 'teamviewer', 'texstudio', 'vlc'] ending_list = ['rc', '.conf', ''] start_list = ['net', 'dev', 'doc', 'lib', 'time', 'conf'] r.shuffle(dir_list) file_letters = ["mod", "co"] + + d = templates['student-entrance2'] for potential_dir in dir_list: try: potential_dir = os.path.join('/home/bilbo/.config', potential_dir) @@ -344,6 +326,3 @@ def prepare_disks(templates, task_params, global_params): # TODO create some additional files write_default_config(templates['simpleArbiterDhcpGW'], global_params) - # finish here - # rename - diff --git a/kpov_judge/tasks/ldap_import/task.py b/kpov_judge/tasks/ldap_import/task.py index d66509b..85862ad 100644 --- a/kpov_judge/tasks/ldap_import/task.py +++ b/kpov_judge/tasks/ldap_import/task.py @@ -21,10 +21,6 @@ computers = { 'disks': [ { 'name': 'maliNetworkManager', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -35,10 +31,6 @@ computers = { 'disks': [ { 'name': 'maliBrezNetworkManager', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -48,12 +40,7 @@ computers = { 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiterDhcp', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}], 'flavor': 'm1.tiny', diff --git a/kpov_judge/tasks/mock_entrance_exam/task.py b/kpov_judge/tasks/mock_entrance_exam/task.py index 1d113b5..68f59cb 100644 --- a/kpov_judge/tasks/mock_entrance_exam/task.py +++ b/kpov_judge/tasks/mock_entrance_exam/task.py @@ -136,42 +136,22 @@ params_meta = { def task(student_IP, net_prog_name, P_c, P_executable, arg_c, env_c, out_stderr_c, out_stdout_c, P_script, param_gen_seed): - from pexpect import pxssh import random - results = {} + r = random.Random(int(param_gen_seed)) - try: - s = pxssh.pxssh(encoding='utf-8') - s.login(student_IP, 'student', 'vaje') - results['ssh'] = True - env_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(11)]) - arg_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(13)]) - stdin_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(17)]) - s.sendline('ls -l {}'.format(P_script)) - s.prompt() - results['script_ls'] = s.before - s.sendline('ls -l {}'.format(P_executable)) - s.prompt() - results['executable_ls'] = s.before - s.sendline('export {}={}; {}'.format(env_c, env_val, P_script)) - s.prompt() - results['script_run'] = s.before - s.sendline('cat {}'.format(out_stderr_c)) - s.prompt() - results['script_stderr'] = s.before - s.sendline('cat {}'.format(out_stdout_c)) - s.prompt() - results['script_stdout'] = s.before - s.sendline('echo "{}" | {} "{}" 2> /dev/null'.format(stdin_val, P_executable, arg_val)) - s.prompt() - results['prog_stdout'] = s.before - s.sendline('echo "{}" | {} "{}" > /dev/null'.format(stdin_val, P_executable, arg_val)) - s.prompt() - results['prog_stderr'] = s.before - s.logout() - except pxssh.ExceptionPxssh as ex: - results['ssh'] = str(ex) - return results + env_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(11)]) + arg_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(13)]) + stdin_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in range(17)]) + + return kpov_util.ssh_test(student_IP, 'student', 'vaje', ( + ('script_ls', 'ls -l {}'.format(P_script)), + ('executable_ls', 'ls -l {}'.format(P_executable)), + ('script_run', 'export {}={}; {}'.format(env_c, env_val, P_script)), + ('script_stderr', 'cat {}'.format(out_stderr_c)), + ('script_stdout', 'cat {}'.format(out_stdout_c)), + ('prog_stdout', 'echo "{}" | {} "{}" 2> /dev/null'.format(stdin_val, P_executable, arg_val)), + ('prog_stderr', 'echo "{}" | {} "{}" > /dev/null'.format(stdin_val, P_executable, arg_val)), + )) def gen_params(user_id, params_meta): import random diff --git a/kpov_judge/tasks/nat_port_forward/task.py b/kpov_judge/tasks/nat_port_forward/task.py index 7613c4a..e96a8e7 100644 --- a/kpov_judge/tasks/nat_port_forward/task.py +++ b/kpov_judge/tasks/nat_port_forward/task.py @@ -19,10 +19,6 @@ computers = { 'disks': [ { 'name': 'maliNetworkManager', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'test-net'}], 'flavor': 'm1.tiny', @@ -33,10 +29,6 @@ computers = { 'disks': [ { 'name': 'student-NATServer', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}], 'flavor': 'm1.tiny', @@ -46,12 +38,7 @@ computers = { 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiter', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -70,18 +57,15 @@ params_meta = { } def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET): - import pexpect - from pexpect import pxssh - import re import random import time - # return None - peer_user = 'student' - peer_password = 'vaje' + import pexpect + from pexpect import pxssh + 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) + tcpdump = pexpect.spawn('sudo /usr/sbin/tcpdump src host {} and dst port {}'.format(IP_TEST_CLIENT, PORT_INNER), encoding='utf-8') + sshconn = pxssh.pxssh(encoding='utf-8') + sshconn.login(IP_TEST_CLIENT, 'student', 'vaje') r = random.Random() k = r.randint(10, 15) results['pre_nc'] = str(k) @@ -92,7 +76,7 @@ def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET): sshconn.sendintr() sshconn.prompt() results['nc_pre'] += sshconn.before - nc = pexpect.spawn('nc -l -p {}'.format(PORT_INNER)) + nc = pexpect.spawn('nc -l -p {}'.format(PORT_INNER), encoding='utf-8') sshconn.sendline() sshconn.prompt() sshconn.sendline("nc {} {}".format(IP_NAT, PORT_OUTER)) @@ -102,8 +86,8 @@ def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET): 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)) + results['route'] = pexpect.run('ip route list 0/0', encoding='utf-8') + results['traceroute'] = pexpect.run('traceroute {}'.format(IP_TEST_CLIENT), encoding='utf-8') # wait for traceroute time.sleep(10) tcpdump.sendintr() @@ -118,7 +102,6 @@ def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET): 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'] = str(r.randint(6000, 10000)) params['PORT_OUTER'] = str(r.randint(10001, 15000)) params['NET'] = kpov_util.IPv4_subnet_gen(r, "10.36.0.0/14", 24) @@ -127,12 +110,6 @@ def gen_params(user_id, params_meta): def task_check(results, params): import re import pickle - #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 hints = [] local_net = params['NET'][:params['NET'].rfind('.')] diff --git a/kpov_judge/tasks/nat_vlc/task.py b/kpov_judge/tasks/nat_vlc/task.py index 89d0f13..ed24b95 100644 --- a/kpov_judge/tasks/nat_vlc/task.py +++ b/kpov_judge/tasks/nat_vlc/task.py @@ -20,7 +20,7 @@ ustvarite še uporabnika {IP_NAT_user}. Poskrbi, da bo SimpleArbiter prek DHCP dobil naslov {IP_simple}. Poskrbi, da bo NATServer deloval kot prehod za SimpleArbiter in izvajal NAT. """, - 'en':""" + 'en':""" Set up two virtual machines - SimpleArbiter (using the disc simpleArbiter) and NATServer. NATServer should have two network adapters. Connect the first adapter to SimpleArbiter and the second adapter to the Internet. Configure the @@ -38,12 +38,7 @@ computers = { 'NATServer': { 'disks': [ { 'name': 'student-NATServer', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}], 'flavor': 'm1.tiny', @@ -53,17 +48,11 @@ computers = { 'disks': [ { 'name': 'simpleArbiter', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', 'config_drive': False - }, - } networks = { 'net1': {'public': False}, 'test-net': {'public': True} } @@ -77,24 +66,19 @@ params_meta = { } def task(IP_simple, IP_NAT, IP_NAT_user, IP_NAT_passwd): - from pexpect import pxssh import pexpect - results = dict() - # Connect to NAT - povezava = pxssh.pxssh() - povezava.login(IP_NAT,IP_NAT_user,IP_NAT_passwd) + + results = kpov_util.ssh_test(IP_NAT, IP_NAT_user, IP_NAT_passwd, ( + ('IP_NAT_ip_forward', 'cat /proc/sys/net/ipv4/ip_forward'), + )) + # Check if If IP_simple is connected to NAT - results['IP_simple_ping_to_NAT'] = pexpect.run('ping -c 5 {}'.format(IP_NAT)) + results['IP_simple_ping_to_NAT'] = pexpect.run('ping -c 5 {}'.format(IP_NAT), encoding='utf-8') # Check routing table on IP_simple - results['IP_simple_routing_table'] = pexpect.run('/sbin/route -n') + results['IP_simple_routing_table'] = pexpect.run('route -n', encoding='utf-8', env={'PATH': '/bin:/sbin'}) # Tracert Check if IP_simple is connected to internet - povezava.prompt() - results['IP_simple_to_internet'] = pexpect.run('/usr/sbin/traceroute 8.8.8.8') - povezava.prompt() - povezava.sendline('cat /proc/sys/net/ipv4/ip_forward') - povezava.prompt() - results['IP_NAT_ip_forward'] = povezava.before - povezava.logout() + results['IP_simple_to_internet'] = pexpect.run('traceroute 8.8.8.8', encoding='utf-8') + return results def gen_params(user_id, params_meta): @@ -121,8 +105,8 @@ def task_check(results, params): score += 3 else: hints.append("Ping to NAT incorrect") - if results['IP_NAT_ip_forward']=="cat /proc/sys/net/ipv4/ip_forward\r\n1\r\n": - score += 2 + if results['IP_NAT_ip_forward'].strip() == "1": + score += 2 else: hints.append("ip_forward not set on NAT?") rs = r"1 +{0} +\({0}\)".format(params['IP_NAT']) diff --git a/kpov_judge/tasks/openvpn_multiple_hops/task.py b/kpov_judge/tasks/openvpn_multiple_hops/task.py index 5d6b033..73a6c53 100644 --- a/kpov_judge/tasks/openvpn_multiple_hops/task.py +++ b/kpov_judge/tasks/openvpn_multiple_hops/task.py @@ -120,144 +120,32 @@ params_meta = { def task(IP_SimpleArbiterLAN, IP_VPNClient1, IP_LANClient1, IP1_VPNClient2, IP2_VPNClient2, IP_LANClient2, IP_VPNClient3, IP_LANClient3): - - from pexpect import pxssh # Used to set up an SSH connection to a remote machine - import pexpect # Allows the script to spawn a child application and control it as if a human were typing commands - - - # The necessary things we need to check if the task was performed correctly - results = dict() - - # The login params (these must be used on the machines!) - peer_user = 'test' - peer_passwd = 'test' - - ### - # Sets up the SSH connections to the machines - ### - # VPNClient1 - sC1 = pxssh.pxssh() - - # Logs in to the machines with the default login params - sC1.login( - IP_LANClient1, - peer_user, - peer_passwd - ) - - ###### - # Ukazi za A - ###### - - # ifconfig -a - sC1.sendline('/sbin/ifconfig -a') - sC1.prompt() - results['VPNClient1_ifconfig'] = sC1.before - - # route -n - sC1.sendline('/sbin/route -n') - sC1.prompt() - results['VPNClient1_route'] = sC1.before - - # ping C2 - sC1.sendline('ping -c 3 {}'.format(IP1_VPNClient2)) - sC1.prompt() - results['VPNClient1_ping1_C2'] = sC1.before - - sC1.sendline('ping -c 3 {}'.format(IP2_VPNClient2)) - sC1.prompt() - results['VPNClient1_ping2_C2'] = sC1.before - - # ping C3 - sC1.sendline('ping -c 3 {}'.format(IP_VPNClient3)) - sC1.prompt() - results['VPNClient1_ping_C3'] = sC1.before - - # traceroute na C - - sC1.sendline('traceroute {}'.format(IP_VPNClient3)) - sC1.prompt() - results['VPNClient1_traceroute_C3'] = sC1.before - - sC1.logout() - - ###### - # Ukazi za B - ###### - - # VPNClient2 - sC2 = pxssh.pxssh() - - sC2.login( - IP_LANClient2, - peer_user, - peer_passwd - ) - - - # ifconfig -a - sC2.sendline('/sbin/ifconfig -a') - sC2.prompt() - results['VPNClient2_ifconfig'] = sC2.before - - # route -n - sC2.sendline('/sbin/route -n') - sC2.prompt() - results['VPNClient2_route'] = sC2.before - - # ping C2 - sC2.sendline('ping -c 3 {}'.format(IP_VPNClient1)) - sC2.prompt() - results['VPNClient2_ping_C1'] = sC2.before - - # ping C3 - - sC2.sendline('ping -c 3 {}'.format(IP_VPNClient3)) - sC2.prompt() - results['VPNClient2_ping_C3'] = sC2.before - - sC2.logout() - - ###### - # Ukazi za C - ###### - - # VPNClient3 - sC3 = pxssh.pxssh() - - sC3.login( - IP_LANClient3, - peer_user, - peer_passwd - ) - - # ifconfig -a - sC3.sendline('/sbin/ifconfig -a') - sC3.prompt() - results['VPNClient3_ifconfig'] = sC3.before - - # route -n - sC3.sendline('/sbin/route -n') - sC3.prompt() - results['VPNClient3_route'] = sC3.before - - # ping C2 - sC3.sendline('ping -c 3 {}'.format(IP2_VPNClient2)) - sC3.prompt() - results['VPNClient3_ping_C2'] = sC3.before - - # ping C3 - sC3.sendline('ping -c 3 {}'.format(IP_VPNClient1)) - sC3.prompt() - results['VPNClient3_ping_C1'] = sC3.before - - # traceroute na C - sC3.sendline('traceroute {}'.format(IP_VPNClient1)) - sC3.prompt() - results['VPNClient3_traceroute_C1'] = sC3.before + tests = { + ('VPNClient1', IP_LANClient1): [ + ('VPNClient1_ping_C2', 'ping -c 3 {}'.format(IP1_VPNClient2)), + ('VPNClient1_ping_C3', 'ping -c 3 {}'.format(IP_VPNClient3)), + ('VPNClient1_traceroute_C3', 'traceroute {}'.format(IP_VPNClient3)), + ], + ('VPNClient2', IP_LANClient2): [ + ('VPNClient2_ping_C1', 'ping -c 3 {}'.format(IP_VPNClient1)), + ('VPNClient2_ping_C3', 'ping -c 3 {}'.format(IP_VPNClient3)), + ], + ('VPNClient3', IP_LANClient3): [ + ('VPNClient3_ping_C1', 'ping -c 3 {}'.format(IP_VPNClient1)), + ('VPNClient3_ping_C2', 'ping -c 3 {}'.format(IP2_VPNClient2)), + ('VPNClient3_traceroute_C1', 'traceroute {}'.format(IP_VPNClient1)), + ], + } - sC3.logout() + for (name, host), host_tests in tests.items(): + host_tests += [ + (name+'_ifconfig', '/sbin/ifconfig -a'), + (name+'_route', '/sbin/route -n'), + ] + results = collections.defaultdict(str) + for (name, host), host_tests in tests.items(): + results.update(kpov_util.ssh_test(host, 'test', 'test', host_tests)) return results def gen_params(user_id, params_meta): @@ -296,18 +184,18 @@ def task_check(results, params): rs = r"tun.*\n.*inet.*{}".format(IP2_C2) if re.search(rs, results['VPNClient2_ifconfig']): score += 1 - else: - hints.append("ifconfig on VPNClient2 is not OK") + else: + hints.append("ifconfig on VPNClient2 is not OK") pass else: - hints.append("ifconfig on VPNClient2 is not OK") + hints.append("ifconfig on VPNClient2 is not OK") pass # C3 rs = r"tun0.*\n.*inet.*{}".format(IP_C3) if re.search(rs, results['VPNClient3_ifconfig']): score += 1 else: - hints.append("ifconfig on VPNClient3 is not OK") + hints.append("ifconfig on VPNClient3 is not OK") pass # testi za route # C1 @@ -321,22 +209,22 @@ def task_check(results, params): if re.search(rs, results['VPNClient1_route']): rs = r"{} {}.*tun0".format(ASD, IP1_C2) if re.search(rs, results['VPNClient1_route']): - score += 1 + score += 1 else: hints.append("route on VPNClient1 is not OK") else: - hints.append("route on VPNClient1 is not OK") + hints.append("route on VPNClient1 is not OK") pass # C2 rs = r"{}.*tun".format(IP_C1) if re.search(rs, results['VPNClient2_route']): rs = r"{}.*tun".format(IP_C3) if re.search(rs, results['VPNClient2_route']): - score += 1 + score += 1 else: hints.append("route on VPNClient2 is not OK") else: - hints.append("route on VPNClient2 is not OK") + hints.append("route on VPNClient2 is not OK") pass # C3 rs = r"{}.*tun0".format(IP2_C2) @@ -349,32 +237,32 @@ def task_check(results, params): if re.search(rs, results['VPNClient3_route']): rs = r"{} {}.*tun0".format(ASD, IP2_C2) if re.search(rs, results['VPNClient3_route']): - score += 1 + score += 1 else: hints.append("route on VPNClient3 is not OK") else: - hints.append("route on VPNClient3 is not OK") + hints.append("route on VPNClient3 is not OK") pass # testi za ping # C1 rs = r"64 bytes from {}: icmp_seq=[0-9]+ ttl=[0-9]+ time=\d+\.\d+ ms".format(IP1_C2) - if re.search(rs, results['VPNClient1_ping1_C2']): - score += 0.5 + if re.search(rs, results['VPNClient1_ping_C2']): + score += 0.5 else: - hints.append("ping from VPNClient1 to VPNClient2 is not OK") + hints.append("ping from VPNClient1 to VPNClient2 is not OK") pass rs = r"64 bytes from {}: icmp_seq=[0-9]+ ttl=[0-9]+ time=\d+\.\d+ ms".format(IP_C3) if re.search(rs, results['VPNClient1_ping_C3']): - score += 0.5 + score += 0.5 else: - hints.append("ping from VPNClient1 to VPNClient3 is not OK") + hints.append("ping from VPNClient1 to VPNClient3 is not OK") pass # C2 rs = r"64 bytes from {}: icmp_seq=[0-9]+ ttl=[0-9]+ time=\d+\.\d+ ms".format(IP_C1) if re.search(rs, results['VPNClient2_ping_C1']): - score += 0.5 + score += 0.5 else: - hints.append("ping from VPNClient2 to VPNClient1 is not OK") + hints.append("ping from VPNClient2 to VPNClient1 is not OK") pass rs = r"64 bytes from {}: icmp_seq=[0-9]+ ttl=[0-9]+ time=\d+\.\d+ ms".format(IP_C3) if re.search(rs, results['VPNClient2_ping_C3']): @@ -385,15 +273,15 @@ def task_check(results, params): # C3 rs = r"64 bytes from {}: icmp_seq=[0-9]+ ttl=[0-9]+ time=\d+\.\d+ ms".format(IP_C1) if re.search(rs, results['VPNClient3_ping_C1']): - score += 0.5 + score += 0.5 else: - hints.append("ping from VPNClient3 to VPNClient1 is not OK") + hints.append("ping from VPNClient3 to VPNClient1 is not OK") pass rs = r"64 bytes from {}: icmp_seq=1 ttl=[0-9]+ time=\d+\.\d+ ms".format(IP2_C2) if re.search(rs, results['VPNClient3_ping_C2']): - score += 0.5 + score += 0.5 else: - hints.append("ping from VPNClient3 to VPNClient2 is not OK") + hints.append("ping from VPNClient3 to VPNClient2 is not OK") pass #score = int(score) @@ -405,7 +293,7 @@ def task_check(results, params): if re.search(rs, results['VPNClient1_traceroute_C3']): score += 1 else: - hints.append("traceroute from VPNClient1 to VPNClient3 is not OK") + hints.append("traceroute from VPNClient1 to VPNClient3 is not OK") pass else: hints.append("traceroute from VPNClient1 to VPNClient3 is not OK") @@ -417,7 +305,7 @@ def task_check(results, params): if re.search(rs, results['VPNClient3_traceroute_C1']): score += 1 else: - hints.append("traceroute from VPNClient1 to VPNClient3 is not OK") + hints.append("traceroute from VPNClient1 to VPNClient3 is not OK") pass else: hints.append("traceroute from VPNClient1 to VPNClient3 is not OK") @@ -430,5 +318,3 @@ def task_check(results, params): def prepare_disks(templates, task_params, global_params): write_default_config(templates['simpleArbiterDhcp'], global_params) - - diff --git a/kpov_judge/tasks/openvpn_simple_smb/task.py b/kpov_judge/tasks/openvpn_simple_smb/task.py index 435b40f..fda7a4a 100644 --- a/kpov_judge/tasks/openvpn_simple_smb/task.py +++ b/kpov_judge/tasks/openvpn_simple_smb/task.py @@ -102,60 +102,43 @@ params_meta = { def task(IP_SimpleArbiterVPN, IP_VPNClient1, IP_LANClient1, DIRNAME): - + import collections from pexpect import pxssh # Used to set up an SSH connection to a remote machine import pexpect # Allows the script to spawn a child application and control it as if a human were typing commands - - + # The necessary things we need to check if the task was performed correctly - results = dict() + results = collections.defaultdict(str) - # The login params (these must be used on the machines!) - peer_user = 'student' - peer_passwd = 'vaje' - - ### - # Sets up the SSH connections to the machines - ### # VPNClient1 - sC1 = pxssh.pxssh() - - # Logs in to the machines with the default login params - sC1.login( - IP_LANClient1, - peer_user, - peer_passwd - ) + sC1 = pxssh.pxssh(encoding='utf-8') + sC1.login(IP_LANClient1, 'student', 'vaje') - ###### # sA - ###### - results['SimpleArbiter_ifconfig'] = pexpect.run( - '/sbin/ifconfig -a') - + 'ifconfig -a', encoding='utf-8', env={'PATH': '/bin:/sbin'}) results['SimpleArbiter_route'] = pexpect.run( - '/sbin/route -n') + 'route -n', encoding='utf-8', env={'PATH': '/bin:/sbin'}) # Pings each of the clients # 10.8.0.6 and 10.8.0.10 are the first two default addresses distributed by OpenVPN # Will output everything ping outputs (set to ping 3 times) results['SimpleArbiter_ping_C1'] = pexpect.run( - 'ping -c 3 {}'.format(IP_VPNClient1)) + 'ping -c 3 {}'.format(IP_VPNClient1), encoding='utf-8') results['SimpleArbiter_traceroute'] = pexpect.run( - '/usr/bin/traceroute {}'.format(IP_VPNClient1)) + 'traceroute {}'.format(IP_VPNClient1), encoding='utf-8') sC1.sendline('cat /etc/exports') sC1.prompt() output = sC1.before results['VPNClient1_nfs_access_control_list'] = output results['SimpleArbiter_mount'] = pexpect.run( - 'sudo mount {}:/home/test/{} /mnt'.format(IP_VPNClient1, DIRNAME)) + 'sudo mount {}:/home/test/{} /mnt'.format(IP_VPNClient1, DIRNAME), encoding='utf-8') results['SimpleArbiter_mount_result'] = pexpect.run( - 'sudo mount') + 'sudo mount', encoding='utf-8') results['SimpleArbiter_ls'] = pexpect.run( - 'ls /mnt') + 'ls /mnt', encoding='utf-8') pexpect.run( - 'sudo umount /mnt') + 'sudo umount /mnt', encoding='utf-8') + # Ping the VPN server sC1.sendline('ping -c 3 {0}'.format( IP_SimpleArbiterVPN )) sC1.prompt() @@ -169,10 +152,8 @@ def task(IP_SimpleArbiterVPN, IP_VPNClient1, IP_LANClient1, DIRNAME): sC1.prompt() results['VPNClient1_ps'] = sC1.before sC1.logout() - - - return results + return results def gen_params(user_id, params_meta): params = dict() @@ -189,64 +170,46 @@ def gen_params(user_id, params_meta): def task_check(results, params): - import re score = 0 hints = [] - # zal si se nisem prišla na jasno s pingi + IP_SA = params['IP_SimpleArbiterVPN'].replace('.', '\.') IP_C1 = params['IP_VPNClient1'].replace('.', '\.') - rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format( - IP_SA) - # print rs, re.match(rs, results['SimpleArbiter_ifconfig']) + rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format(IP_SA) if re.search(rs, results['SimpleArbiter_ifconfig']): score += 1 - # print "ifconfig OK" else: hints.append("ifconfig on SimpleArbiter not OK") - pass - # print ('SA_ifconfig', results['SimpleArbiter_ifconfig']) - # results['SimpleArbiter_route'] = pexpect.run( if re.search( - "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format( - IP_C1), + "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format(IP_C1), results['SimpleArbiter_ping_C1']): - # print "Server ping OK" score += 1 else: hints.append("ping from server not OK") - pass - # print ("Server ping", results['SimpleArbiter_ping_C1']) - # ignore this - # print results['SimpleArbiter_mount'] - # print results['SimpleArbiter_traceroute'] rs = "1 +{0} \({0}\)".format(IP_C1) - if re.search(rs, - results['SimpleArbiter_traceroute']): + if re.search(rs, results['SimpleArbiter_traceroute']): score += 1 else: hints.append("traceroute not OK") - pass - # print ("fail!", rs, results['SimpleArbiter_traceroute']) if results['VPNClient1_nfs_access_control_list'].find( '/home/test/' + params['DIRNAME'] + ' ') >= 0: score += 1 if results['SimpleArbiter_mount_result'].find( - '{}:/home/test/{} on /mnt type nfs'.format( - params['IP_VPNClient1'], - params['DIRNAME'])): - # print "mount OK" + '{}:/home/test/{} on /mnt type nfs'.format( + params['IP_VPNClient1'], params['DIRNAME'])): score += 1 else: hints.append("mount not OK") + # get r into the correct state r = random.Random(params['secret_random_seed']) - s = "\n".join([ - "".join([r.choice("0123456789abcdef") for i in range(32)]) + s = "\n".join(["".join([r.choice("0123456789abcdef") for i in range(32)]) for i in range(16)]) keyfile = kpov_util.fname_gen(r, extension=False) + # now check the filenames fnames_ok = True for i in range(3): @@ -254,34 +217,24 @@ def task_check(results, params): foo = kpov_util.fortune(r, 4096) pos = results['SimpleArbiter_ls'].find(fname + '.txt') fnames_ok = fnames_ok and pos >= 0 - #if pos < 0: - # hints.append("missing file:" + fname) if fnames_ok: score += 2 else: hints.append("shared filenames not OK:") + # Ping the VPN server if re.search( - "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format( - IP_SA), + "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format(IP_SA), results['VPNClient1_ping_VPN_server']): - # print "ping OK" score += 1 else: hints.append("ping from client not OK") - pass - # print "Client ping", results['VPNClient1_ping_VPN_server'] - rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format( - IP_C1) - if re.search(rs, - results['VPNClient1_ifconfig']): + rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format(IP_C1) + if re.search(rs, results['VPNClient1_ifconfig']): score += 1 - # print "ifconfig OK" else: hints.append("ifconfig on VPNClient1 not OK") - pass - # print ('VPNClient1_ifconfig', results['VPNClient1_ifconfig']) if results['VPNClient1_ps'].find('openvpn') > 0: score += 1 @@ -289,11 +242,7 @@ def task_check(results, params): hints.append("openvpn not found running on VPNClient") return score, hints - def prepare_disks(templates, task_params, global_params): - - #d = templates['simpleArbiterDhcp'] - #guestmount -a d -m /dev/VG/LV -m /dev/sda1:/boot --ro /mnt #asistent je pocasnela :) import random @@ -327,6 +276,3 @@ iface tap0 inet static templates['student-VPNClient1'].write("/home/student/" + keyfile, s) # uid, gid (student = ) templates['student-VPNClient1'].chown(1000, 1000, "/home/student/" + keyfile) - - - diff --git a/kpov_judge/tasks/openwrt/task.py b/kpov_judge/tasks/openwrt/task.py index b55905c..98f1420 100644 --- a/kpov_judge/tasks/openwrt/task.py +++ b/kpov_judge/tasks/openwrt/task.py @@ -18,10 +18,6 @@ computers = { 'disks': [ { 'name': 'maliNetworkManager', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -32,10 +28,6 @@ computers = { 'disks': [ { 'name': 'maliBrezNetworkManager', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -45,12 +37,7 @@ computers = { 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiterDhcp', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}], 'flavor': 'm1.tiny', diff --git a/kpov_judge/tasks/radius_mysql_pam/task.py b/kpov_judge/tasks/radius_mysql_pam/task.py index 1a11ef8..be99e6a 100644 --- a/kpov_judge/tasks/radius_mysql_pam/task.py +++ b/kpov_judge/tasks/radius_mysql_pam/task.py @@ -58,10 +58,6 @@ computers = { 'disks': [ { 'name': 'student-RadiusServer', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', @@ -71,12 +67,7 @@ computers = { 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiterDhcpGW', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}], 'flavor': 'm1.tiny', @@ -97,20 +88,23 @@ params_meta = { 'MYSQL_SEED':{'descriptions': {'si': 'seed', 'en': 'seed'}, 'w': False, 'public':True, 'type': None, 'generated': True}, } -def task(IP_RS, RADIUS_SECRET, RADIUS_USERNAME, RADIUS_PASSWORD, MYSQL_DB_NAME, MYSQL_ADMIN_USER, MYSQL_ADMIN_PASSWORD, MYSQL_SEED): - from pexpect import pxssh - import pexpect +def task(IP_RS, RADIUS_SECRET, RADIUS_USERNAME, RADIUS_PASSWORD, + MYSQL_DB_NAME, MYSQL_ADMIN_USER, MYSQL_ADMIN_PASSWORD, MYSQL_SEED): + import collections import random - results = dict() - peer_user = 'student' - peer_passwd = 'vaje' + import pexpect + r = random.Random(MYSQL_SEED) MYSQL_TEST_USER = kpov_util.username_gen(r) MYSQL_TEST_PASSWORD = kpov_util.alnum_gen(r, 7) RADIUS_NEW_PASSWORD = kpov_util.alnum_gen(r, 7) - # Testiranje radius strtežnika + + results = collections.defaultdict(str) + + # Testiranje radius strežnika results['Test_RadiusServer'] = pexpect.run('radtest {0} {1} {2} 1812 {3}'.format( RADIUS_USERNAME, RADIUS_PASSWORD, IP_RS, RADIUS_SECRET)) + # Testiranje podatkovne base mysql mysql = pexpect.spawn('mysql -u {MYSQL_ADMIN_USER} -p{MYSQL_ADMIN_PASSWORD} -h {IP_RS}'.format(**locals())) mysql.expect("mysql>") @@ -123,22 +117,25 @@ def task(IP_RS, RADIUS_SECRET, RADIUS_USERNAME, RADIUS_PASSWORD, MYSQL_DB_NAME, results['select_from_users'] = mysql.before mysql.sendline("INSERT INTO radcheck (UserName, Attribute, Value, Op) VALUES ('{MYSQL_TEST_USER}', 'Cleartext-Password', '{MYSQL_TEST_PASSWORD}', ':=');".format(**locals())) mysql.expect("mysql>") + results['radtest_OK'] = pexpect.run('radtest {0} {1} {2} 1812 {3}'.format( MYSQL_TEST_USER, MYSQL_TEST_PASSWORD, IP_RS, RADIUS_SECRET)) results['radtest_NOK'] = pexpect.run('radtest {0} {1} {2} 1812 {3}'.format( MYSQL_TEST_USER, "Flügzeug", IP_RS, RADIUS_SECRET)) results['radtest_NOK'] = pexpect.run('radtest {0} {1} {2} 1812 {3}'.format( MYSQL_TEST_USER, "Flügzeug", IP_RS, RADIUS_SECRET)) - sT = pxssh.pxssh() + mysql.sendline("UPDATE radcheck SET value='{RADIUS_NEW_PASSWORD}' where UserName='{RADIUS_USERNAME}' and Attribute='Cleartext-Password';".format(**locals())) - sT.login(IP_RS, RADIUS_USERNAME, RADIUS_NEW_PASSWORD) - results['login_test'] = sT.before + + results.update(kpov_util.ssh_test(IP_RS, RADIUS_USERNAME, RADIUS_NEW_PASSWORD)) + mysql.sendline("UPDATE radcheck SET value='{RADIUS_PASSWORD}' where UserName='{RADIUS_USERNAME}' and Attribute='Cleartext-Password';".format(**locals())) mysql.expect('mysql>') mysql.sendline("DELETE FROM radcheck where UserName='{MYSQL_TEST_USER}' and Attribute='Cleartext-Password';".format(**locals())) mysql.expect('mysql>') mysql.sendline('\q'); - # Testiranje PAM s testnim uporabnikom + # TODO Testiranje PAM s testnim uporabnikom + return results def gen_params(user_id, params_meta): @@ -209,8 +206,8 @@ def task_check(results, params): else: hints.append('radtest negative output incorrect: ' + results['radtest_NOK']) print((results['radtest_NOK'], s)) - s = "{RADIUS_USERNAME}@.*:~\\$".format(**params) - if re.search(s, results['login_test'], flags=re.DOTALL): + s = "{RADIUS_USERNAME}@.*:".format(**params) + if re.search(s, results['motd'], flags=re.DOTALL): # print "login_test OK" score += 1 else: @@ -220,4 +217,3 @@ def task_check(results, params): def prepare_disks(templates, task_params, global_params): write_default_config(templates['simpleArbiterDhcpGW'], global_params) - diff --git a/kpov_judge/tasks/rename_grep_network/task.py b/kpov_judge/tasks/rename_grep_network/task.py index cba9ac9..c1be010 100644 --- a/kpov_judge/tasks/rename_grep_network/task.py +++ b/kpov_judge/tasks/rename_grep_network/task.py @@ -152,30 +152,12 @@ params_meta = { } def task(testip, testuser, passwd, mntdir, magicstr): - import collections - from pexpect import pxssh - - commands = [ + return kpov_util.ssh_test(testip, testuser, passwd, ( ('home_ls', 'ls ~/'), ('dst_file_contents', 'cat ~/*I*.txt'), ('dst_ls', 'ls -l ~/*I*.txt'), ('mnt', 'mount'), - ] - - results = collections.defaultdict(str) - try: - s = pxssh.pxssh(encoding='utf-8') - s.login(testip, testuser, passwd) - results['ssh'] = True - for test, command in commands: - s.sendline(command) - s.prompt() - if test: - results[test] = s.before - s.logout() - except Exception as ex: - results['ssh'] = str(ex) - return results + )) def gen_params(user_id, params_meta): import random @@ -216,7 +198,6 @@ def task_check(results, params): # r"pokakalz\r", # hidden_contents, # re.MULTILINE) - expected_contents = "cat ~/*I*.txt\r\n".format(dstfile) + expected_contents if results["dst_file_contents"] == expected_contents: score += 3 else: @@ -228,9 +209,8 @@ def task_check(results, params): diff_pos = (i, results["dst_file_contents"][start:end]) break hints += ["wrong file contents\n" + str(diff_pos[1])] - #hints += ["wrong file contents"] params['dstfile'] = dstfile - expected_dst_ls = "ls -l ~/\\*I\\*.txt\r\n-rw--w---- 1 {testuser} bilbo .*{dstfile}\r\n".format(**params) + expected_dst_ls = "-rw--w---- 1 {testuser} bilbo .*{dstfile}".format(**params) if re.match(expected_dst_ls, results["dst_ls"]): score += 2 else: @@ -253,10 +233,7 @@ def task_check(results, params): def prepare_disks(templates, task_params, global_params): import random import os - #print "Haha!" - #print params - #print templates - d = templates['smallstudent-personal'] + # first create the file contents to make it easyer to check. hidden_contents = task_params['magicstr'] r = random.Random(task_params['rndseed']) @@ -266,6 +243,7 @@ def prepare_disks(templates, task_params, global_params): x += "".join([r.choice("uiasdfghjkyxcvbnm1234567890ASDFGHJKYZXCVBNM") for i in range(60)]) x += r.choice(["lz", "1z", "Iz", "iz", "l2", "I2", "12"]) hidden_contents += x + "\n" + # create hidden file dir_list = ['Qlipper', 'Thunar', 'blender', 'autostart', 'kazam', 'mc', 'netsurf', 'pulse', 'qupzilla', 'radare2', 'teamviewer', 'texstudio', 'vlc'] ending_list = ['rc', '.conf', '', '.txt'] @@ -274,6 +252,8 @@ def prepare_disks(templates, task_params, global_params): start_list.append("".join([r.choice("qQoOp") for i in range(64)]) + "O") r.shuffle(dir_list) file_letters = ["mod", "co"] + + d = templates['smallstudent-personal'] d.mkdir('/mnt/.hideme') d.mkdir('/media/.hideme') for potential_dir in dir_list: @@ -304,6 +284,3 @@ def prepare_disks(templates, task_params, global_params): # TODO create some additional files # write_default_config(templates['simpleArbiterDhcpGW'], global_params) - # finish here - # rename - diff --git a/kpov_judge/tasks/set_ip_static_dhcp/task.py b/kpov_judge/tasks/set_ip_static_dhcp/task.py index 4cdbc14..3a3fcc6 100644 --- a/kpov_judge/tasks/set_ip_static_dhcp/task.py +++ b/kpov_judge/tasks/set_ip_static_dhcp/task.py @@ -85,23 +85,17 @@ params_meta = { def task(IP_NM, DNS_NM, IP_static, DNS_static): import collections - from pexpect import pxssh - tests = ['nmcli -c no d', 'nslookup www.arnes.si'] - results = collections.defaultdict(str) + tests = ( + ('nmcli', 'nmcli -c no d'), + ('nslookup', 'nslookup www.arnes.si'), + ) + results = collections.defaultdict(str) for name, host in [('nm', IP_NM), ('static', IP_static)]: - try: - s = pxssh.pxssh(encoding='utf-8') - s.login(host, 'student', 'vaje') - results['ssh-'+name] = True - for test in tests: - s.sendline(test) - s.prompt() - results[test.split()[0]+'-'+name] = s.before - s.logout() - except Exception as ex: - results['ssh'] = str(ex) + host_results = kpov_util.ssh_test(host, 'student', 'vaje', tests) + for key, value in host_results.items(): + results[key+'-'+name] = value return results def gen_params(user_id, params_meta): diff --git a/kpov_judge/tasks/set_motd/task.py b/kpov_judge/tasks/set_motd/task.py index 956f570..5f1b0b3 100644 --- a/kpov_judge/tasks/set_motd/task.py +++ b/kpov_judge/tasks/set_motd/task.py @@ -76,20 +76,7 @@ params_meta = { def task(peer_ip, peer_user, peer_passwd, niz): "Check whether ssh works" - import collections - from pexpect import pxssh - results = collections.defaultdict(str) - try: - s = pxssh.pxssh(encoding='utf-8') - s.login(peer_ip, peer_user, peer_passwd, - original_prompt=r'{0}@.*:\~\$'.format(peer_user), - auto_prompt_reset=False) - results['ssh'] = True - results['motd'] = s.before - s.logout() - except Exception as ex: - results['ssh'] = str(ex) - return results + return kpov_util.ssh_test(peer_ip, peer_user, peer_passwd) def gen_params(user_id, params_meta): return kpov_util.default_gen(user_id, params_meta) diff --git a/kpov_judge/tasks/vlc_stream_rtp/task.py b/kpov_judge/tasks/vlc_stream_rtp/task.py index 7948e51..2e731f2 100644 --- a/kpov_judge/tasks/vlc_stream_rtp/task.py +++ b/kpov_judge/tasks/vlc_stream_rtp/task.py @@ -79,7 +79,6 @@ def task(NASLOV, TOK): results['ps'] = pexpect.run('ps xa') results['tcpdump_hex'] = pexpect.run('sudo /usr/sbin/tcpdump -x -c 2 dst host 239.255.255.255 and port 9875'.format(NASLOV)) results['tcpdump'] = pexpect.run('sudo /usr/sbin/tcpdump -c 8 dst host {}'.format(NASLOV)) - # print("HAHA") return results def gen_params(user_id, params_meta): -- cgit v1.2.1