Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1 91bf75df Scott Ullrich
<?php
2 2900e518 Scott Ullrich
/*
3 c5d81585 Renato Botelho
 * diag_command.php
4 cb41dd63 Renato Botelho
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * All rights reserved.
8 fd9ebcd5 Stephen Beaver
 *
9 c5d81585 Renato Botelho
 * Exec+ v1.02-000 - Copyright 2001-2003, All rights reserved
10
 * Created by technologEase (http://www.technologEase.com)
11
 * (modified for m0n0wall by Manuel Kasper <mk@neon1.net>)\
12 9da2cf1c Stephen Beaver
 *
13 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
14
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
15
 * All rights reserved.
16 fd9ebcd5 Stephen Beaver
 *
17 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
18
 * you may not use this file except in compliance with the License.
19
 * You may obtain a copy of the License at
20 fd9ebcd5 Stephen Beaver
 *
21 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
22 fd9ebcd5 Stephen Beaver
 *
23 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
24
 * distributed under the License is distributed on an "AS IS" BASIS,
25
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
 * See the License for the specific language governing permissions and
27
 * limitations under the License.
28 fd9ebcd5 Stephen Beaver
 */
29 2900e518 Scott Ullrich
30 6b07c15a Matthew Grooms
##|+PRIV
31
##|*IDENT=page-diagnostics-command
32 5230f468 jim-p
##|*NAME=Diagnostics: Command
33 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Command' page.
34 36a844df Stephen Beaver
##|*MATCH=diag_command.php*
35 6b07c15a Matthew Grooms
##|-PRIV
36
37 7c9a30c8 jim-p
$allowautocomplete = true;
38
39 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
40 458e0e0b Scott Ullrich
41 59255714 NewEraCracker
if ($_POST['submit'] == "DOWNLOAD" && file_exists($_POST['dlPath'])) {
42 5b237745 Scott Ullrich
	session_cache_limiter('public');
43
	$fd = fopen($_POST['dlPath'], "rb");
44
	header("Content-Type: application/octet-stream");
45
	header("Content-Length: " . filesize($_POST['dlPath']));
46 be4b8e72 Scott Ullrich
	header("Content-Disposition: attachment; filename=\"" .
47 5b237745 Scott Ullrich
		trim(htmlentities(basename($_POST['dlPath']))) . "\"");
48 2d181b70 jim-p
	if (isset($_SERVER['HTTPS'])) {
49
		header('Pragma: ');
50
		header('Cache-Control: ');
51
	} else {
52
		header("Pragma: private");
53
		header("Cache-Control: private, must-revalidate");
54
	}
55 be4b8e72 Scott Ullrich
56 5b237745 Scott Ullrich
	fpassthru($fd);
57
	exit;
58 59255714 NewEraCracker
} else if ($_POST['submit'] == "UPLOAD" && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
59 5b237745 Scott Ullrich
	move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
60 8545adde k-paulius
	$ulmsg = sprintf(gettext('Uploaded file to /tmp/%s.'), htmlentities($_FILES['ulfile']['name']));
61 5b237745 Scott Ullrich
}
62 2900e518 Scott Ullrich
63 41b1ff89 Phil Davis
if ($_POST) {
64
}
65 74285e13 Scott Ullrich
66 5b237745 Scott Ullrich
// Function: is Blank
67
// Returns true or false depending on blankness of argument.
68
69 41b1ff89 Phil Davis
function isBlank($arg) {
70 6c07db48 Phil Davis
	return preg_match("/^\s*$/", $arg);
71 41b1ff89 Phil Davis
}
72 5b237745 Scott Ullrich
73
// Function: Puts
74
// Put string, Ruby-style.
75
76 41b1ff89 Phil Davis
function puts($arg) {
77
	echo "$arg\n";
78
}
79 5b237745 Scott Ullrich
80
// "Constants".
81
82 45d6ada5 Sjon Hortensius
$Version = '';
83 aa205c3b Ermal
$ScriptName = $REQUEST['SCRIPT_NAME'];
84 5b237745 Scott Ullrich
85
// Get year.
86
87 45d6ada5 Sjon Hortensius
$arrDT = localtime();
88 5b237745 Scott Ullrich
$intYear = $arrDT[5] + 1900;
89
90 a6a6ee00 k-paulius
$pgtitle = array(gettext("Diagnostics"), gettext("Command Prompt"));
91 998abf60 Bill Marquette
include("head.inc");
92 5b237745 Scott Ullrich
?>
93 8fd9052f Colin Fleming
<script type="text/javascript">
94
//<![CDATA[
95 45d6ada5 Sjon Hortensius
	// Create recall buffer array (of encoded strings).
