예제 #1
0
def compileProblem(problem, solved):
    db = DB(config.DATABASE)
    db.execute('UPDATE solved SET status=? where id=?',
               ('COMPILING', solved['id']))
    db.commit()

    snippet_dir = os.path.join(
        'data/snippets',
        binascii.hexlify(bytes(solved['owner'], 'utf-8')).decode('utf-8'))

    code = solved['answer']

    suffix = problem['suffix']
    suffix = bytes(suffix.encode('utf-8'))

    answerFile = save_snippet(solved['owner'], '_' + str(problem['id']) + '.s',
                              code)

    suffixFile = open(
        os.path.join('data/suffixes', '_' + str(solved['problem']) + '.s'),
        'wb')
    suffixFile.write(suffix)

    suffixFile.close()
    execFileName = answerFile.name[0:-2]

    p = subprocess.Popen(
        (config.CC_PATH, answerFile.name, suffixFile.name, '-o', execFileName,
         '-I', config.INCLUDE_PATH, '-I', snippet_dir, '-m32', '-nostdlib',
         '-fno-stack-protector'),
        stderr=subprocess.PIPE)

    code = p.wait()
    err = p.stderr.read()

    if code != 0:
        print(err)
        db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
                   ('FAIL', err, solved['id']))
        db.commit()

    os.unlink(answerFile.name)

    if code == 0:
        runBinary(problem, solved, execFileName)
예제 #2
0
def compileProblem(problem, solved):
    db = DB(config.DATABASE)
    db.execute('UPDATE solved SET status=? where id=?',
               ('COMPILING', solved['id']))
    db.commit()

    snippet_dir = os.path.join(
        'data/snippets',
        binascii.hexlify(bytes(solved['owner'], 'utf-8')).decode('utf-8'))

    code = solved['answer'] + \
        u"\n.globl __NoTraceHere__\nnop\n__NoTraceHere__:" + \
        problem['suffix']

    answerFile = NamedTemporaryFile(mode='w',
                                    prefix='asm_tmp_',
                                    suffix='.s',
                                    delete=False)
    answerFile.write(code)

    answerFile.close()
    execFileName = answerFile.name[0:-2]

    p = subprocess.Popen((config.CC_PATH, answerFile.name, '-o', execFileName,
                          '-I', config.INCLUDE_PATH, '-I', snippet_dir, '-m32',
                          '-nostdlib', '-fno-stack-protector'),
                         stderr=subprocess.PIPE)

    code = p.wait()
    err = p.stderr.read()

    if code != 0:
        print(err)
        db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
                   ('FAIL', err, solved['id']))
        db.commit()

    os.unlink(answerFile.name)

    if code == 0:
        runBinary(problem, solved, execFileName)
예제 #3
0
def compileProblem(problem, solved):
    db = DB(config.DATABASE)
    db.execute('UPDATE solved SET status=? where id=?', ('COMPILING', solved['id']))
    db.commit()

    snippet_dir = os.path.join( 'data/snippets', binascii.hexlify( bytes(solved['owner'], 'utf-8') ).decode('utf-8') )

    code = solved['answer'] + \
        u"\n.globl __NoTraceHere__\nnop\n__NoTraceHere__:" + \
        problem['suffix']

    answerFile = NamedTemporaryFile(mode= 'w', prefix='asm_tmp_',
                    suffix='.s', delete=False)
    answerFile.write(code)

    answerFile.close()
    execFileName = answerFile.name[0:-2]

    p = subprocess.Popen((config.CC_PATH,
        answerFile.name,
        '-o', execFileName,
        '-I', config.INCLUDE_PATH,
        '-I', snippet_dir,
        '-m32', '-nostdlib', '-fno-stack-protector'), stderr=subprocess.PIPE)

    code = p.wait()
    err = p.stderr.read()

    if code != 0:
        print(err)
        db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
            ('FAIL', err, solved['id']))
        db.commit()


    os.unlink(answerFile.name)

    if code == 0:
        runBinary(problem, solved, execFileName)
예제 #4
0
#!/usr/bin/python3

from asmlearner import config
from asmlearner.library.database.sqlite import DB
from os.path import join
import getpass
from hashlib import sha1


db = DB(config.DATABASE)
db.executescript(join(config.PROJECT_DIR, 'init.sql'))
id_ = input('ID: ')
password_ = getpass.getpass('PW: ')
pw_hash = sha1(password_.encode('utf-8') * 10).hexdigest()

