<commit_before>#!/usr/bin/env python3
## -*- coding: utf-8 -*-
##
##  <NAME> - 2021-07-27
##
##  Description: In this challenge, every character of the user input is computed into a
##  signal handler. If a character N is good, the current handler calls the the handler
##  in charge of verifying the character N + 1. Handlers have no dependence between each
##  oter. At the end of each handler execution we can ask for a model to solve the current
##  character. Each character of the user input is computed in order to obtain an hash.
##  In every handler this hash must be equal to 0x3E8.
##
##  Output:
##  $ time python solve.py
##  [+] Loading 0x400040 - 0x400238
##  [+] Loading 0x400238 - 0x400254
##  [+] Loading 0x400000 - 0x40324c
##  [+] Loading 0x603e10 - 0x604088
##  [+] Loading 0x603e28 - 0x603ff8
##  [+] Loading 0x400254 - 0x400298
##  [+] Loading 0x402d8c - 0x402e78
##  [+] Loading 0x000000 - 0x000000
##  [+] Loading 0x603e10 - 0x604000
##  Starting the emulation of all signal handler
##  [+] Symbolic variable 00 = 44 (D)
##  [+] Symbolic variable 01 = 69 (i)
##  [+] Symbolic variable 02 = 64 (d)
##  [+] Symbolic variable 03 = 5f (_)
##  [+] Symbolic variable 04 = 79 (y)
##  [+] Symbolic variable 05 = 6f (o)
##  [+] Symbolic variable 06 = 75 (u)
##  [+] Symbolic variable 07 = 5f (_)
##  [+] Symbolic variable 08 = 6c (l)
##  [+] Symbolic variable 09 = 69 (i)
##  [+] Symbolic variable 10 = 6b (k)
##  [+] Symbolic variable 11 = 65 (e)
##  [+] Symbolic variable 12 = 5f (_)
##  [+] Symbolic variable 13 = 73 (s)
##  [+] Symbolic variable 14 = 69 (i)
##  [+] Symbolic variable 15 = 67 (g)
##  [+] Symbolic variable 16 = 6e (n)
##  [+] Symbolic variable 17 = 61 (a)
##  [+] Symbolic variable 18 = 6c (l)
##  [+] Symbolic variable 19 = 73 (s)
##  [+] Symbolic variable 20 = 3f (?)
##  python solve.py  2.28s user 0.01s system 99% cpu 2.294 total
##

import os
import sys

from triton import *


def emulate(ctx, handler):
    # Define a fake stack for each handler
    ctx.setConcreteRegisterValue(ctx.registers.rbp, 0x7fffffff)
    ctx.setConcreteRegisterValue(ctx.registers.rsp, 0x6fffffff)

    pc = handler
    while pc:
        # Fetch opcode
        opcode = ctx.getConcreteMemoryAreaValue(pc, 16)

        # Create the ctx instruction
        instruction = Instruction(pc, opcode)

        # Process
        ctx.processing(instruction)

        # Next
        pc = ctx.getConcreteRegisterValue(ctx.registers.rip)
        if pc == 0x400CDA:
            # In the handler 0x400C95, we can skip the basic bloc 0x400CE5
            break

    # Each character of the user input is computed in order to obtain an hash.
    # In every handler this hash must be equal to 0x3E8. EAX contains the result
    # of the hash computation.
    success = ctx.getRegisterAst(ctx.registers.eax)
    model   = ctx.getModel(success == 0x3E8)
    for k, v in list(sorted(model.items())):
        value = v.getValue()
        ctx.setConcreteVariableValue(ctx.getSymbolicVariable(k), value)
        print('[+] Symbolic variable %02d = %02x (%c)' %(k, value, chr(value)))
<commit_msg>Fix #872 and #1135: Fix missing unittest<commit_after>#!/usr/bin/env python3
## -*- coding: utf-8 -*-
##
##  <NAME> - 2021-07-27
##
##  Description: In this challenge, every character of the user input is computed into a
##  signal handler. If a character N is good, the current handler calls the the handler
##  in charge of verifying the character N + 1. Handlers have no dependence between each
##  oter. At the end of each handler execution we can ask for a model to solve the current
##  character. Each character of the user input is computed in order to obtain an hash.
##  In every handler this hash must be equal to 0x3E8.
##
##  Output:
##  $ time python solve.py
##  [+] Loading 0x400040 - 0x400238
##  [+] Loading 0x400238 - 0x400254
##  [+] Loading 0x400000 - 0x40324c
##  [+] Loading 0x603e10 - 0x604088
##  [+] Loading 0x603e28 - 0x603ff8
##  [+] Loading 0x400254 - 0x400298
##  [+] Loading 0x402d8c - 0x402e78
##  [+] Loading 0x000000 - 0x000000
##  [+] Loading 0x603e10 - 0x604000
##  Starting the emulation of all signal handler
##  [+] Symbolic variable 00 = 44 (D)
##  [+] Symbolic variable 01 = 69 (i)
##  [+] Symbolic variable 02 = 64 (d)
##  [+] Symbolic variable 03 = 5f (_)
##  [+] Symbolic variable 04 = 79 (y)
##  [+] Symbolic variable 05 = 6f (o)
##  [+] Symbolic variable 06 = 75 (u)
##  [+] Symbolic variable 07 = 5f (_)
##  [+] Symbolic variable 08 = 6c (l)
##  [+] Symbolic variable 09 = 69 (i)
##  [+] Symbolic variable 10 = 6b (k)
##  [+] Symbolic variable 11 = 65 (e)
##  [+] Symbolic variable 12 = 5f (_)
##  [+] Symbolic variable 13 = 73 (s)
##  [+] Symbolic variable 14 = 69 (i)
##  [+] Symbolic variable 15 = 67 (g)
##  [+] Symbolic variable 16 = 6e (n)
##  [+] Symbolic variable 17 = 61 (a)
##  [+] Symbolic variable 18 = 6c (l)
##  [+] Symbolic variable 19 = 73 (s)
##  [+] Symbolic variable 20 = 3f (?)
##  python solve.py  2.28s user 0.01s system 99% cpu 2.294 total
##

import os
import sys

from triton import *


def emulate(ctx, handler):
    # Define a fake stack for each handler
    ctx.setConcreteRegisterValue(ctx.registers.rbp, 0x7fffffff)
    ctx.setConcreteRegisterValue(ctx.registers.rsp, 0x6fffffff)

    pc = handler
    while pc:
        # Fetch opcode
        opcode = ctx.getConcreteMemoryAreaValue(pc, 16)

        # Create the ctx instruction
        instruction = Instruction(pc, opcode)

        # Process
        if ctx.processing(instruction) != EXCEPTION.NO_FAULT:
            return

        # Next
        pc = ctx.getConcreteRegisterValue(ctx.registers.rip)
        if pc == 0x400CDA:
            # In the handler 0x400C95, we can skip the basic bloc 0x400CE5
            break

    # Each character of the user input is computed in order to obtain an hash.
    # In every handler this hash must be equal to 0x3E8. EAX contains the result
    # of the hash computation.
    success = ctx.getRegisterAst(ctx.registers.eax)
    model   = ctx.getModel(success == 0x3E8)
    for k, v in list(sorted(model.items())):
        value = v.getValue()
        ctx.setConcreteVariableValue(ctx.getSymbolicVariable(k), value)
        print('[+] Symbolic variable %02d = %02x (%c)' %(k, value, chr(value)))

{'fraction_non_alphanumeric': 0.12867647058823528, 'fraction_numerical': 0.08072250639386189, 'mean_word_length': 3.062987012987013, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6820649', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2178, 'n_words': 715}