# OSインジェクション・evalインジェクション対策
## 脆弱性についての影響
- 予期しないOSコマンドやPHPが実行されてしまう可能性がある
- `&& rm -rf /`
## JVNでの脆弱性リスト
- https://jvndb.jvn.jp/ja/contents/2022/JVNDB-2022-001804.html
> OpenSSL の c_rehash スクリプトには、コマンドインジェクションを防ぐためのシェルのメタ文字のサニタイズに関する脆弱性が存在します。
<details>
<summary>projects/openssl.git/commitdiff</summary>
```git
--- a/tools/c_rehash.in
+++ b/tools/c_rehash.in
@@ -152,6 +152,23 @@ sub check_file {
return ($is_cert, $is_crl);
}
+sub compute_hash {
+ my $fh;
+ if ( $^O eq "VMS" ) {
+ # VMS uses the open through shell
+ # The file names are safe there and list form is unsupported
+ if (!open($fh, "-|", join(' ', @_))) {
+ print STDERR "Cannot compute hash on '$fname'\n";
+ return;
+ }
+ } else {
+ if (!open($fh, "-|", @_)) {
+ print STDERR "Cannot compute hash on '$fname'\n";
+ return;
+ }
+ }
+ return (<$fh>, <$fh>);
+}
# Link a certificate to its subject name hash value, each hash is of
# the form <hash>.<n> where n is an integer. If the hash value already exists
```
```git
@@ -161,10 +178,12 @@ sub check_file {
sub link_hash_cert {
my $fname = $_[0];
- $fname =~ s/\"/\\\"/g;
- my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
+ my ($hash, $fprint) = compute_hash($openssl, "x509", $x509hash,
+ "-fingerprint", "-noout",
+ "-in", $fname);
chomp $hash;
chomp $fprint;
+ return if !$hash;
$fprint =~ s/^.*=//;
$fprint =~ tr/://d;
my $suffix = 0;
```
```git
@@ -202,10 +221,12 @@ sub link_hash_cert {
sub link_hash_crl {
my $fname = $_[0];
- $fname =~ s/'/'\\''/g;
- my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
+ my ($hash, $fprint) = compute_hash($openssl, "crl", $crlhash,
+ "-fingerprint", "-noout",
+ "-in", $fname);
chomp $hash;
chomp $fprint;
+ return if !$hash;
$fprint =~ s/^.*=//;
$fprint =~ tr/://d;
```
</details>
## 対策
- OSコマンドやeval関数実行までに命令文をエスケープする
- 特殊文字を検出して例外を投げる
## 悪用事例
- https://jvndb.jvn.jp/ja/contents/2021/JVNDB-2021-000093.html
> シックス・アパート株式会社が提供する Movable Type の XMLRPC API には、OS コマンドインジェクション (CWE-78) の脆弱性が存在します。
Movable Type の XMLRPC API に細工したメッセージを POST メソッドで送信することで、任意の OS コマンドが実行可能となります。
> 【2021年11月10日 追記】
2021年11月10日現在、本脆弱性の実証コードの公開と、本脆弱性を悪用した攻撃を確認しています。
> この脆弱性情報は、次の方が製品開発者に直接報告し、製品開発者との調整を経て、製品利用者への周知を目的に JVN での公表に至りました。
報告者: Étienne Gervais 氏、Charl-Alexandre Le Brun 氏、Chatwork株式会社