96 5b237745 Scott Ullrich
<?php
97
98 6c07db48 Phil Davis
if (isBlank($_POST['txtRecallBuffer'])) {
99 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array;");
100 5b237745 Scott Ullrich
} else {
101 6aef15f8 Stephen Beaver
	puts("	 var arrRecallBuffer = new Array(");
102 6c07db48 Phil Davis
	$arrBuffer = explode("&", $_POST['txtRecallBuffer']);
103
	for ($i = 0; $i < (count($arrBuffer) - 1); $i++) {
104 6aef15f8 Stephen Beaver
		puts("		'" . htmlspecialchars($arrBuffer[$i], ENT_QUOTES | ENT_HTML401) . "',");
105 41b1ff89 Phil Davis
	}
106 6aef15f8 Stephen Beaver
	puts("		'" . htmlspecialchars($arrBuffer[count($arrBuffer) - 1], ENT_QUOTES | ENT_HTML401) . "'");
107
	puts("	 );");
108 5b237745 Scott Ullrich
}
109
?>
110 45d6ada5 Sjon Hortensius
111 41b1ff89 Phil Davis
	// Set pointer to end of recall buffer.
112
	var intRecallPtr = arrRecallBuffer.length-1;
113 5b237745 Scott Ullrich
114 45d6ada5 Sjon Hortensius
	// Functions to extend String class.
115
	function str_encode() { return escape( this ) }
116
	function str_decode() { return unescape( this ) }
117
118
	// Extend string class to include encode() and decode() functions.
119
	String.prototype.encode = str_encode
120
	String.prototype.decode = str_decode
121
122
	// Function: is Blank
123
	// Returns boolean true or false if argument is blank.
124
	function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
125
126
	// Function: frmExecPlus onSubmit (event handler)
127
	// Builds the recall buffer from the command string on submit.
128
	function frmExecPlus_onSubmit( form ) {
129 5b237745 Scott Ullrich
130 45d6ada5 Sjon Hortensius
		if (!isBlank(form.txtCommand.value)) {
131
			// If this command is repeat of last command, then do not store command.
132
			if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
133
134
			// Stuff encoded command string into the recall buffer.
135 41b1ff89 Phil Davis
			if (isBlank(form.txtRecallBuffer.value)) {
136 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value = form.txtCommand.value.encode();
137 41b1ff89 Phil Davis
			} else {
138 45d6ada5 Sjon Hortensius
				form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
139 41b1ff89 Phil Davis
			}
140 45d6ada5 Sjon Hortensius
		}
141
142
		return true;
143
	}
144 5b237745 Scott Ullrich
145 45d6ada5 Sjon Hortensius
	// Function: btnRecall onClick (event handler)
146
	// Recalls command buffer going either up or down.
147
	function btnRecall_onClick( form, n ) {
148 5b237745 Scott Ullrich
149 45d6ada5 Sjon Hortensius
		// If nothing in recall buffer, then error.
150
		if (!arrRecallBuffer.length) {
151 6c07db48 Phil Davis
			alert('<?=gettext("Nothing to recall"); ?>!');
152 45d6ada5 Sjon Hortensius
			form.txtCommand.focus();
153
			return;
154
		}
155 5b237745 Scott Ullrich
156 45d6ada5 Sjon Hortensius
		// Increment recall buffer pointer in positive or negative direction
157
		// according to <n>.
158
		intRecallPtr += n;
159 5b237745 Scott Ullrich
160 45d6ada5 Sjon Hortensius
		// Make sure the buffer stays circular.
161
		if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
162
		if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
163 5b237745 Scott Ullrich
164 45d6ada5 Sjon Hortensius
		// Recall the command.
165
		form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
166
	}
167 5b237745 Scott Ullrich
168 45d6ada5 Sjon Hortensius
	// Function: Reset onClick (event handler)
169
	// Resets form on reset button click event.
170
	function Reset_onClick( form ) {
171 5b237745 Scott Ullrich
172 45d6ada5 Sjon Hortensius
		// Reset recall buffer pointer.
173
		intRecallPtr = arrRecallBuffer.length;
174 5b237745 Scott Ullrich
175 45d6ada5 Sjon Hortensius
		// Clear form (could have spaces in it) and return focus ready for cmd.
176
		form.txtCommand.value = '';
177
		form.txtCommand.focus();
178 5b237745 Scott Ullrich
179 45d6ada5 Sjon Hortensius
		return true;
180
	}
