diff options
author | gasperfele@fri1.uni-lj.si <gasperfele@fri1.uni-lj.si@5cf9fbd1-b2bc-434c-b4b7-e852f4f63414> | 2017-10-23 07:53:50 +0000 |
---|---|---|
committer | gasperfele@fri1.uni-lj.si <gasperfele@fri1.uni-lj.si@5cf9fbd1-b2bc-434c-b4b7-e852f4f63414> | 2017-10-23 07:53:50 +0000 |
commit | bf22716922d0a97a8276a7ffbc4044d5e7adc506 (patch) | |
tree | 562882ac17d8245821ef39677751a781e1209108 /kpov_judge/tasks | |
parent | 57d5ff3ed55e3235051f6c195b7b2cd7b0deb1af (diff) |
Dodana 1., nedelujoca verzija pristopnega kolokvija 2017
git-svn-id: https://svn.lusy.fri.uni-lj.si/kpov-public-svn/kpov-public@422 5cf9fbd1-b2bc-434c-b4b7-e852f4f63414
Diffstat (limited to 'kpov_judge/tasks')
-rw-r--r-- | kpov_judge/tasks/rename_grep_network/task.py | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/kpov_judge/tasks/rename_grep_network/task.py b/kpov_judge/tasks/rename_grep_network/task.py new file mode 100644 index 0000000..cc21479 --- /dev/null +++ b/kpov_judge/tasks/rename_grep_network/task.py @@ -0,0 +1,311 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# TODO: +# - check if everything is filled in (computers, params, preparation) +# - improve scoring +# - test +# - switch to a real SSH/SFTP client to properly handle filenames + + +instructions = { + 'si': u""" +Ustvari dva navidezna računalnika s slikama diskov +- SimpleArbiterExam s sliko diska simpleArbiterDhcp in +SmallStudent s slikama diska student-entrance3 +in smallstudent-personal. + +Drugi razdelek na sliki smallstudent-personal priklopi na imenik {mntdir} + +Na SimpleArbiterExam se lahko prijaviš z uporabniškim imenom tester in geslom test. +Na SmallStudentExam se lahko prijaviš kot root z geslom kaboom. + +Poskrbi, da bo SmallStudent s SimpleArbiter dostopen na naslovu {testip}. + +Na SmallStudent ustvari uporabnika {testuser} z geslom {passwd}. + +Na smallstudent-personal je nekje skrita +datoteka, ki vsebuje niz {magicstr}. +Skopiraj jo v domači imenik {testuser} in preimenuj tako, da vse znake 'O' v imenu zamenjaš z 'I'. +Pazi, da nobena druga datoteka v domačem imeniku v svojem imenu ne bo vsebovala "I". + +Poskrbi, da bo lastnik {testuser}, skupina pa naj ostane nespremenjena. +Brati naj jo ima pravico samo lastnik, pisati lastnik in skupina, poganjati nihče. +""", + 'en': u''' +''', +} + +instructions = {'si': 'Potrpite.', 'en': 'Have patience.'} + +computers = { + 'SimpleArbiter': { + 'disks': [ + { + 'name': 'simpleArbiterDhcpGW', + }, + ], + 'network_interfaces': [ + { + 'network': 'net1', + }, + { + 'network': 'net2', + }, + ], + 'flavor': 'm1.tiny', + 'config_drive': False, + }, + 'SmallStudent': { + 'disks': [ + { + 'name': 'student-entrance3', + }, + { + 'name': 'smallstudent-personal', + } + ], + 'network_interfaces': [ + { + 'network': 'net2', + }, + ], + 'flavor': 'm1.tiny', + 'config_drive': False, + } +} + +networks = { + 'net1': { + 'public': True, + }, + 'net2': { + 'public': False, + } +} + +params_meta = { + 'testip': { + 'descriptions': { + 'si': 'IP SmallStudent', + 'en': 'IP SmallStudent', + }, + 'w': False, + 'public': True, + 'type': 'IP', + 'generated': True, + }, + 'testuser': { + 'descriptions': { + 'si': 'Uporabnik na SmallStudent', + 'en': 'Username on SmallStudent', + }, + 'w': False, + 'public': True, + 'type': 'username', + 'generated': True, + }, + 'passwd': { + 'descriptions': { + 'si': 'Geslo na SmallStudent', + 'en': 'Password on SmallStudent', + }, + 'w': False, + 'public': True, + 'type': None, + 'generated': True, + + }, + 'mntdir': { + 'descriptions': { + 'si': 'imenik za priklop diska', + 'en': 'mountpoint', + } + }, + 'magicstr' : { + 'descriptions': { + 'si': 'Niz v iskani datoteki', + 'en': 'String in the file you need to find', + }, + 'w': False, + 'public': True, + 'type': None, + 'generated': True, + + }, + # 'progname': { + # 'descriptions': { + # 'si': 'Ime programa', + # 'en': 'Program filename', + # }, + # 'w': False, + # 'public': True, + # 'type': 'filename', + # 'generated': True, + # }, + 'pubseed': { + 'descriptions': { + 'si': 'Nekaj nepredvidenega', + 'en': 'A random seed', + }, + 'w': False, + 'public': True, + 'type': None, + 'generated': True, + }, + 'rndseed': { + 'descriptions': { + 'si': 'random seed za skrito datoteko', + 'en': 'random seed for hiding the file', + }, + 'w': False, + 'public': False, + 'type': None, + 'generated': True, + }, +} + +def task(testip, testuser, passwd, mntdir, magicstr, pubseed): + from pexpect import pxssh + import random + target = pxssh.pxssh() + target.login(testip, testuser, passwd) + target.sendline('ls ~/') + target.prompt() + home_ls = target.before + target.sendline('cat ~/*I*.txt') + target.prompt() + dst_file_contents = target.before + target.sendline('ls -l ~/*I*.txt') + target.prompt() + dst_ls = target.before + target.sendline('mount') + target.prompt() + mnt = target.before() + results = { + 'home_ls': home_ls, + 'dst_ls': dst_ls, + 'mnt': mnt, + 'dst_file_contents': dst_file_contents, + } + target.logout() + return results + +def gen_params(user_id, params_meta): + import random + params = dict() + r = random.Random(user_id) + params['testip'] = kpov_random_helpers.IPv4_addr_gen(r, + network = '10.94.80.0/19', n_generated=1)[0] + params['testuser'] = kpov_random_helpers.default_generators['username'](r) + params['passwd'] = kpov_random_helpers.alnum_gen(r, 8) + params['magicstr'] = "".join([r.choice("qwerztlpoQWERTPOL") for i in range(10)]) + params['mntdir'] = "/" + kpov_random_helpers.default_generators['filename'](r) + # params['progname'] = kpov_random_helpers.default_generators['filename'](r) + # while params['dstfile'] == params['progname']: + # params['progname'] = kpov_random_helpers.default_generators['filename'](r) + params['pubseed'] = kpov_random_helpers.alnum_gen(r, 8) + params['rndseed'] = kpov_random_helpers.alnum_gen(r, 8) + return params + + +def task_check(results, params): + import os + import re + hints = [] + score = 0 + hidden_contents = params['magicstr'] + r = random.Random(params['rndseed']) + dstfile = "".join([r.choice("qQoOp") for i in range(48)]) + "I.txt" + dstfile = dstfile.replace('O', 'I') + for i in range(1000): + start = "".join([r.choice(["po", "p0", "no", "ko", "fo", "qo"]) for i in range(20)]) + mid = "".join([r.choice("uiasdfghjkyxcvbnm1234567890ASDFGHJKYZXCVBNM") for i in range(60)]) + end = r.choice(["lz", "1z", "Iz", "iz", "l2", "I2", "12"]) + #if start[:2] == "po" and end == "lz": + # start = "po" + # mid = "kaka" + 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 ~/*I*.txt\r\n".format(dstfile) + expected_contents + if results["dst_file_contents"] == expected_contents: + score += 3 + else: + diff_pos = "" + for i, c in enumerate(results["dst_file_contents"]): + if len(expected_contents) < i or c != expected_contents[i]: + start = max(0, i-10) + end = min(len(expected_contents), len(results["dst_file_contents"]), i+20) + diff_pos = (i, results["dst_file_contents"][start:end]) + break + hints += ["wrong file contents\n" + str(diff_pos[1])] + #hints += ["wrong file contents"] + expected_ls = u"ls -l ~/*I*.txt\r\n-rw--w---- 1 {testuser} bilbo .*{dstfile}\r\n".format(**params) + if re.match(expected_ls, results["dst_ls"]): + score += 3 + else: + hints += [u"missing file or wrong user/permissions\n" + results["dst_ls"]] + if results["home_ls"].find(params['progname']) > -1: + score += 2 + else: + hints += ["missing program"] + expected_tita = u"" + if results["tita_return"] == expected_tita: + score += 2 + else: + hints += [u"program output incorrect\n" + results["tita_return"]] + return score, hints + + +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']) + dstfile = "".join([r.choice("qQoOp") for i in range(48)]) + "I.txt" + for i in range(1000): + x = "".join([r.choice(["po", "p0", "no", "ko", "fo", "qo"]) for i in range(20)]) + 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.mkdir('.hideme') + for potential_dir in dir_list: + try: + potential_dir = os.path.join('.hideme', potential_dir) + d.mkdir(potential_dir) + d.chown(1001, 1001, potential_dir) + except: + pass + rndstr2 = dstfile + for i in range(r.randint(2, 20)): + hidden_file_name = os.path.join(potential_dir, + rndstr2) + d.write(hidden_file_name, hidden_contents) + d.chown(1001, 1001, hidden_file_name) + file_letters = ["stamp", "", "dev", "re"] + hidden_contents = "".join([r.choice("asdfghjkyxcvbnm1234567890 \n") for j in range(10000)]) + rndstr2 = r.choice(start_list) + \ + r.choice(file_letters) + r.choice(ending_list) + file_letters = file_letters + ["mod", "co"] + # TODO create some additional files + + # write_default_config(templates['simpleArbiterDhcpGW'], global_params) + # finish here + # rename + |