CTF/Misc

PatriotCTF 2024 - Emoji Stack

xoheon 2024. 9. 23. 12:04

๐Ÿ’ก ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค


ํ’€์ด

 

def emoji_stack_interpreter(program):
    # Initialize the stack (256 cells) and the pointer
    stack = [0] * 256
    pointer = 0
    output = []
    
    # Create a pointer for reading the program
    i = 0
    
    # Helper function to get the current value at the pointer
    def get_value():
        return stack[pointer]

    # Helper function to update the stack with modular arithmetic
    def set_value(value):
        stack[pointer] = value % 256

    while i < len(program):
        cmd = program[i]
        
        if cmd == ':point_right:':
            pointer = (pointer + 1) % 256  # Move right, wrap around if needed
        elif cmd == ':point_left:':
            pointer = (pointer - 1) % 256  # Move left, wrap around if needed
        elif cmd == ':thumbsup:':
            set_value(get_value() + 1)  # Increment the current cell
        elif cmd == ':thumbsdown:':
            set_value(get_value() - 1)  # Decrement the current cell
        elif cmd == ':speech_balloon:':
            output.append(chr(get_value()))  # Print the ASCII value of the current cell
        elif cmd == ':repeat:':
            # Repeat the previous instruction (next two characters represent the hex value)
            repeat_count = int(program[i+1:i+3], 16)
            previous_cmd = program[i-1]
            for _ in range(repeat_count):
                if previous_cmd == ':thumbsup:':
                    set_value(get_value() + 1)
                elif previous_cmd == ':thumbsdown:':
                    set_value(get_value() - 1)
                elif previous_cmd == ':point_right:':
                    pointer = (pointer + 1) % 256
                elif previous_cmd == ':point_left:':
                    pointer = (pointer - 1) % 256
            i += 2  # Skip the next two hex characters
        i += 1

    return ''.join(output)


# Read the content of the input file
file_path = 'input.txt'
with open(file_path, 'r') as file:
    program = file.read()

# Run the interpreter
flag = emoji_stack_interpreter(program)
print(flag)

 

1. . emoji_stack_interpreter ํ•จ์ˆ˜ ์ •์˜

 

์Šคํƒ ์ดˆ๊ธฐํ™”:

stack = [0] * 256: 256๊ฐœ์˜ ์…€๋กœ ๊ตฌ์„ฑ๋œ ์Šคํƒ์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

 

pointer = 0: ์Šคํƒ ํฌ์ธํ„ฐ๋Š” ์ฒ˜์Œ์— ์ฒซ ๋ฒˆ์งธ ์…€์„ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.

 

output = []: ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค.

 

get_value ํ•จ์ˆ˜: ํ˜„์žฌ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์…€์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

set_value ํ•จ์ˆ˜: ํ˜„์žฌ ์…€์— ๊ฐ’์„ ๋„ฃ์„ ๋•Œ 256์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์…€ ๊ฐ’์ด 0~255 ์‚ฌ์ด์— ์žˆ๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

 

2. while ๋ฃจํ”„:

 

ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐ ๋ช…๋ น์„ ํ•œ ๊ธ€์ž์”ฉ ์ฝ์œผ๋ฉด์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

ํฌ์ธํ„ฐ ์ด๋™ (๐Ÿ‘‰, ๐Ÿ‘ˆ):

๐Ÿ‘‰: ์Šคํƒ ํฌ์ธํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. pointer = (pointer + 1) % 256๋กœ, 256์„ ๋„˜์–ด๊ฐ€๋ฉด ๋‹ค์‹œ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„์˜ต๋‹ˆ๋‹ค.

 

๐Ÿ‘ˆ: ์Šคํƒ ํฌ์ธํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. pointer = (pointer - 1) % 256๋กœ, 0์—์„œ ์™ผ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด ๋งˆ์ง€๋ง‰ ์…€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

๊ฐ’ ์ฆ๊ฐ€ ๋ฐ ๊ฐ์†Œ (๐Ÿ‘, ๐Ÿ‘Ž):

๐Ÿ‘: ํ˜„์žฌ ์…€ ๊ฐ’์„ 1 ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.

๐Ÿ‘Ž: ํ˜„์žฌ ์…€ ๊ฐ’์„ 1 ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ ๋ช…๋ น (๐Ÿ’ฌ):

ํ˜„์žฌ ์…€์˜ ๊ฐ’์„ ASCII ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด output ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. chr(get_value())๋ฅผ ํ†ตํ•ด ASCII ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ˜๋ณต ์ฒ˜๋ฆฌ (๐Ÿ”):

๐Ÿ” ๋‹ค์Œ์— ์˜ค๋Š” ๋‘ ์ž๋ฆฌ ์ˆซ์ž๋Š” 16์ง„์ˆ˜๋กœ ๋ช‡ ๋ฒˆ ๋ฐ˜๋ณตํ• ์ง€ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๐Ÿ”47์€ ์ง์ „ ๋ช…๋ น์„ 0x47(์ฆ‰, 71๋ฒˆ) ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ๋•Œ ์ง์ „ ๋ช…๋ น(๐Ÿ‘, ๐Ÿ‘Ž, ๐Ÿ‘‰, ๐Ÿ‘ˆ)์„ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

3. ํŒŒ์ผ ์ฝ๊ธฐ:

 

input.txt ํŒŒ์ผ์„ ์ฝ์–ด Emoji Stack ํ”„๋กœ๊ทธ๋žจ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

 

4. ํ•ด์„๊ธฐ ์‹คํ–‰:

 

emoji_stack_interpreter(program) ํ•จ์ˆ˜์— ํŒŒ์ผ์—์„œ ์ฝ์€ ํ”„๋กœ๊ทธ๋žจ์„ ์ „๋‹ฌํ•˜์—ฌ ์‹คํ–‰ํ•œ ํ›„, ์ถœ๋ ฅ๊ฐ’(ํ”Œ๋ž˜๊ทธ)์„ ๋ฐ˜ํ™˜

'CTF > Misc' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

2024 snakectf - Unlucky Cactus  (2) 2024.09.08
CCE 2024 Qulas - HWKborad  (0) 2024.08.04