181 fa7855f3 Colin Fleming
//]]>
182 5b237745 Scott Ullrich
</script>
183 45d6ada5 Sjon Hortensius
<?php
184 5b237745 Scott Ullrich
185 947141fd Phil Davis
if (isBlank($_POST['txtCommand']) && isBlank($_POST['txtPHPCommand']) && isBlank($ulmsg)) {
186 7457ba51 k-paulius
	print_callout(gettext("The capabilities offered here can be dangerous. No support is available. Use them at your own risk!"), 'danger', gettext('Advanced Users Only'));
187 947141fd Phil Davis
}
188 5b237745 Scott Ullrich
189 59255714 NewEraCracker
if ($_POST['submit'] == "EXEC" && !isBlank($_POST['txtCommand'])):?>
190 45d6ada5 Sjon Hortensius
	<div class="panel panel-success responsive">
191 b7edfd3c Phil Davis
		<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Shell Output - %s'), htmlspecialchars($_POST['txtCommand']))?></h2></div>
192 45d6ada5 Sjon Hortensius
		<div class="panel-body">
193 7b91ab09 Jared Dillard
			<div class="content">
194 5b237745 Scott Ullrich
<?php
195 45d6ada5 Sjon Hortensius
	putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
196
	putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " "));
197 6aef15f8 Stephen Beaver
	$output = array();
198
	exec($_POST['txtCommand'] . ' 2>&1', $output);
199 5eb9f6ad NewEraCracker
200
	$output = implode("\n", $output);
201
	print("<pre>" . htmlspecialchars($output) . "</pre>");
202 7b91ab09 Jared Dillard
?>
203
			</div>
204 45d6ada5 Sjon Hortensius
		</div>
205
	</div>
206 fa172bc5 NewEraCracker
<?php endif; ?>
207 5b237745 Scott Ullrich
208 36a844df Stephen Beaver
<form action="diag_command.php" method="post" enctype="multipart/form-data" name="frmExecPlus" onsubmit="return frmExecPlus_onSubmit( this );">
209 45d6ada5 Sjon Hortensius
	<div class="panel panel-default">
210 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute Shell Command')?></h2></div>
211 45d6ada5 Sjon Hortensius
		<div class="panel-body">
212 7b91ab09 Jared Dillard
			<div class="content">
213 37676f4e jim-p
				<input id="txtCommand" name="txtCommand" placeholder="Command" type="text" class="col-sm-7"	 value="<?=htmlspecialchars($_POST['txtCommand'])?>" />
214 7b91ab09 Jared Dillard
				<br /><br />
215
				<input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>" />
216 37676f4e jim-p
217
				<div class="btn-group">
218 b8ce3e91 jim-p
					<button type="button" class="btn btn-success btn-sm" name="btnRecallPrev" onclick="btnRecall_onClick( this.form, -1 );" title="<?=gettext("Recall Previous Command")?>">
219 37676f4e jim-p
						<i class="fa fa-angle-double-left"></i>
220
					</button>
221 59255714 NewEraCracker
					<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXEC" title="<?=gettext("Execute the entered command")?>">
222 df2aa83e Stephen Beaver
						<i class="fa fa-bolt"></i>
223 37676f4e jim-p
						<?=gettext("Execute"); ?>
224
					</button>
225 b8ce3e91 jim-p
					<button type="button" class="btn btn-success btn-sm" name="btnRecallNext" onclick="btnRecall_onClick( this.form,  1 );" title="<?=gettext("Recall Next Command")?>">
226 37676f4e jim-p
						<i class="fa fa-angle-double-right"></i>
227 6fe069a4 jim-p
					</button>
228 417ace68 Jared Dillard
					<button style="margin-left: 10px;" type="button" class="btn btn-default btn-sm" onclick="return Reset_onClick( this.form );" title="<?=gettext("Clear command entry")?>">
229 37676f4e jim-p
						<i class="fa fa-undo"></i>
230
						<?=gettext("Clear"); ?>
231
					</button>
232
				</div>
233 7b91ab09 Jared Dillard
			</div>
234 45d6ada5 Sjon Hortensius
		</div>
235
	</div>
236
237
	<div class="panel panel-default">
238 3d7a8696 k-paulius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Download File')?></h2></div>
239 45d6ada5 Sjon Hortensius
		<div class="panel-body">
240 7b91ab09 Jared Dillard
			<div class="content">
241 5c0ab3cd NewEraCracker
				<input name="dlPath" type="text" id="dlPath" placeholder="File to download" class="col-sm-4" value="<?=htmlspecialchars($_GET['dlPath']);?>"/>
242 7b91ab09 Jared Dillard
				<br /><br />
243 37676f4e jim-p
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">
244
					<i class="fa fa-download icon-embed-btn"></i>
245
					<?=gettext("Download")?>
246
				</button>
247 7b91ab09 Jared Dillard
			</div>
248 45d6ada5 Sjon Hortensius
		</div>
249
	</div>
250 fbcf0037 Scott Ullrich
251 45d6ada5 Sjon Hortensius
<?php
252 947141fd Phil Davis
	if ($ulmsg) {
253 7c945f74 k-paulius
		print_info_box($ulmsg, 'success', false);
254 947141fd Phil Davis
	}
