Dewera / Lunar

A lightweight native DLL mapping library that supports mapping directly from memory
MIT License
584 stars 102 forks source link

Freezes program while injecting -> waiting for URL URI response #36

Closed Baseult closed 2 years ago

Baseult commented 2 years ago

Hey I am using your Library and trying ot inject a dll doing it the following way:

var process = Process.GetProcessesByName(ProcessName)[0];
var dllFilePath = Path.GetTempPath() + DllName + ".dll";
var flags = MappingFlags.None;
var mapper = new LibraryMapper(process, dllFilePath, flags);
mapper.MapLibrary();

Somehow the injector freezes while injecting, and I traced it down to this function in SymbolHandler.cs:

using var response = await httpClient.GetAsync(new Uri($"https://msdl.microsoft.com/download/symbols/{pdbData.Path}/{pdbData.Guid:N}{pdbData.Age}/{pdbData.Path}"));

I kept waiting for a few minutes but nothing happened, I guess it waits for a response but can't reach the url?

The first 2 requests worked, then it just freezed the program.

Dewera commented 2 years ago

Interesting issue. I did a bit of a messy upgrade to the PDB downloading in the .Net 6 upgrade as Microsoft deprecated the WebClient class I was using prior and I just wanted to get it live. I'll take a look at rewriting the downloading logic with some safeguards and a progress bar to provide more clarity.

Baseult commented 2 years ago

alright, keep up the great work 👍

Dewera commented 2 years ago

Can you clear the files in %appdata%/Lunar/Dependencies and try again with the new update? If you get any exceptions while downloading please let me know what they are here

Baseult commented 2 years ago

Okay I cleared the Dependencies folder, using the new upload and still having the same issue. The console messages also do not appear in my output. At this point I don't know if it is an issue on my end or not.

I am supposed to build the lunar project and add the dll as reference to my C# .NET 6.0 project right?

Ill add a video below showing the error while i debug it, maybe it helps you to solve the issue. https://youtu.be/RJX7w4R1Sv0

Dewera commented 2 years ago

Yeah you're doing everything right from the looks of it. Extract the Uri it's building in the GetAsync method and send it through so I can take a look at what's going on (it should be a proper url.) I can't replicate this on my end so it's a bit hard to debug.

Baseult commented 2 years ago

Hey I changed the download and now this is working:

 // Download the PDB from the Microsoft symbol server

HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://msdl.microsoft.com/download/symbols/{pdbData.Path}/{pdbData.Guid:N}{pdbData.Age}/{pdbData.Path}");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
    using (FileStream strm = File.Create(pdbFilePath))
    using (StreamWriter sw = new StreamWriter(strm))
        sw.Write(reader.ReadToEnd());
}

return pdbFilePath;

Somehow I get another 2 errors now:

"System can not find the specified file" at
return GetModule("ntdll.dll").Address + _symbolHandler.GetSymbol(symbolName).RelativeAddress;
in ProcessContext.cs

because of the check in SymbolHandler.cs:
if (symbolTableAddress == 0)
    {
        throw new Win32Exception();
    }

sx1 sx3

and

"The key already existed in the dictionary" at
_moduleCache.TryAdd(moduleName, new Module(moduleAddress, new PeImage(File.ReadAllBytes(moduleFilePath))));
in ProcessContext.cs
at System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.System.Collections.Generic.IDictionary<TKey, TValue>.Add(TKey, TValue)

sx2

Just in case the download code wasn't working correctly, I downloaded the pdb manually and placed it inside the Dependencies folder -> Same result.

Dewera commented 2 years ago

Is this issue still relevant? I still can't replicate any of these issues (and pipeline doesn't seem to have issue either.)