0%

如何用 docker 出一道 ctf 题 (crypto)

如何用 docker 出一道 ctf 题 (crypto)

目前 docker 的使用越来越宽泛,ctfd 也支持从 dockerhub 一键拉题了。因此,学习如何使用 docker 出 ctf 题是非常必要的。

python3 篇

在介绍 python2 篇和 python3 篇之前,首先需要对 raw_input 和 input 这两个函数做一个讲解。引用菜鸟教程

注意:input () 和 raw_input () 这两个函数均能接收 字符串 ,但 raw_input () 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input () ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。

除非对 input () 有特别需要,否则一般情况下我们都是推荐使用 raw_input () 来与用户交互。

注意:python3 里 input () 默认接收到的是 str 类型。

因此,在 python3 环境下出题,输入端要填 input ()。

和 web 篇一样,我们以一道完整的题来演示一下。题目为 sha256 碰撞。

sha256.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import os
import hashlib
import random
import string

BUFSIZE = ''

def c0():
random.seed(os.urandom(128))
alphabet = string.ascii_letters+string.digits
proof = ''.join(random.choices(alphabet, k=16))
hash_value = hashlib.sha256(proof.encode('ascii')).hexdigest()
print('???'+'+'+proof[3:])
print(hash_value)
buf = input(BUFSIZE).replace('\n', '')
tmp = buf+proof[3:]
if hashlib.sha256(tmp.encode('ascii')).hexdigest() == hash_value:
print('proof completed\n')
else:
print('bye~')
return 0
return 1

def win():
print(os.environ.get('FLAG', 'Flag not Found...')+'\n')

if __name__ == "__main__":
try:
c0 = c0()
if c0 == 1:
win()
except:
pass

先是一波经典偷懒,直接拿了 ctftrainingcopperstudy2.py 进行修改,改除了这道题目。由于答案在环境变量里,可以采用 print(os.environ.get('FLAG')) 这种方法,但是我个人更喜欢 from flag import flag 这种方法,区别不大,主要就是知道如何从系统环境变量中拿 flag 就行。

然后看 Dockerfile,更简单。

Dockerfile

1
2
3
4
5
6
7
FROM ctfhub/base_misc_socat_python_36
COPY sha256.py /app/sha256.py

MAINTAINER mapleice <https://liuxin2020.github.io>
EXPOSE 10000

ENTRYPOINT socat TCP4-LISTEN:10000,tcpwrap=script,reuseaddr,fork EXEC:"/usr/bin/env python3 -u /app/sha256.py"

做成镜像传上去就行了。

其他记得注意填写 requirements.txt

python2

有空回来更。注意使用 raw_input

------------- 本文结束感谢您的阅读 -------------