<?xml version="1.0" encoding="UTF-8" ?>
<feed xml:lang="ja" xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0">
  <title type="text">安全でないPHPプログラムの作り方</title>
  <subtitle type="html">Insecure PHP Programming</subtitle>
  <link rel="self" type="application/atom+xml" href="https://insecurephp.blog.shinobi.jp/atom"/>
  <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/"/>
  <updated>2008-10-29T15:35:47+09:00</updated>
  <author><name>Kenji</name></author>
  <generator uri="//www.ninja.co.jp/blog/" version="0.9">忍者ブログ</generator>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" />
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/15</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%A4%E3%83%B3%E3%82%B8%E3%82%A7%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3/%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%E3%81%AE%E8%84%86%E5%BC%B1%E6%80%A7-4-" />
    <published>2008-11-21T23:53:07+09:00</published> 
    <updated>2008-11-21T23:53:07+09:00</updated> 
    <category term="スクリプトインジェクション" label="スクリプトインジェクション" />
    <title>ログインフォームの脆弱性(4)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> の解説です。ソースコードをまだ見られていない方は、まず、<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> をご覧ください。<br />
<br />
<br />
この脆弱性を取り除くには、<br />
<br />
&lt;form method=&quot;POST&quot; action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot;&gt;<br />
<br />
の部分は、以下のようにするのがよいと思います。<br />
<br />
&lt;form method=&quot;POST&quot; action=&quot;&quot;&gt;<br />
<br />
あるいは、$_SERVER['PHP_SELF'] を <a href="http://www.php.net/manual/ja/function.htmlspecialchars.php">htmlspecialchars()</a> でエスケープ処理する方法もあるかと思います。<br />
<br />
&lt;form method=&quot;POST&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'); ?&gt;&quot;&gt;<br />
<br />
<br />
また、以下のユーザ名の入力値を表示する部分は、<br />
<br />
&lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;&lt;?php echo $_POST['username']; ?&gt;<br />
<br />
以下のように、<a href="http://www.php.net/manual/ja/function.htmlspecialchars.php">htmlspecialchars()</a> でエスケープ処理します。<br />
<br />
&lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;&lt;?php echo htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8'); ?&gt;<br />
<br />
<a href="http://www.php.net/manual/ja/function.htmlspecialchars.php">htmlspecialchars()</a> でエスケープ処理する場合は、必ず、第2引数に ENT_QUOTES を指定しましょう。デフォルトでは、シングルクオート(')がエスケープされませんので、シングルクオートでインジェクションできるケースでは脆弱性が残る可能性があります。<br />
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/14</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E3%81%93%E3%81%AE%E3%83%96%E3%83%AD%E3%82%B0%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6/%E3%81%93%E3%81%AE%E3%83%96%E3%83%AD%E3%82%B0%E3%81%AE%E7%9B%AE%E7%9A%84" />
    <published>2008-11-13T15:34:09+09:00</published> 
    <updated>2008-11-13T15:34:09+09:00</updated> 
    <category term="このブログについて" label="このブログについて" />
    <title>このブログの目的</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[このブログの目的は、意識的に、自由自在に、効率的に、安全でない(脆弱性のある) PHPプログラムを作成できるようになることです。

無意識に脆弱性のあるプログラムを作るという状態がなくなることを目指しています。意識的に脆弱性を入れられるようになれば、今までと同じ作業時間でも多くの脆弱性を作れるようになるでしょう。

そのような状態になれれば、脆弱性は、すべて意識されたものとなります。もし望むなら、脆弱性のない安全なプログラムを作ることもできるでしょう。そんなことを望む人がいるかどうかはわかりませんが。(笑)

ただし、そのような状態になるのは、正直言って簡単ではありません。
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/13</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%A4%E3%83%B3%E3%82%B8%E3%82%A7%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3/%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%E3%81%AE%E8%84%86%E5%BC%B1%E6%80%A7-3-" />
    <published>2008-11-13T15:13:34+09:00</published> 
    <updated>2008-11-13T15:13:34+09:00</updated> 
    <category term="スクリプトインジェクション" label="スクリプトインジェクション" />
    <title>ログインフォームの脆弱性(3)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> の解説です。ソースコードをまだ見られていない方は、まず、<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> をご覧ください。<br />
<br />
<br />
補足ですが、コメントにあったように URL を<br />
<br />
<blockquote>http://localhost/this.php?username=%22%3E%3Cscript%20type=<br />
%22text/javascript%22%3Ealert('xss')%3C/script%3E%3Ca%20name=%22a<br />
</blockquote><br />
<br />
のように入力したとしても、このログインフォームでは、クロスサイトスクリプティングはできません。実際にそのように入力しても警告は表示されません。<br />
<br />
これは、username の表示に、<br />
<br />
<blockquote>$_POST['username']</blockquote><br />
<br />
と $_POST を使っているために、GETリクエストで値を送信しても無視されるためです。これを上記のように GET でも攻撃可能にするには、$_POST を $_REQUEST に変更すれば ok です。具体的には、<br />
<br />
<blockquote>ユーザ名: &lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;&lt;?php echo <span style="color:#FF0000">$_REQUEST['username']</span>; ?&gt;&quot;&gt;&lt;br&gt;</blockquote><br />
<br />
です。<br />
<br />
こうすれば、inputタグは、以下のようになり、スクリプトが実行可能になります。<br />
<br />
&lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;<span style="color:#FF0000">&quot;&gt;&lt;script type=&quot;text/javascript&quot;&gt;alert('xss')&lt;/script&gt;&lt;a name=&quot;a</span>&quot;&gt;<br />
<br />
<a href="http://insecurephp.blog.shinobi.jp/Entry/15/">ログインフォームの脆弱性(4)</a> へ続く。<br />
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/12</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E9%9B%91%E8%AB%87/%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%E3%81%AE%E3%82%BF%E3%82%A4%E3%83%88%E3%83%AB" />
    <published>2008-11-09T21:25:07+09:00</published> 
    <updated>2008-11-09T21:25:07+09:00</updated> 
    <category term="雑談" label="雑談" />
    <title>コメントのタイトル</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[いろいろコメントが付くようになったのですが、このブログサービスでは、コメントを記入されるとき、タイトルを入れないと「無題」と付くようです。

ということで、タイトルを入れないと全部「無題」で新しいコメントかどうかの区別が付きにくいです。

コメントをご記入の場合は、できるだけ何かタイトルを入れるようにお願いします。
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/11</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E8%84%86%E5%BC%B1%E6%80%A7%E3%81%AE%E3%81%82%E3%82%8B%E3%82%BD%E3%83%BC%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89/%E8%84%86%E5%BC%B1%E6%80%A7%E3%81%AE%E3%81%82%E3%82%8B%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E5%AE%9F%E8%A1%8C" />
    <published>2008-11-06T20:34:20+09:00</published> 
    <updated>2008-11-06T20:34:20+09:00</updated> 
    <category term="脆弱性のあるソースコード" label="脆弱性のあるソースコード" />
    <title>脆弱性のあるコマンド実行</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[exec()関数で、外部コマンドを実行しています。OSコマンドインジェクションを防ぐために「"」をエスケープしています。
<br><br>
どこが脆弱でしょうか？
<br><br>

<textarea cols="60" rows="8" class="prettyprint">
<?php

$cmd = '/usr/bin/wget';

$URI = 'http://' . $_GET['url'];
$safer_URI = strtr($URI, "\"", " ");
exec($cmd . " \"" . $safer_URI . "\"", $results, $return);
</textarea>
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/10</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%A4%E3%83%B3%E3%82%B8%E3%82%A7%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3/%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%E3%81%AE%E8%84%86%E5%BC%B1%E6%80%A7-2-" />
    <published>2008-11-06T20:13:09+09:00</published> 
    <updated>2008-11-06T20:13:09+09:00</updated> 
    <category term="スクリプトインジェクション" label="スクリプトインジェクション" />
    <title>ログインフォームの脆弱性(2)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> の解説です。ソースコードをまだ見られていない方は、まず、<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> をご覧ください。<br />
<br />
<br />
<br />
すでにコメントでほぼ明らかになっていますが、このコードの二つ目の脆弱性は、<br />
<br />
<blockquote><br />
&lt;form method=&quot;POST&quot; action=&quot;&lt;?php echo <span style="color:#FF0000">$_SERVER['PHP_SELF']</span>; ?&gt;&quot;&gt;<br />
</blockquote><br />
<br />
の $_SERVER['PHP_SELF'] をそのまま表示していることです。$_SERVER['PHP_SELF'] は 現在実行しているスクリプトのファイル名ですが、この値には PATH_INFO も含まれます。<br />
<br />
例えば、URL に「http://localhost/this.php<span style="color:#FF0000">/%22%3E%3Cscript%3Ealert('xss')%3C/script%3E%3Ca%20name=%22a</span>」と入力すると、この form タグは、<br />
<br />
<blockquote><br />
&lt;form method=&quot;POST&quot; action=&quot;/this.php<span style="color:#FF0000">/&quot;&gt;&lt;script&gt;alert('xss')&lt;/script&gt;&lt;a name=&quot;a</span>&quot;&gt;<br />
</blockquote><br />
<br />
となります。<br />
<br />
実際にやってみるとすぐにわかりますが、この時、警告ダイアログがブラウザの画面に表示されます。入力した JavaScript のコードがブラウザで実行されていることがわかります。<br />
<br />
これで、ユーザが自由に JavaScript コードをページに埋め込むことができることがわかります。これも、クロスサイトスクリプティング(XSS)脆弱性です。<br />
<br />
<a href="http://insecurephp.blog.shinobi.jp/Entry/13/">ログインフォームの脆弱性(3)</a> へ続く。<br />
<br />
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/9</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E8%84%86%E5%BC%B1%E6%80%A7%E3%81%AE%E3%81%82%E3%82%8B%E3%82%BD%E3%83%BC%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89/%E3%82%82%E3%81%A3%E3%81%A8%E8%84%86%E5%BC%B1%E6%80%A7%E3%81%AE%E3%81%82%E3%82%8B%E3%83%A1%E3%83%BC%E3%83%AB%E9%80%81%E4%BF%A1%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0" />
    <published>2008-11-04T20:57:44+09:00</published> 
    <updated>2008-11-04T20:57:44+09:00</updated> 
    <category term="脆弱性のあるソースコード" label="脆弱性のあるソースコード" />
    <title>もっと脆弱性のあるメール送信フォーム</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[よくあるメール送信フォームです。
このフォームを使い迷惑メールを送ってみてください。
<br><br>

なお、表示の関係でソースコード中の textarea タグを textarea_ としています。
<br><br>

<textarea cols="60" rows="42" class="prettyprint">
<?php

if ( ! empty($_POST['submit'])) {
    $to      = $_POST['to'];
    $subject = $_POST['subject'];
    $body    = $_POST['body'];
    $from    = $_POST['from'];

    if (mb_send_mail($to, $subject, $body, 'From: ' . $from)) {
        echo "メールを送信しました";
    }
    else {
        echo "メール送信失敗です";
    }

    exit;
}
?>

<html>
<head>
<title>メール送信フォーム</title>
</head>

<body>

<form method="POST" action="">
名前: 
<input type="text" name="username" size="20"><br>
メールアドレス: 
<input type="text" name="from" size="20"><br>
件名: 
<input type="text" name="subject" size="20"><br>
本文:
<textarea_ row="5" name="body" cols="40"></textarea_>
<input type="hidden" name="to" value="kenji@example.jp">
<input type="submit" name="submit" value="送信">
</form>

</body>
</html>
</textarea>
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/8</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E8%84%86%E5%BC%B1%E6%80%A7%E3%81%AE%E3%81%82%E3%82%8B%E3%82%BD%E3%83%BC%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89/%E5%95%8F%E9%A1%8C%E3%81%AE%E3%81%82%E3%82%8B%E8%AA%8D%E8%A8%BC%E4%BB%98%E3%81%8D%E3%83%9A%E3%83%BC%E3%82%B8" />
    <published>2008-11-03T10:34:49+09:00</published> 
    <updated>2008-11-03T10:34:49+09:00</updated> 
    <category term="脆弱性のあるソースコード" label="脆弱性のあるソースコード" />
    <title>問題のある認証付きページ</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[ログイン済みのユーザのみアクセスできるページの処理です。ログインしているかどうか判定する関数 loggedin() の中身は省略しています。ログインしていないユーザはログインページにリダイレクトします。
<br><br>


このコードのどこが問題でしょうか？
<br><br>


<textarea cols="60" rows="15" class="prettyprint">
<?php

function loggedin() {
    // ログインしているかどうか判定
    return FALSE;
}


if (loggedin() == FALSE) {
    // ログインしていない場合はログインページへリダイレクト
    header('Location: http://www.example.jp/login.php');
}

// ログイン済みユーザの処理
... 略 ...
</textarea>
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/7</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E3%81%93%E3%81%AE%E3%83%96%E3%83%AD%E3%82%B0%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6/%E3%81%93%E3%81%AE%E3%83%96%E3%83%AD%E3%82%B0%E3%81%AE%E5%AF%BE%E8%B1%A1%E8%AA%AD%E8%80%85" />
    <published>2008-11-02T12:08:05+09:00</published> 
    <updated>2008-11-02T12:08:05+09:00</updated> 
    <category term="このブログについて" label="このブログについて" />
    <title>このブログの対象読者</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[このブログの対象読者を書いていませんでした。

このブログの対象読者は「PHP初心者〜」です。なので、PHP初心者の方も含まれています。初心者からの疑問やコメントなども歓迎します。

]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
  <entry>
    <id>insecurephp.blog.shinobi.jp://entry/6</id>
    <link rel="alternate" type="text/html" href="https://insecurephp.blog.shinobi.jp/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%A4%E3%83%B3%E3%82%B8%E3%82%A7%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3/%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%E3%81%AE%E8%84%86%E5%BC%B1%E6%80%A7-1-" />
    <published>2008-11-02T09:50:10+09:00</published> 
    <updated>2008-11-02T09:50:10+09:00</updated> 
    <category term="スクリプトインジェクション" label="スクリプトインジェクション" />
    <title>ログインフォームの脆弱性(1)</title>
    <content mode="escaped" type="text/html" xml:lang="utf-8"> 
      <![CDATA[<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> の解説です。ソースコードをまだ見られていない方は、まず、<a href="http://insecurephp.blog.shinobi.jp/Entry/2/">脆弱性のあるログインフォーム</a> をご覧ください。<br />
<br />
<br />
<br />
すでにコメントでほぼ明らかになっていますが、このコードの脆弱性のひとつは、<br />
<br />
<blockquote>ユーザ名: &lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;&lt;?php echo <span style="color:#FF0000">$_POST['username']</span>; ?&gt;&quot;&gt;&lt;br&gt;</blockquote><br />
<br />
の $_POST['username'] をそのまま表示していることです。$_POST['username'] はユーザが POSTメソッドで送信した usernameフィールドの値ですね。<br />
<br />
例えば、ユーザ名に「&quot;&gt;&lt;script&gt;alert('xss')&lt;/script&gt;&lt;a name=&quot;a」と入力すると、この inputタグは、<br />
<br />
<blockquote>&lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;<span style="color:#FF0000">&quot;&gt;&lt;script&gt;alert('xss')&lt;/script&gt;&lt;a name=&quot;a</span>&quot;&gt;</blockquote><br />
<br />
となります。<br />
<br />
実際にやってみるとすぐにわかりますが、この時、警告ダイアログがブラウザの画面に表示されます。入力した JavaScript のコードがブラウザで実行されていることがわかります。<br />
<br />
これで、ユーザが自由に JavaScript コードをページに埋め込むことができることがわかります。これは、典型的なクロスサイトスクリプティング(XSS)脆弱性です。<br />
<br />
これが何故、危険かわからない人がいましたら、コメントにその旨ご記入ください。<br />
<br />
続きは、<a href="http://insecurephp.blog.shinobi.jp/Entry/10/">ログインフォームの脆弱性(2)</a><br />
]]> 
    </content>
    <author>
            <name>Kenji</name>
        </author>
  </entry>
</feed>