Skip to content

Commit 850ecc0

Browse files
committed
Added module path to DLL exports.
1 parent bf5390c commit 850ecc0

1 file changed

Lines changed: 41 additions & 25 deletions

File tree

NtApiDotNet/Win32/SafeLoadLibraryHandle.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ IMAGE_NT_OPTIONAL_HDR_MAGIC IImageOptionalHeader.GetMagic()
265265
{
266266
return Magic;
267267
}
268-
269-
270268
}
271269

272270
[StructLayout(LayoutKind.Sequential)]
@@ -424,13 +422,18 @@ public class DllExport
424422
/// Name of the forwarder, if used.
425423
/// </summary>
426424
public string Forwarder { get; }
425+
/// <summary>
426+
/// Get the module this was exported from.
427+
/// </summary>
428+
public string ModulePath { get; }
427429

428-
internal DllExport(string name, int ordinal, long address, string forwarder)
430+
internal DllExport(string name, int ordinal, long address, string forwarder, string module_path)
429431
{
430432
Name = name ?? $"#{ordinal}";
431433
Ordinal = ordinal;
432434
Address = address;
433435
Forwarder = forwarder;
436+
ModulePath = module_path;
434437
}
435438

436439
/// <summary>
@@ -457,20 +460,29 @@ public class DllImport
457460
/// </summary>
458461
public IEnumerable<DllImportFunction> Functions { get; }
459462
/// <summary>
463+
/// List of names imported.
464+
/// </summary>
465+
public IEnumerable<string> Names => Functions.Select(f => f.Name);
466+
/// <summary>
460467
/// Could of functions
461468
/// </summary>
462469
public int FunctionCount { get; }
463470
/// <summary>
464471
/// True of the imports are delay loaded.
465472
/// </summary>
466473
public bool DelayLoaded { get; }
474+
/// <summary>
475+
/// The path to the executable this import came from.
476+
/// </summary>
477+
public string ModulePath { get; }
467478

468-
internal DllImport(string dll_name, bool delay_loaded, List<DllImportFunction> funcs)
479+
internal DllImport(string dll_name, bool delay_loaded, List<DllImportFunction> funcs, string module_path)
469480
{
470481
DllName = dll_name;
471482
Functions = funcs.AsReadOnly();
472483
FunctionCount = funcs.Count;
473484
DelayLoaded = delay_loaded;
485+
ModulePath = module_path;
474486
}
475487

476488
/// <summary>
@@ -608,6 +620,7 @@ internal SafeLoadLibraryHandle(IntPtr handle, bool owns_handle)
608620

609621
internal SafeLoadLibraryHandle() : base(true)
610622
{
623+
611624
}
612625
#endregion
613626

@@ -795,29 +808,12 @@ public IEnumerable<ImageSection> GetImageSections()
795808
/// <summary>
796809
/// Get path to loaded module.
797810
/// </summary>
798-
public string FullPath
799-
{
800-
get
801-
{
802-
StringBuilder builder = new StringBuilder(260);
803-
if (Win32NativeMethods.GetModuleFileName(handle, builder, builder.Capacity) == 0)
804-
{
805-
throw new SafeWin32Exception();
806-
}
807-
return builder.ToString();
808-
}
809-
}
811+
public string FullPath => GetFullPath();
810812

811813
/// <summary>
812814
/// Get the module name.
813815
/// </summary>
814-
public string Name
815-
{
816-
get
817-
{
818-
return Path.GetFileName(FullPath);
819-
}
820-
}
816+
public string Name => Path.GetFileName(FullPath);
821817

822818
/// <summary>
823819
/// Whether this library is mapped as an image.
@@ -955,6 +951,8 @@ public static NtResult<SafeLoadLibraryHandle> LoadLibrary(string name, LoadLibra
955951
}
956952
return Win32Utils.GetLastWin32Error().CreateResultFromDosError<SafeLoadLibraryHandle>(false);
957953
}
954+
if (ret.FullPath == string.Empty)
955+
ret._full_path = Path.GetFullPath(name);
958956
return ret.CreateResult();
959957
}
960958

@@ -1076,6 +1074,7 @@ public static void PinModule(IntPtr address)
10761074
private List<DllExport> _exports;
10771075
private List<DllImport> _imports;
10781076
private DllDebugData _debug_data;
1077+
private string _full_path;
10791078

10801079
private IntPtr RvaToVA(long rva)
10811080
{
@@ -1192,7 +1191,7 @@ private void ParseExports()
11921191
func_va = 0;
11931192
}
11941193
_exports.Add(new DllExport(ordinal_to_names.ContainsKey(i) ? ordinal_to_names[i] : null,
1195-
i + export_directory.Base, func_va, forwarder));
1194+
i + export_directory.Base, func_va, forwarder, FullPath));
11961195
}
11971196
}
11981197
catch
@@ -1250,7 +1249,7 @@ private DllImport ParseSingleImport(int name_rva, int lookup_rva, int iat_rva, b
12501249
funcs.Add(ReadImport(dll_name, lookup, iat_func));
12511250
}
12521251

1253-
return new DllImport(dll_name, delay_loaded, funcs);
1252+
return new DllImport(dll_name, delay_loaded, funcs, FullPath);
12541253
}
12551254

12561255
private void ParseNormalImports(bool is_64bit)
@@ -1426,6 +1425,23 @@ private void SetupValues()
14261425
_is_64bit = optional_header.GetMagic() == IMAGE_NT_OPTIONAL_HDR_MAGIC.HDR64;
14271426
_dll_characteristics = optional_header.GetDllCharacteristics();
14281427
}
1428+
1429+
private string GetFullPath()
1430+
{
1431+
if (_full_path == null)
1432+
{
1433+
StringBuilder builder = new StringBuilder(260);
1434+
if (Win32NativeMethods.GetModuleFileName(handle, builder, builder.Capacity) == 0)
1435+
{
1436+
_full_path = string.Empty;
1437+
}
1438+
else
1439+
{
1440+
_full_path = builder.ToString();
1441+
}
1442+
}
1443+
return _full_path;
1444+
}
14291445
#endregion
14301446

14311447
#region Static Properties

0 commit comments

Comments
 (0)