81 lines
2.1 KiB
C#
81 lines
2.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using PureCrack.Util;
|
|
using PureCrack.Wire;
|
|
|
|
namespace PureCrack.Relay;
|
|
|
|
public static class CaptureWriter
|
|
{
|
|
public static string Dump(string path, byte[] rawBody, byte[]? plaintext)
|
|
{
|
|
string text = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
|
string text2 = SanitizePathForFilename(path);
|
|
string text3 = Path.Combine(Workspace.CapturesDir, text + "_" + text2);
|
|
File.WriteAllBytes(text3 + ".raw.bin", rawBody);
|
|
if (plaintext != null)
|
|
{
|
|
File.WriteAllBytes(text3 + ".pt.bin", plaintext);
|
|
File.WriteAllText(text3 + ".pt.txt", BuildPrettyDump(path, plaintext), Encoding.UTF8);
|
|
}
|
|
return text3;
|
|
}
|
|
|
|
private static string BuildPrettyDump(string path, byte[] pt)
|
|
{
|
|
StringBuilder stringBuilder = new StringBuilder(pt.Length * 4);
|
|
stringBuilder.Append("URL: ").Append(path).Append('\n');
|
|
stringBuilder.Append("Decrypted ").Append(pt.Length).Append(" bytes\n\nHEX:\n");
|
|
for (int i = 0; i < pt.Length; i += 32)
|
|
{
|
|
int num = Math.Min(32, pt.Length - i);
|
|
stringBuilder.Append(i.ToString("x4")).Append(" ");
|
|
for (int j = 0; j < num; j++)
|
|
{
|
|
stringBuilder.Append(pt[i + j].ToString("x2")).Append(' ');
|
|
}
|
|
for (int k = num; k < 32; k++)
|
|
{
|
|
stringBuilder.Append(" ");
|
|
}
|
|
stringBuilder.Append(" |");
|
|
for (int l = 0; l < num; l++)
|
|
{
|
|
byte b = pt[i + l];
|
|
stringBuilder.Append((char)((b >= 32 && b < 127) ? b : 46));
|
|
}
|
|
stringBuilder.Append("|\n");
|
|
}
|
|
stringBuilder.Append("\nPROTOBUF TREE:\n");
|
|
try
|
|
{
|
|
stringBuilder.Append(ProtoNet.Dump(pt));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
stringBuilder.Append("<parse err: ").Append(ex.Message).Append(">\n");
|
|
}
|
|
return stringBuilder.ToString();
|
|
}
|
|
|
|
private static string SanitizePathForFilename(string path)
|
|
{
|
|
string text = path.Replace('/', '_').Trim(new char[1] { '_' });
|
|
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
|
|
foreach (char oldChar in invalidFileNameChars)
|
|
{
|
|
text = text.Replace(oldChar, '_');
|
|
}
|
|
if (text.Length == 0)
|
|
{
|
|
text = "root";
|
|
}
|
|
if (text.Length > 80)
|
|
{
|
|
text = text.Substring(0, 80);
|
|
}
|
|
return text;
|
|
}
|
|
}
|