using Microsoft.AspNetCore.Mvc; using SharpPdb.Native; using System.Net; using System.Net.Http.Headers; using System.Text; using System.IO; using Microsoft.AspNetCore.StaticFiles; using System.Runtime.InteropServices; using Zodiacon.DebugHelp; using Microsoft.PdbDownloader.Logic.Pdb; using Microsoft.PdbDownloader.Logic.Pe; namespace PdbServer.Controllers { [ApiController] [Route("[controller]")] public class Symbols : ControllerBase { [DllImport("Symbol Parser.dll",CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int EnumFunction(IntPtr pdbpath,IntPtr fname); //const string pdburl = "http://msdl.microsoft.com/download/"; const string pdburl = "http://msdl.blackint3.com:88/download/"; const string selfdocpath = "D:\\Web\\Symbols"; private readonly ILogger _logger; public Symbols(ILogger logger) { _logger = logger; } [HttpGet] [Route("QueryAllModuleSymbol")] public string QueryAllModuleSymbol(string ModuleName, string Guid, string Agent) { StringBuilder sb = new StringBuilder(); string filename = $"{selfdocpath}\\{ModuleName}_{Guid}_{Agent}.pdb"; if (!System.IO.File.Exists(filename)) { byte[] data = Helpers.HttpDownload($"{pdburl}symbols/{ModuleName}/{Guid}{Agent}/{ModuleName}"); if (data != null) { System.IO.File.WriteAllBytes(filename, data); } } if (System.IO.File.Exists(filename)) { using var reader = new PdbFileReader(filename); foreach (var v in reader.Functions) { sb.Append(v.Offset); sb.Append('#'); sb.Append(v.Name); sb.Append('|'); } } if (sb.Length > 0) sb.Length -= 1; return sb.ToString(); } [HttpGet] [Route("SymbolCheck")] public int SymbolCheck(string ModuleName, string Guid, string Agent) { string filename = $"{selfdocpath}\\{ModuleName}_{Guid}_{Agent}.pdb"; if (!System.IO.File.Exists(filename)) { if(System.IO.File.Exists($"{selfdocpath}\\{Guid}_{Agent}.no")) { return 0; } byte[] data = Helpers.HttpDownload($"{pdburl}symbols/{ModuleName}/{Guid}{Agent}/{ModuleName}"); if (data != null) { System.IO.File.WriteAllBytes(filename, data); } else { System.IO.File.WriteAllBytes($"{selfdocpath}\\{Guid}_{Agent}.no", new byte[0]); } } if (System.IO.File.Exists(filename)) { return 1; } return 0; } public static byte[] Compress(byte[] rawData) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.IO.Compression.GZipStream compressedzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true); compressedzipStream.Write(rawData, 0, rawData.Length); compressedzipStream.Close(); return ms.ToArray(); } [HttpGet] [Route("SymbolDownLoad")] public string SymbolDownLoad(string ModuleName, string Guid, string Agent) { StringBuilder sb = new StringBuilder(); string filename = $"{selfdocpath}\\{ModuleName}_{Guid}_{Agent}.pdb"; if (!System.IO.File.Exists(filename)) { if (System.IO.File.Exists($"{selfdocpath}\\{Guid}_{Agent}.no")) { return ""; } byte[] data = Helpers.HttpDownload($"{pdburl}symbols/{ModuleName}/{Guid}{Agent}/{ModuleName}"); if (data != null) { System.IO.File.WriteAllBytes(filename, data); } } if (System.IO.File.Exists(filename)) { byte[] filebuffer = System.IO.File.ReadAllBytes(filename); var bytes= Compress(filebuffer); for(int i=0;i