twxxk (owner)

Revisions

gist: 38981 Download_button fork
public
Public Clone URL: git://gist.github.com/38981.git
Embed All Files: show embed
phpinfox.php #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* phpinfox.php
* 単一ファイルでphpinfoおよび、入力したコードの実行結果を確認できる
* PHP5用
*
* @author twk
*/
 
/*
アクセス制御はできれば .htaccess 等に行った方が安全です。
設定例
<Files ~ "^phpinfox\.php$">
order deny,allow
deny from all
allow from 127.0.0.1
allow from 192.168.
</Files>
*/
 
/** アクセス可能なREMOTE_ADDRの値(前方一致)*/
$phpInfoxAllowedIps = array(
'127.0.0.1', '192.168.',
);
 
if (get_magic_quotes_gpc()) {
$in = array(&$_GET, &$_POST, &$_COOKIE);
while (list($k,$v) = each($in)) {
foreach ($v as $key => $val) {
if (!is_array($val)) {
$in[$k][$key] = stripslashes($val);
continue;
}
$in[] =& $in[$k][$key];
}
}
unset($in);
}
 
/**
* TODO PHP内でIPアドレスチェックできるようにする
* TODO PEARや各フレームワーク等の存在チェックと環境確認ができるようにする
* TODO Session等を使い入力履歴を管理できるようにする
* TODO フレームワーク内に組み込めるようにする(iframeを使わないオプション)
* TODO よく使う設定値のみを確認できるようにする
*/
 
if (!count(array_filter($phpInfoxAllowedIps,
create_function('$addr', 'return strpos($_SERVER["REMOTE_ADDR"], $addr) === 0;'))))
{
header('HTTP/1.0 403 Forbidden');
header('Content-Type: text/plain');
echo "Forbidden ({$_SERVER['REMOTE_ADDR']})";
exit;
}
 
$gView = array(
'escape' => true,
'parent' => !count($_POST),
'php' => '',
'result' => '',
);
 
function phpinfox()
{
global $gView;
 
$php = @$_POST['php'];
if (empty($php)) $php = <<<EOM
\$s = '';
 
\$s .= rand();
 
print_r(\$s);
EOM;
 
$result = '';
$escape = true;
 
// eval
ob_start();
try
{
$php = trim($php);
if ($php && substr($php, -1, 1) != ';')
$php .= ';';
 
eval($php);
}
catch (Exception $e)
{
$escape = false;
echo $e->getMessage() . $e->getTraceAsString();
}
$result .= ob_get_clean();
 
 
$gView = array(
'escape' => $escape,
'parent' => !count($_POST),
'php' => $php,
'result' => $result,
);
}
function h($var)
{
global $gView;
return $gView['escape'] ? htmlspecialchars($var) : $var;
}
 
phpinfox();
 
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpinfox</title>
<style>
#phpinfox h1 { font-size: 18px; margin: 0px; background-color: #f0f0f0; }
#phpinfox h2 { font-size: 16px; margin: 0px; margin-right: 0.5em; }
#phpinfox textarea { width: 90%; height: 10em; }
#phpinfox iframe { border: solid 1px #444; width: 90%; }
</style>
</head>
<body>
<?php if ($gView['parent']) : ?>
<?php
// phpinfo handling
ob_start();
phpinfo();
$matches = array();
preg_match('%(<style[^>]*>.*?</style>).*?<body[^>]*>(.*)</body>%s', ob_get_clean(), $matches);
$phpinfo = $matches[1] . $matches[2];
 
// pear info handling
$pearInfo = 'with PEAR';
ob_start();
try
{
$b = @include_once 'PEAR.php';
if ($b)
$pearInfo = 'without PEAR';
// may produce Fatal error: Call to a member function getVersion() on a non-object
// in PEAR/Info.php on line 57
// $info = new PEAR_Info();
// $info->show();
}
catch (Exception $e){
// echo $e->getMessage() . $e->getTraceAsString();
}
// $pearInfo = ob_get_clean();
?>
<div id="phpinfox">
<h1>phpinfox</h1>
<form method="post" action="phpinfox.php" target="result">
<h2>PHP (<?php echo htmlspecialchars(PHP_VERSION), ' ', $pearInfo; ?>)</h2>
<textarea name="php"
><?php echo htmlspecialchars($gView['php']); ?></textarea><br />
<input type="submit" /><input type="reset" /> echo等で出力するのをお忘れなく。<br />
<br />
<h2>実行結果</h2>
<iframe name="result" src="phpinfox.php"></iframe>
</form>
</div>
<?php echo $phpinfo; ?>
<?php else : ?>
<?php echo h($gView['result']); ?>
<?php endif; ?>
</body>
</html>