summaryrefslogtreecommitdiff
path: root/kpov_judge
diff options
context:
space:
mode:
Diffstat (limited to 'kpov_judge')
-rwxr-xr-xkpov_judge/kpov_random_helpers.py2
-rw-r--r--kpov_judge/tasks/mock_entrance_exam/task.py154
2 files changed, 121 insertions, 35 deletions
diff --git a/kpov_judge/kpov_random_helpers.py b/kpov_judge/kpov_random_helpers.py
index 0a25d00..fc422fa 100755
--- a/kpov_judge/kpov_random_helpers.py
+++ b/kpov_judge/kpov_random_helpers.py
@@ -119,7 +119,7 @@ default_generators = {
'username': username_gen,
'password': lambda r: alnum_gen(r, 8),
'short_text': lambda r: fortune(r, 40),
- 'uint': lambda r: r.nextint(0, 2**32),
+ 'uint': lambda r: r.randint(0, 2**32),
'hostname': lambda r: hostname_gen(r),
None: lambda r: alnum_gen(r, 8),
'': unknown_generator,
diff --git a/kpov_judge/tasks/mock_entrance_exam/task.py b/kpov_judge/tasks/mock_entrance_exam/task.py
index 341ae07..7080f4c 100644
--- a/kpov_judge/tasks/mock_entrance_exam/task.py
+++ b/kpov_judge/tasks/mock_entrance_exam/task.py
@@ -121,6 +121,12 @@ def task(student_IP, net_prog_name,
arg_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(13)])
stdin_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(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()
@@ -130,10 +136,10 @@ def task(student_IP, net_prog_name,
conn.sendline('cat {}', out_stdout_c)
conn.prompt()
results['script_stdout'] = conn.before()
- conn.sendline('echo "{}" | {} "{}" 2> /dev/null'.format(stdin_val, P_c, arg_val))
+ 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_c, arg_val))
+ conn.sendline('echo "{}" | {} "{}" > /dev/null'.format(stdin_val, P_executable, arg_val))
conn.prompt()
results['prog_stderr'] = conn.before()
target.logout()
@@ -144,58 +150,138 @@ def gen_params(user_id, params_meta):
r = random.Random(user_id)
params = kpov_random_helpers.default_gen(user_id, params_meta)
homedir = '/home/student'
-
params['env_c'] = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ') for i in xrange(5)])
params['P_c'] = "".join([r.choice('abcdefghijklmnoprst') for i in xrange(5)]) + ".c"
params['param_gen_seed'] = r.randint(0, 2**24)
params['c_destroy_gen_seed'] = r.randint(0, 2**24)
- for k in []:
- params[k] = homedir + params[k]
+ for k in ['P_c', 'P_executable', 'out_stderr_c', 'P_script', 'out_stdout_c']:
+ params[k] = homedir + params[k]
return params
def task_check(results, params):
import os
+ def test_out_gen(arg, var):
+ s_out = ""
+ s_err = ""
+ r = 0
+ arg_len = len(arg)
+ env_len = len(var)
+ for i in xrange(100):
+ s_out += chr(20 + (ord(arg[i % arg_len]) ^ ord(var[i % env_len]) % 64))
+ r += ord(arg[i % arg_len]) + ord(var[i % env_len]) + i;
+ if (i % 20 == 0):
+ s_out += "RAUS\n";
+ if (i % 31 == 0):
+ s_out += 'ma'
+ s_err += chr(r % 32 + ord('A'));
+ if (i % 23 == 0):
+ s_err += "PATACIS\n"
+ retval = r % 16
+ return(s_out, s_err, retval)
+
score = 0
hints = []
- r = random.Random(param_gen_seed)
+ r = random.Random(params['param_gen_seed'])
env_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(11)])
-
arg_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(13)])
stdin_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(17)])
-
- matched = 0
- files = os.listdir(params['folder'])
- for fn in files:
- if fn in results['files']:
- matched += 1
- if matched > 0:
- score = 1
+ expected_script_stdout, expected_script_stderr, rval = test_out_gen(
+ params['arg_c'], env_val
+ )
+ split_stderr = expected_script_stderr.split('\n')
+ expected_script_stderr = "\n".join([ i for i in split_stderr if i.find('ma') >= 0])
+ if expected_script_stderr != results['script_stderr']:
+ hints += ['wrong script stderr']
+ else:
+ score += 2
+ if expected_script_stdout != results['script_stdout']:
+ hints += ['wrong script stdout']
+ else:
+ score += 2
+ expected_prog_stdout, expected_prog_stderr, rval = test_out_ge-n(
+ arg_val, stdin_val
+ )
+ if expected_prog_stderr != results['prog_stderr']:
+ hints += ['wrong program stderr']
else:
- hints += ["no files"]
- if matched > len(files)/2:
score += 2
+ if expected_prog_stdout != results['prog_stdout']:
+ hints += ['wrong program stdout']
else:
- hints += ["less than half the files"]
- if (matched == len(files)):
- score += 3
+ score += 2
+ if results['script_ls'].find('-r') < 0:
+ hints += ['script not found']
else:
- hints += ["wrong number of files"]
- rl = results['contents'].split('\n')
- rl.sort()
- tl = []
- for fn in files:
- with open(fn) as f:
- tl += f.read().upper().split('\n')
- tl.sort()
- if rl == tl:
- score += 4
+ score += 1
+ if results['executable_ls'].find('xr') < 0:
+ hints += ['C executable not found']
else:
- hints += ["wrong files"]
+ score += 1
return hints, score
def prepare_disks(templates, params):
- import random
- c_source = """
+ c_source = """#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+/* Odstranite vse odvecne velike crke Q, W ali X in program se bo prevedel. */
+int main(int argc, char **argv){
+ unsigned char *arg;
+ unsigned char var[255];
+ int i, arg_len, env_len, r;
+ scanf("%s", var);
+ arg = argv[1];
+ arg_len = strlen(argv[1]);
+ env_len = strlen(var);
+ r = 0;
+ for (i = 0; i<100; i++){
+ printf("%c", 20 + (arg[i % arg_len] ^ var[i % env_len] % 64));
+ r += (int)arg[i % arg_len] + (int)var[i % env_len] + i;
+ if (i % 20 == 0){
+ printf("RAUS\n");
+ }
+ if (i % 31 == 0){
+ printf("ma");
+ }
+ fprintf(stderr, "%c", r % 32 + 'A');
+ if (i % 23 == 0){
+ fprintf(stderr, "PATACIS\n");
+ }
+ }
+ printf("\n");
+ fprintf(stderr, "\n");
+ return r % 16;
+}
"""
- pass
+ evil_shell_source = """#!/bin/sh
+while true; do
+ ifconfig eth1 10.0.2.19;
+ ifconfig eth0 10.0.2.20;
+ ifconfig eth2 10.0.2.21;
+ sleep 10;
+done
+"""
+ import random
+ d = templates['Student']
+ r = random.Random(params['c_destroy_gen_seed'])
+ destroyed_c_source = c_source[:100]
+ for c in c_source[100:]:
+ i = r.randint(0, 5)
+ if i == 1:
+ destroyed_c_source += 'QW'
+ if i == 2:
+ destroyed_c_source += 'XW'
+ if i == 3:
+ destroyed_c_source += 'QX'
+ destroyed_c_source += c
+ d.write(params['P_c'], destroyed_c_source)
+ d.chown(1000, 1000, params['P_c'])
+ sh_path ='/usr/local/bin' + params['net_prog_name']
+ d.write(sh_path, evil_shell_source)
+ d.chmod(0775, sh_path)
+ d.write("/etc/rc.local", """#!/bin/sh -e
+
+{}
+
+exit 0
+""".format(sh_path))