db.execute('INSERT INTO user (id, password, role) VALUES(?, ?, \'admin\')', (id_, pw_hash))
db.commit()
예제 #5
0
#!/usr/bin/python3

from asmlearner import app, config
from asmlearner.library.database.sqlite import DB

from os.path import join

if __name__ == '__main__':
    DB(config.DATABASE).executescript(join(config.PROJECT_DIR, 'init.sql'))

    app.secret_key = config.SECRET_KEY


    app.debug = True
    app.run(host='0.0.0.0', port=3333)
예제 #6
0
def runBinary(problem, solved, execFileName):
    inputFile = NamedTemporaryFile(mode='w',
                                   prefix='asm_input_tmp_',
                                   delete=False)
    inputFile.write(problem['input'])
    inputFile.close()
    inputFilePath = inputFile.name
    print(open(inputFilePath, 'rb').read())
    tracerArgv = (config.TRACER_PATH, execFileName, inputFilePath, '/dev/fd/1')
    p = subprocess.Popen(
        (config.OBJDUMP_PATH, '-M', 'intel', '-d', execFileName),
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    print(tracerArgv)
    code = p.wait()
    dump_out = p.stdout.read()

    db = DB(config.DATABASE)
    if code != 0:
        db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
                   ('FAIL', dump_out, solved['id']))
        db.commit()
    else:
        inputFileRead = open(inputFilePath, 'rb')

        p = subprocess.Popen(tracerArgv,
                             stdout=subprocess.PIPE,
                             stdin=inputFileRead,
                             stderr=subprocess.PIPE)
        err = out = p.stdout.read()
        code = p.wait()

        if code != 0:
            db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
                       ('WRONG', err, solved['id']))
        else:
            out = out + b'\n\n$ objdump -d [binary_file]\n' + dump_out
            print(out)
            m = re.findall(problem['answer_regex'].encode(), out)
            print(len(m))
            db.execute(
                'UPDATE solved SET status=?, errmsg=? where id=?',
                ('CORRECT' if len(m) > 0 else 'WRONG', out, solved['id']))
    db.commit()
    os.unlink(execFileName)
    os.unlink(inputFilePath)
예제 #7
0
def runBinary(problem, solved, execFileName):
    inputFile = NamedTemporaryFile(mode='w', prefix='asm_input_tmp_', delete=False)
    inputFile.write(problem['input'])
    inputFile.close()
    inputFilePath = inputFile.name
    print(open(inputFilePath, 'rb').read())
    tracerArgv = (config.TRACER_PATH, execFileName, inputFilePath, '/dev/fd/1')
    p = subprocess.Popen((config.OBJDUMP_PATH, '-M', 'intel', '-d', execFileName),
            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print(tracerArgv)
    code = p.wait()
    err = p.stdout.read()

    db = DB(config.DATABASE)
    if code != 0:
        db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
        ('FAIL', err, solved['id']))
        db.commit()
    else:
        inputFileRead = open(inputFilePath, 'rb')

        p = subprocess.Popen(tracerArgv, stdout=subprocess.PIPE, stdin=inputFileRead, stderr=subprocess.PIPE)
        err = out = p.stdout.read()
        code = p.wait()

        if code != 0:
            db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
                ('WRONG', err, solved['id']))
        else:
            print(out)
            m = re.findall(problem['answer_regex'].encode(), out)
            print(len(m))
            db.execute('UPDATE solved SET status=?, errmsg=? where id=?',
                ('CORRECT' if len(m) > 0 else 'WRONG', out, solved['id']))
    db.commit()
    os.unlink(execFileName)
    os.unlink(inputFilePath)
예제 #8
0
def before_req():
    g.db = DB(config.DATABASE)
예제 #9
0
 def get_db(self):
     db = DB(config.DATABASE)
     return db
예제 #10
0
#!/usr/bin/python3

from asmlearner import config
from asmlearner.library.database.sqlite import DB
from os.path import join
import getpass
from hashlib import sha1

db = DB(config.DATABASE)
db.executescript(join(config.PROJECT_DIR, 'init.sql'))
id_ = input('ID: ')
password_ = getpass.getpass('PW: ')
pw_hash = sha1(password_.encode('utf-8') * 10).hexdigest()

db.execute('INSERT INTO user (id, password, role) VALUES(?, ?, \'admin\')',
           (id_, pw_hash))
db.commit()