[護(hù)網(wǎng)杯 2018]easy_tornado
給了三個(gè)txt,flag.txt給出了flag文件乾忱,welcome.txt提示了render讥珍,hints.txt給出了摘要算法md5(cookie_secret+md5(filename))。并且觀察url可以發(fā)現(xiàn)提交了兩個(gè)參數(shù)分別為一個(gè)filename和一個(gè)filehash窄瘟。既然這里給出了摘要算法衷佃,應(yīng)該就是提交的filehash值算法。
這里要計(jì)算filehash值就要獲取cookie_secret蹄葱,根據(jù)welcome提示氏义,應(yīng)該是render模板注入(ssti)锄列,通過(guò)ssti獲取cookie_secret再計(jì)算出filehash讀取flag文件獲取flag。注入點(diǎn)在一個(gè)錯(cuò)誤提示中惯悠,當(dāng)提交的filehash不正確時(shí)就會(huì)跳轉(zhuǎn)到一個(gè)錯(cuò)誤消息的url邻邮,其中有一個(gè)參數(shù)為msg,修改這個(gè)參數(shù)發(fā)現(xiàn)提交的值顯示在頁(yè)面中克婶。提交msg={{1}}回顯為1筒严,嘗試提交{{1+1}}發(fā)現(xiàn)被過(guò)濾了,但是{{2^2}}回顯為0情萤,說(shuō)明確實(shí)存在ssti萝风。但是如何獲取cookie_secret還是不知道,看了一下wp發(fā)現(xiàn)時(shí)利用handler.settings進(jìn)行讀取紫岩,但是都沒(méi)有說(shuō)明為什么這樣讀取。我們利用這個(gè)變量可以獲取到cookie_secret然后使用hints.txt中給出的算法即可計(jì)算出filehash讀取flag文件睬塌。
Tornado templates support control statements and expressions. Control statements are surrounded by
{%
and%}
, e.g.{% if len(items) > 2 %}
. Expressions are surrounded by{{
and}}
, e.g.{{ items[0] }}
.
Python中從服務(wù)端模板注入到沙盒逃逸的源碼探索 (一)一文對(duì)為什么利用handler.settings獲取cookie_secret做出了部分解釋?zhuān)撐姆懦隽瞬糠謙ornado源碼泉蝌,我們可以在一下代碼中看到有一個(gè)將self賦值給handler的操作
def get_template_namespace(self):
namespace = dict(
handler=self,
request=self.request,
current_user=self.current_user,
locale=self.locale,
_=self.locale.translate,
pgettext=self.locale.pgettext,
static_url=self.static_url,
xsrf_form_html=self.xsrf_form_html,
reverse_url=self.reverse_url
)
namespace.update(self.ui)
return namespace
這是RequestHandler類(lèi)中的一個(gè)方法,也就是說(shuō)這里將RequestHandler類(lèi)本身賦值給了handler揩晴,因此handler.settings實(shí)際上就是RequestHandler.settings(self.settings)勋陪,這一點(diǎn)在tornado文檔中也有說(shuō)明。
- Expressions can be any Python expression, including function calls. Template code is executed in a namespace that includes the following objects and functions. (Note that this list applies to templates rendered using
RequestHandler.render
andrender_string
. If you’re using thetornado.template
module directly outside of aRequestHandler
many of these entries are not present).escape
: alias fortornado.escape.xhtml_escape
xhtml_escape
: alias fortornado.escape.xhtml_escape
url_escape
: alias fortornado.escape.url_escape
json_encode
: alias fortornado.escape.json_encode
squeeze
: alias fortornado.escape.squeeze
linkify
: alias fortornado.escape.linkify
datetime
: the Pythondatetime
modulehandler
: the currentRequestHandler
objectrequest
: alias forhandler.request
current_user
: alias forhandler.current_user
locale
: alias forhandler.locale
_
: alias forhandler.locale.translate
static_url
: alias forhandler.static_url
xsrf_form_html
: alias forhandler.xsrf_form_html
reverse_url
: alias forApplication.reverse_url
- All entries from the
ui_methods
andui_modules
Application
settings- Any keyword arguments passed to
render
orrender_string
tornado提供了一些變量可以在模板中訪問(wèn)硫兰,因此我們可以利用handler來(lái)訪問(wèn)settings诅愚。
Tornado小記 -- 模板中的Handler一文對(duì)RequestHandler.settings做出了解釋?zhuān)琑equestHandler.settings實(shí)際上是指向了self.application.settings,在tornado文檔中也能看到這個(gè)說(shuō)明劫映。
RequestHandler.settings
An alias for
self.application.settings
.
在[tornado文檔](https://www.tornadoweb.org/en/stable/guide/security.html#User authentication)此處能夠看到
class MainHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
name = tornado.escape.xhtml_escape(self.current_user)
self.write("Hello, " + name)
settings = {
"cookie_secret": "__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
"login_url": "/login",
}
application = tornado.web.Application([
(r"/", MainHandler),
(r"/login", LoginHandler),
], **settings)
在這里我們可以看到在cookie_secret是存在settings中的违孝,settings又作為參數(shù)傳入了Application構(gòu)造函數(shù),因此可以通過(guò)self.application.settings獲取到cookie_secret泳赋。
既然這里最終是訪問(wèn)到self.application.settings獲取cookie_secret雌桑,并且需要利用tornado提供的handler來(lái)訪問(wèn),那么我們不僅可以通過(guò)handler.settings來(lái)獲取祖今,也可以使用handler.application.settings校坑,這樣實(shí)際上也是訪問(wèn)到self.application.settings。