255 45d6ada5 Sjon Hortensius
?>
256
	<div class="panel panel-default">
257 3d7a8696 k-paulius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Upload File')?></h2></div>
258 45d6ada5 Sjon Hortensius
		<div class="panel-body">
259 7b91ab09 Jared Dillard
			<div class="content">
260
				<input name="ulfile" type="file" class="btn btn-default btn-sm btn-file" id="ulfile" />
261
				<br />
262 37676f4e jim-p
				<button name="submit" type="submit" class="btn btn-primary btn-sm" id="upload" value="UPLOAD">
263
					<i class="fa fa-upload icon-embed-btn"></i>
264
					<?=gettext("Upload")?>
265
				</button>
266 7b91ab09 Jared Dillard
			</div>
267 45d6ada5 Sjon Hortensius
		</div>
268
	</div>
269
<?php
270 f5ba2db3 Stephen Beaver
	// Experimental version. Writes the user's php code to a file and executes it via a new instance of PHP
271
	// This is intended to prevent bad code from breaking the GUI
272 59255714 NewEraCracker
	if ($_POST['submit'] == "EXECPHP" && !isBlank($_POST['txtPHPCommand'])) {
273 3d7a8696 k-paulius
		puts("<div class=\"panel panel-success responsive\"><div class=\"panel-heading\"><h2 class=\"panel-title\">PHP Response</h2></div>");
274 5eb9f6ad NewEraCracker
275 fc8b158f Stephen Beaver
		$tmpname = tempnam("/tmp", "");
276
		$phpfile = fopen($tmpname, "w");
277 3e115dbf Stephen Beaver
		fwrite($phpfile, "<?php\n");
278
		fwrite($phpfile, "require_once(\"/etc/inc/config.inc\");\n");
279
		fwrite($phpfile, "require_once(\"/etc/inc/functions.inc\");\n\n");
280
		fwrite($phpfile, $_POST['txtPHPCommand'] . "\n");
281
		fwrite($phpfile, "?>\n");
282
		fclose($phpfile);
283
284 5eb9f6ad NewEraCracker
		$output = array();
285 45bcbee9 jim-p
		exec("/usr/local/bin/php -d log_errors=off " . $tmpname, $output);
286 3e115dbf Stephen Beaver
287 fc8b158f Stephen Beaver
		unlink($tmpname);
288 3e115dbf Stephen Beaver
289 5eb9f6ad NewEraCracker
		$output = implode("\n", $output);
290
		print("<pre>" . htmlspecialchars($output) . "</pre>");
291
292 3e115dbf Stephen Beaver
//		echo eval($_POST['txtPHPCommand']);
293 45d6ada5 Sjon Hortensius
		puts("</div>");
294 bd8eeef9 Stephen Beaver
?>
295 8fd9052f Colin Fleming
<script type="text/javascript">
296
//<![CDATA[
297 947141fd Phil Davis
	events.push(function() {
298 bd8eeef9 Stephen Beaver
		// Scroll to the bottom of the page to more easily see the results of a PHP exec command
299
		$("html, body").animate({ scrollTop: $(document).height() }, 1000);
300
	});
301 8fd9052f Colin Fleming
//]]>
302 bd8eeef9 Stephen Beaver
</script>
303
<?php
304 fbcf0037 Scott Ullrich
}
305 5b237745 Scott Ullrich
?>
306 45d6ada5 Sjon Hortensius
	<div class="panel panel-default responsive">
307 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Execute PHP Commands')?></h2></div>
308 45d6ada5 Sjon Hortensius
		<div class="panel-body">
309 7b91ab09 Jared Dillard
			<div class="content">
310
				<textarea id="txtPHPCommand" placeholder="Command" name="txtPHPCommand" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand'])?></textarea>
311
				<br />
312 59255714 NewEraCracker
				<button name="submit" type="submit" class="btn btn-warning btn-sm" value="EXECPHP" title="<?=gettext("Execute this PHP Code")?>">
313 df2aa83e Stephen Beaver
					<i class="fa fa-bolt"></i>
314 37676f4e jim-p
					<?=gettext("Execute")?>
315
				</button>
316 7b91ab09 Jared Dillard
				<?=gettext("Example"); ?>: <code>print("Hello World!");</code>
317
			</div>
318 45d6ada5 Sjon Hortensius
		</div>
319
	</div>
320 fa7855f3 Colin Fleming
</form>
321 74285e13 Scott Ullrich
322
<?php
323 45d6ada5 Sjon Hortensius
include("foot.inc");
324 74285e13 Scott Ullrich
325 947141fd Phil Davis
if ($_POST) {
326
}