Esempio n. 1
0
def create(source):
    common.check_env()
    common.check_env(_M(source), 'file with questions was not found')

    question_block = []
    questions_block = [question_block]
    with open(_M(source), 'r') as source_file:
        for line in source_file:
            line = line.strip()
            if len(line) == 0 and len(question_block) > 0:
                question_block = []
                questions_block.append(question_block)
            if len(line) > 0:
                question_block.append(line)

    students = common.load_students()
    target_file = open(_M('output'), 'w')  # TODO Unhardcode

    for i, student in enumerate(students, 1):
        print('Gen {0}: {1}'.format(i, student))

        print('{0}. {1}'.format(i, student.name), file=target_file)
        print('==================================================',
              file=target_file)
        for j, block in enumerate(questions_block, 1):
            print('{0}. {1}'.format(j, random.choice(block)), file=target_file)
        print(file=target_file)
Esempio n. 2
0
def gen():
    """Generate students tasks"""

    common.check_env()
    common.create_module(__name__)

    # TODO Add certificate chain generation here

    students = common.load_students()
    if os.path.exists(_M(VARIANTS_DB)):
        passwords = [x for x in common.load_csv(_M(VARIANTS_DB))]
    else:
        passwords = [[x.name, ''] for x in students]

    cache = []
    for i, student in enumerate(students):
        print('Gen {0}: {1} '.format(i + 1, student), end='')

        if len(student.keybase) == 0:
            print('has no keybase account')
            continue

        if len(passwords[i][1]) == 0:
            passwords[i][1] = common.gen_pass()
            print('generated')
        else:
            print('already generated')

        rsa_pub = common.extract_module(_get_rsa(student.fingerprint))
        cache.append([rsa_pub, passwords[i][1]])

    common.save_csv(_M(CACHE_DB), cache)
    common.save_csv(_M(VARIANTS_DB), passwords)

    print('Created students tasks')
Esempio n. 3
0
def find(mask):
    """Find student by name|email|keybase login"""

    for i, student in enumerate(common.load_students(), 1):
        if not common.fuzzy_check(i, student, mask):
            continue
        common.print_student_full(i, student)
Esempio n. 4
0
def importpgp():
    """Import students PGP keys from keybase.io"""

    for student in common.load_students():
        if not student.keybase:
            continue

        print('Importing', student.name)
        key_url = 'https://keybase.io/{0}/key.asc'.format(student.keybase)
        key_data = urllib.request.urlopen(key_url).read()
        subprocess.check_output(['gpg', '--import', '-'], input=key_data)
Esempio n. 5
0
def gen():
    """Генерирует набор кошельков студентов"""

    common.check_env()
    common.create_module(__name__)
    native = _gen_native()

    students = common.load_students()
    wallets = collections.OrderedDict()
    block = ctypes.create_string_buffer(BLOCK_SIZE)
    for i, student in enumerate(students):
        print('Gen {0}: {1} ['.format(i + 1, student), end='')

        key_name = _M('student{}.key'.format(i + 1))
        if not os.path.exists(key_name):
            print('k', end='')
            subprocess.check_call([
                'openssl', 'ecparam', '-genkey', '-name', 'secp256k1',
                '-noout', '-out', key_name
            ],
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)

        pub_name = _M('student{}.pub'.format(i + 1))
        if not os.path.exists(pub_name):
            print('p', end='')
            subprocess.check_call([
                'openssl', 'ec', '-pubout', '-in', key_name, '-out', pub_name
            ],
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)

        enc_name = _M('student{}.enc'.format(i + 1))
        if not os.path.exists(enc_name) and len(student.keybase) != 0:
            print('e', end='')
            subprocess.check_call([
                'gpg', '--trust-model', 'always', '--yes', '-r',
                student.fingerprint, '--output', enc_name, '--encrypt',
                key_name
            ],
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)

        native.write_target(block, pub_name.encode('utf8'))
        wallets[student.name] = ''.join('{:02x}'.format(x)
                                        for x in block[96:160])

        print('] generated')

    with open(WALLETS_FILE, 'w', encoding='utf8') as wallets_file:
        json.dump(wallets, wallets_file, ensure_ascii=False)

    print('Created students tasks')
