{"id":14795,"date":"2013-09-09T08:00:03","date_gmt":"2013-09-08T23:00:03","guid":{"rendered":"http:\/\/www.sssg.org\/blogs\/hiro345\/?p=14795"},"modified":"2013-09-09T10:11:56","modified_gmt":"2013-09-09T01:11:56","slug":"php%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8b%e3%83%87%e3%82%a3%e3%83%ac%e3%82%af%e3%83%88%e3%83%aa%e3%83%88%e3%83%a9%e3%83%90%e3%83%bc%e3%82%b5%e3%83%ab%e3%81%b8%e3%81%ae%e5%af%be%e5%bf%9c%e6%96%b9","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/14795.html","title":{"rendered":"PHP\u306b\u304a\u3051\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u30c8\u30e9\u30d0\u30fc\u30b5\u30eb\u3078\u306e\u5bfe\u5fdc\u65b9\u6cd5"},"content":{"rendered":"<p>\u3044\u307e\u3055\u3089\u306a\u304c\u3089\u3001\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u30c8\u30e9\u30d0\u30fc\u30b5\u30eb\u3078\u306e\u5bfe\u5fdc\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8abf\u3079\u307e\u3057\u305f\u3002<br \/>\n<!--more--><br \/>\n<strong>\u554f\u984c\u3068\u306a\u308b\u8106\u5f31\u6027<\/strong><br \/>\n\u6b21\u306e\u3088\u3046\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092 getfile_ng.php \u3068\u3057\u3066\u7528\u610f\u3057\u3001http:\/\/example.jp\/getfile_ng.php \u3067\u52d5\u4f5c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; gutter: true\">\r\n&lt;?php\r\n$filename=$_GET[&#039;filename&#039;];\r\n$file = &#039;\/var\/www\/html\/&#039; . $filename;\r\nif (file_exists($file) === true) {\r\n  readfile($file); \/\/ NG\r\n}\r\n<\/pre>\n<p>\u3053\u306e\u3068\u304d\u300chttp:\/\/example.jp\/getfile_ng.php?filename=..\/..\/..\/..\/..\/etc\/passwd\u300d\u3068\u3059\u308b\u3068\/etc\/passwd\u304c\u8868\u793a\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u3059\u3050\u306b\u601d\u3044\u3064\u304f\u5bfe\u5fdc\u7b56\u3068\u3057\u3066\u306f\u3001\u300c.html\u300d\u306e\u3088\u3046\u306b\u56fa\u5b9a\u306e\u6587\u5b57\u5217\u3092\u3064\u3051\u308b\u65b9\u6cd5\u3067\u3059\u3002\u3053\u3046\u3057\u3066\u304a\u3051\u3070\u5927\u4e08\u592b\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<pre class=\"brush: php; gutter: true\">\r\n&lt;?php\r\n$filename=$_GET[&#039;filename&#039;];\r\n$file = &#039;\/var\/www\/html\/&#039; . $filename . &#039;.html&#039;;\r\nif (file_exists($file) === true) {\r\n  readfile($file); \/\/ NG\r\n}\r\n<\/pre>\n<p>\u3053\u308c\u306b\u5bfe\u3057\u3066\u306f\u300chttp:\/\/example.jp\/getfile_ng.php?filename=..\/..\/..\/..\/..\/etc\/passwd%00\u300d\u3068\u3059\u308b\u3068\/etc\/passwd\u304c\u8868\u793a\u3055\u308c\u3066\u3057\u307e\u3046\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002Firefox 23.0.1 for Linux, PHP 5.4.19 on CentOS6\u3067\u306f\u554f\u984c\u306f\u767a\u751f\u3057\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001\u53c2\u8003\u8cc7\u6599\u3067\u306f\u3053\u306e\u3088\u3046\u306b\u7d39\u4ecb\u3055\u308c\u3066\u3044\u305f\u306e\u3067\u53e4\u3044\u74b0\u5883\u306a\u3069\u3067\u306f\u767a\u751f\u3059\u308b\u3068\u601d\u308f\u308c\u308b\u305f\u3081\u3001\u7d39\u4ecb\u3092\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u3053\u3046\u3044\u3063\u305f\u65b9\u6cd5\u3067\u306f\u5bfe\u7b56\u304c\u8db3\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u4e00\u822c\u306b\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306a\u5bfe\u7b56\u306e\u3044\u305a\u308c\u304b\u3092\u3068\u308c\u3070\u826f\u3044\u3068\u8a00\u308f\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30d5\u30a1\u30a4\u30eb\u540d\u306b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u540d\u304c\u542b\u307e\u308c\u306a\u3044\u3088\u3046\u306b\u3059\u308b<\/li>\n<li>\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u59a5\u5f53\u6027\u30c1\u30a7\u30c3\u30af\u3092\u3059\u308b\uff08\u5358\u7d14\u306a\u306e\u306f\u82f1\u6570\u5b57\u306b\u9650\u5b9a\u304b\u3064\u62e1\u5f35\u5b50\u306f\u56fa\u5b9a\uff09<\/li>\n<li>\u5916\u90e8\u304b\u3089\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u76f4\u63a5\u6307\u5b9a\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3059\u308b<\/li>\n<\/ul>\n<p><strong>strpos\u3068str_replace\u3067\u5bfe\u7b56<\/strong><br \/>\n\u5bfe\u5fdc\u7b56\u306f\u30d5\u30a1\u30a4\u30eb\u6307\u5b9a\u3057\u305f\u3044\u3082\u306e\u304c\u3069\u3046\u3044\u3063\u305f\u3082\u306e\u3067\u3042\u308b\u304b\u3068\u3044\u3046\u8981\u4ef6\u306b\u3088\u308a\u5909\u308f\u3063\u3066\u304d\u307e\u3059\u3002\u3072\u3068\u3064\u306e\u65b9\u6cd5\u306b\u3001\u300c..\u300d\u304c\u542b\u307e\u308c\u3066\u3044\u305f\u3089\u51e6\u7406\u3092\u7d42\u4e86\u3057\u3066\u3001NULL\u30d0\u30a4\u30c8\u653b\u6483\u306b\u5bfe\u3057\u3066\u306fNULL\u3092\u9664\u53bb\u3059\u308b\u51e6\u7406\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; gutter: true\">\r\n&lt;?php\r\nfunction get_filename() {\r\n  $filename=$_GET[&#039;filename&#039;];\r\n  if (strpos($filename, &#039;..&#039;) !== false) {\r\n    die(&#039;filename\u306b\u306f\u76f8\u5bfe\u30d1\u30b9\u306f\u6307\u5b9a\u4e0d\u53ef&#039;);\r\n  }\r\n  return str_replace(&#039;\\0&#039;, &#039;&#039;, $filename);\r\n}\r\n\r\n$filename = get_filename();\r\n$file = &#039;\/var\/www\/html\/&#039; . $filename;\r\nif (file_exists($file) === true) {\r\n  readfile($file); \/\/ OK\r\n}\r\n<\/pre>\n<p>strpos\u3067\u300c..\u300d\u3092\u30c1\u30a7\u30c3\u30af\u3001str_replace\u3067\u300c\\0\u300d\u3092\u9664\u53bb\u3068\u3044\u3046\u51e6\u7406\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><strong>basename\u3068str_replace\u3068mb_ereg\u3067\u5bfe\u7b56<\/strong><br \/>\nbasename\u95a2\u6570\u3092\u4f7f\u3063\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u6307\u5b9a\u306b\u306f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u540d\u3092\u6307\u5b9a\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3059\u3002\u6b21\u306e\u4f8b\u3067\u306fNULL\u30d0\u30a4\u30c8\u653b\u6483\u306b\u5bfe\u3059\u308bNULL\u3092\u9664\u53bb\u3059\u308b\u51e6\u7406\u306f\u5165\u308c\u3066\u3001\u62e1\u5f35\u5b50\u304c\u60f3\u5b9a\u3057\u3066\u3044\u308b\u3082\u306e\u3068\u4e00\u81f4\u3057\u3066\u3044\u308b\u304b\u3092\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; gutter: true\">\r\n&lt;?php\r\nfunction get_file() {\r\n  $filename = str_replace(&#039;\\0&#039;, &#039;&#039;, $_GET[&#039;filename&#039;]);\r\n  return &#039;\/var\/www\/html\/&#039; . basename($filename) . &#039;.html&#039;;\r\n}\r\n\r\n$file = get_file();\r\nif (file_exists($file) === true) {\r\n  if (mb_ereg(&#039;\\.html$&#039;, $file) === 1) {\r\n    readfile($file); \/\/ OK\r\n  } else {\r\n    die(&quot;\u62e1\u5f35\u5b50\u304c .html \u4ee5\u5916\u3067\u3059&quot;);\r\n  }\r\n}\r\n<\/pre>\n<p>\u3053\u3053\u3067\u306fNULL\u30d0\u30a4\u30c8\u653b\u6483\u306a\u3069\u306b\u5bfe\u3057\u3066\u5b89\u5168\u306a\u30d0\u30a4\u30ca\u30ea\u30bb\u30fc\u30d5\u306a\u95a2\u6570mb_ereg\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002\u540c\u3058\u304f\u30d0\u30a4\u30ca\u30ea\u30bb\u30fc\u30d5\u306apreg_match\u306a\u3069\u3092\u4f7f\u3046\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3059\u3002\u300c<a href=\"http:\/\/php.net\/manual\/ja\/ref.pcre.php\">PHP: PCRE \u95a2\u6570 &#8211; Manual<\/a>\u300d\u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u95a2\u6570\u3067\u3059\u306d\u3002\u4f3c\u305f\u3088\u3046\u306a\u95a2\u6570\u3067\u3001\u300c<a href=\"http:\/\/php.net\/manual\/ja\/ref.regex.php\">PHP: POSIX \u6b63\u898f\u8868\u73fe\u95a2\u6570 &#8211; Manual<\/a>\u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u95a2\u6570\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u3061\u3089\u306f\u30d0\u30a4\u30ca\u30ea\u30bb\u30fc\u30d5\u3067\u306f\u306a\u304fPHP5.3.0\u3067\u975e\u63a8\u5968\u3068\u306a\u308aPHP6.0.0\u3067\u524a\u9664\u3055\u308c\u308b\u305d\u3046\u306a\u306e\u3067\u3001\u7279\u5225\u306a\u7406\u7531\u304c\u306a\u3051\u308c\u3070\u5229\u7528\u3057\u306a\u3044\u65b9\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p><strong>\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u30c1\u30a7\u30c3\u30af<\/strong><br \/>\n\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u30d5\u30a1\u30a4\u30eb\u540d\u5168\u4f53\u306b\u3064\u3044\u3066\u6b63\u898f\u8868\u73fe\u3067\u59a5\u5f53\u6027\u30c1\u30a7\u30c3\u30af\u3092\u3057\u305f\u65b9\u304c\u826f\u3044\u5834\u5408\u3082\u3042\u308b\u3067\u3057\u3087\u3046\u3002\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u82f1\u6570\u5b57\u3060\u3051\u3067\u3088\u3044\u306e\u306a\u3089\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; gutter: true\">\r\n&lt;?php\r\nfunction get_file() {\r\n  $filename = str_replace(&#039;\\0&#039;, &#039;&#039;, $_GET[&#039;filename&#039;]);\r\n  if (! preg_match(&#039;\/\\A[a-z0-9]+\\z\/ui&#039;, $filename)) {\r\n    die(&#039;filename\u306f\u82f1\u6570\u5b57\u306e\u307f\u6307\u5b9a\u53ef\u80fd&#039;);\r\n  }\r\n  return &#039;\/var\/www\/html\/&#039; . basename($filename) . &#039;.html&#039;;\r\n}\r\n\r\n$file = get_file();\r\nif (file_exists($file) === true &amp;&amp; mb_ereg(&#039;\\.html$&#039;, $file) === 1) {\r\n  readfile($file); \/\/ OK\r\n}\r\n<\/pre>\n<p>\u3069\u3053\u3067\u30d5\u30a1\u30a4\u30eb\u540d\u30c1\u30a7\u30c3\u30af\u3092\u3059\u308b\u306e\u304c\u826f\u3044\u306e\u304b\u306f\u610f\u898b\u304c\u8272\u3005\u3042\u308a\u305d\u3046\u3067\u3059\u304c\u3001\u6700\u5f8c\u306b\u958b\u304f\u3068\u3053\u308d\u3067\u30c1\u30a7\u30c3\u30af\u3057\u3066\u304a\u3051\u3070\u9593\u9055\u3044\u304c\u306a\u3044\u3060\u308d\u3046\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u6700\u5f8c\u306b\u3044\u308c\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<p><strong>open_basedir\u3092\u6307\u5b9a\u3057\u3066\u3088\u308a\u5b89\u5168\u306b<\/strong><br \/>\nPHP5.3.0\u4ee5\u964d\u3067\u3042\u308c\u3070\u3001open_basedir\u3092\u6307\u5b9a\u3057\u3066\u3001\u9650\u5b9a\u3055\u308c\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u5185\u306e\u307f\u516c\u958b\u3068\u3044\u3046\u65b9\u6cd5\u304c\u3068\u308c\u307e\u3059\u3002\u4e0a\u8a18\u306e\u5bfe\u7b56\u3092\u7a81\u7834\u3055\u308c\u305f\u3068\u304d\u306e\u4fdd\u967a\u3068\u3057\u3066\u5165\u308c\u3066\u304a\u304f\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: php; gutter: true\">\r\n&lt;?php\r\nini_set(&#039;open_basedir&#039;, &#039;\/var\/www\/html&#039;); \/\/ \u8ffd\u52a0\r\n\r\nfunction get_filename() {\r\n  $filename=$_GET[&#039;filename&#039;];\r\n  if (strpos($filename, &#039;..&#039;) !== false) {\r\n    die(&#039;filename\u306b\u306f\u76f8\u5bfe\u30d1\u30b9\u306f\u6307\u5b9a\u4e0d\u53ef&#039;);\r\n  }\r\n  return str_replace(&#039;\\0&#039;, &#039;&#039;, $filename);\r\n}\r\n\r\n$filename = get_filename();\r\n$file = &#039;\/var\/www\/html\/&#039; . $filename;\r\nif (file_exists($file) === true) {\r\n  readfile($file); \/\/ OK\r\n}\r\n<\/pre>\n<p>\u4e0b\u8a18\u66f8\u7c4d\u304c\u53c2\u8003\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li><a type=\"amzn\" asin=\"4774144371\">\u30d1\u30fc\u30d5\u30a7\u30af\u30c8PHP (PERFECT SERIES 3) <\/a><\/li>\n<li><a type=\"amzn\" asin=\"4797361190\">\u4f53\u7cfb\u7684\u306b\u5b66\u3076 \u5b89\u5168\u306aWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u4f5c\u308a\u65b9\u3000\u8106\u5f31\u6027\u304c\u751f\u307e\u308c\u308b\u539f\u7406\u3068\u5bfe\u7b56\u306e\u5b9f\u8df5 <\/a><\/li>\n<\/ul>\n<p><iframe loading=\"lazy\" style=\"width: 120px; height: 240px;\" src=\"\/\/rcm-jp.amazon.co.jp\/e\/cm?t=hiro345-22&amp;o=9&amp;p=8&amp;l=as1&amp;asins=4774144371&amp;ref=tf_til&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr\" height=\"240\" width=\"320\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><iframe loading=\"lazy\" style=\"width: 120px; height: 240px;\" src=\"\/\/rcm-jp.amazon.co.jp\/e\/cm?t=hiro345-22&amp;o=9&amp;p=8&amp;l=as1&amp;asins=4797361190&amp;ref=tf_til&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr\" height=\"240\" width=\"320\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3044\u307e\u3055\u3089\u306a\u304c\u3089\u3001\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u30c8\u30e9\u30d0\u30fc\u30b5\u30eb\u3078\u306e\u5bfe\u5fdc\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8abf\u3079\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[295],"class_list":["post-14795","post","type-post","status-publish","format-standard","hentry","category-program","tag-php"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/14795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=14795"}],"version-history":[{"count":8,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/14795\/revisions"}],"predecessor-version":[{"id":14852,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/14795\/revisions\/14852"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=14795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=14795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=14795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}