Esempio n. 6
0
def gen():
    """Generate students tasks"""

    common.check_env()
    common.create_module(__name__)

    students = common.load_students()
    if os.path.exists(_M(VARIANTS_DB)):
        passwords = [x for x in common.load_csv(_M(VARIANTS_DB))]
    else:
        passwords = [[x.name, ''] for x in students]

    for i, student in enumerate(students):
        print('Gen {0}: {1} '.format(i + 1, student), end='')

        if len(passwords[i][1]) > 0:
            print('skipped, already generated')
            continue

        passwords[i][1] = common.gen_pass()
        correct = random.choice(MASKS)
        for j, mask in enumerate(MASKS):
            filename = _M('file{0}_{1}.bin'.format(i + 1, j))

            code_file = open('temp', 'w')
            print(mask.format(passwords[i][1]), file=code_file)
            code_file.close()
            subprocess.check_call([
                'gpg', '--batch', '--yes',
                '--output', filename, '--clear-sign', 'temp'])
            os.unlink('temp')

            if mask == correct:
                continue

            code_file = open(filename, 'r')
            content = code_file.read()
            code_file.close()

            content = content.replace(passwords[i][1], common.gen_pass())

            code_file = open(filename, 'w')
            code_file.write(content)
            code_file.close()
        print('generated')

    common.save_csv(_M(VARIANTS_DB), passwords)

    print('Created students tasks')
Esempio n. 7
0
def gen():
    """Generate students tasks"""

    common.check_env()
    common.create_module(__name__)

    students = common.load_students()
    if os.path.exists(_M(VARIANTS_DB)):
        passwords = [x for x in common.load_csv(_M(VARIANTS_DB))]
    else:
        passwords = [[x.name, '', '', '', ''] for x in students]

    for i, student in enumerate(students):
        print('Gen {0}: {1} '.format(i + 1, student), end='')

        if len(passwords[i][1]) > 0:
            print('skipped, already generated')
            continue

        print('generating...')
        variant = Variant()

        target, native = _gen_native(variant,
                                     _M('ref_{}.so'.format(i + 1)),
                                     debug=True)
        code = native.check(0, 0)
        reference = code >> 32
        code = code & 0xFFFFFFFF

        passwords[i][1] = '{:08x}'.format(code)
        passwords[i][2] = '{}'.format(variant.mask)
        passwords[i][3] = '{}'.format(variant.nonce)
        passwords[i][4] = '{:08x}'.format(reference)

        target_dir = _M('stud_{}'.format(i + 1))
        target_so = os.path.join(target_dir, 'native.so')
        target_cli = os.path.join(target_dir, 'cli.py')

        os.makedirs(target_dir, exist_ok=True)
        target, native = _gen_native(variant, target_so, reference=reference)
        shutil.copyfile(_R('cli.py'), target_cli)

        print('generated')

    shutil.rmtree(_M('tmp'))
    common.save_csv(_M(VARIANTS_DB), passwords)

    print('Created students tasks')
Esempio n. 8
0
def gen():
    """Generate students tasks"""

    common.check_env()
    common.create_module(__name__)

    students = common.load_students()
    if os.path.exists(_M(VARIANTS_DB)):
        passwords = [x for x in common.load_csv(_M(VARIANTS_DB))]
    else:
        passwords = [[x.name, ''] for x in students]

    for i, student in enumerate(students):
        print('Gen {0}: {1} '.format(i + 1, student), end='')

        if len(student.keybase) == 0:
            print('skipped, keybase account is not configured')
            continue

        if len(passwords[i][1]) > 0:
            print('skipped, already generated')
            continue

        print('generating...')
        passwords[i][1] = common.gen_pass()
        source_filename = 'file{0}.bin'.format(i + 1)
        target_filename = source_filename + '.gpg'
        code_file = open(_M(source_filename), 'w')
        print(MASK.format(passwords[i][1]), file=code_file)
        code_file.close()

        subprocess.check_call(['gpg', '--trust-model', 'always', '--yes',
                               '-r', student.fingerprint,
                               '-z', '0', '-e', _M(source_filename)])
        os.unlink(_M(source_filename))
        _broke(_M(target_filename))

        print('checking...')
        container = OpenPgpContainer(_M(target_filename))
        print('generated')

    common.save_csv(_M(VARIANTS_DB), passwords)

    print('Created students tasks')
Esempio n. 9
0
def list():
    """Show students list"""

    for i, student in enumerate(common.load_students(), 1):
        common.print_student_full(i, student)