-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continuing work getting various import classes working.
- Loading branch information
Lloyd Kinsella
committed
Aug 6, 2018
1 parent
c42b900
commit 62bfaa3
Showing
20 changed files
with
620 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/Workshell.PE/Content/Imports/DelayedImportHintNameTable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Workshell.PE.Extensions; | ||
|
||
namespace Workshell.PE.Content | ||
{ | ||
public sealed class DelayedImportHintNameTable : ImportHintNameTableBase<DelayedImportHintNameEntry> | ||
{ | ||
internal DelayedImportHintNameTable(PortableExecutableImage image, DataDirectory directory, Location location, Tuple<ulong, uint, ushort, string, bool>[] entries) : base(image, directory, location, entries, true) | ||
{ | ||
} | ||
|
||
#region Static Methods | ||
|
||
public static async Task<DelayedImportHintNameTable> GetAsync(PortableExecutableImage image, DelayedImportDirectory directory = null) | ||
{ | ||
if (directory == null) | ||
directory = await DelayedImportDirectory.LoadAsync(image).ConfigureAwait(false); | ||
|
||
var entries = new Dictionary<uint, Tuple<ulong, uint, ushort, string, bool>>(); | ||
var ilt = await DelayedImportAddressTables.GetLookupTableAsync(image, directory).ConfigureAwait(false); | ||
var calc = image.GetCalculator(); | ||
var stream = image.GetStream(); | ||
|
||
foreach (var table in ilt) | ||
{ | ||
foreach (var entry in table) | ||
{ | ||
if (entry.Address == 0) | ||
continue; | ||
|
||
if (entries.ContainsKey(entry.Address)) | ||
continue; | ||
|
||
if (!entry.IsOrdinal) | ||
{ | ||
var offset = calc.RVAToOffset(entry.Address); | ||
var size = 0u; | ||
var isPadded = false; | ||
ushort hint = 0; | ||
var name = new StringBuilder(256); | ||
|
||
stream.Seek(offset.ToInt64(), SeekOrigin.Begin); | ||
|
||
hint = await stream.ReadUInt16Async().ConfigureAwait(false); | ||
size += sizeof(ushort); | ||
|
||
while (true) | ||
{ | ||
var b = await stream.ReadByteAsync().ConfigureAwait(false); | ||
|
||
size++; | ||
|
||
if (b <= 0) | ||
break; | ||
|
||
name.Append((char)b); | ||
} | ||
|
||
if (size % 2 != 0) | ||
{ | ||
isPadded = true; | ||
size++; | ||
} | ||
|
||
var tuple = new Tuple<ulong, uint, ushort, string, bool>(offset, size, hint, name.ToString(), isPadded); | ||
|
||
entries.Add(entry.Address, tuple); | ||
} | ||
} | ||
} | ||
|
||
Location location; | ||
|
||
if (entries.Count > 0) | ||
{ | ||
var firstEntry = entries.Values.MinBy(tuple => tuple.Item1); | ||
var lastEntry = entries.Values.MaxBy(tuple => tuple.Item1); | ||
var tableOffset = firstEntry.Item1; | ||
var tableSize = ((lastEntry.Item1 + lastEntry.Item2) - tableOffset).ToUInt32(); | ||
var tableRVA = calc.OffsetToRVA(tableOffset); | ||
var tableVA = image.NTHeaders.OptionalHeader.ImageBase + tableRVA; | ||
var tableSection = calc.RVAToSection(tableRVA); | ||
|
||
location = new Location(tableOffset, tableRVA, tableVA, tableSize, tableSize, tableSection); | ||
} | ||
else | ||
{ | ||
location = new Location(0, 0, 0, 0, 0, null); | ||
} | ||
|
||
var result = new DelayedImportHintNameTable(image, directory.Directory, location, entries.Values.ToArray()); | ||
|
||
return result; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Workshell.PE.Content | ||
{ | ||
public sealed class DelayedImportLibrary : ImportLibraryBase | ||
{ | ||
internal DelayedImportLibrary(ImportLibraryFunction[] functions, string name) : base(functions, name, true) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Workshell.PE.Extensions; | ||
|
||
namespace Workshell.PE.Content | ||
{ | ||
public sealed class DelayedImports : ImportsBase<DelayedImportLibrary> | ||
{ | ||
internal DelayedImports(DelayedImportLibrary[] libraries) : base(libraries) | ||
{ | ||
} | ||
|
||
#region Static Methods | ||
|
||
public static async Task<DelayedImports> GetAsync(PortableExecutableImage image) | ||
{ | ||
var directory = await DelayedImportDirectory.LoadAsync(image).ConfigureAwait(false); | ||
|
||
if (directory == null) | ||
return null; | ||
|
||
var ilt = await DelayedImportAddressTables.GetLookupTableAsync(image, directory).ConfigureAwait(false); | ||
|
||
if (ilt == null) | ||
return null; | ||
|
||
var hnt = await DelayedImportHintNameTable.GetAsync(image, directory).ConfigureAwait(false); | ||
|
||
if (hnt == null) | ||
return null; | ||
|
||
return await GetAsync(image, ilt, hnt).ConfigureAwait(false); | ||
} | ||
|
||
public static async Task<DelayedImports> GetAsync(PortableExecutableImage image, DelayedImportAddressTables ilt, DelayedImportHintNameTable hnt) | ||
{ | ||
var libraries = new List<DelayedImportLibrary>(); | ||
var calc = image.GetCalculator(); | ||
var stream = image.GetStream(); | ||
|
||
foreach (var table in ilt) | ||
{ | ||
var builder = new StringBuilder(256); | ||
var offset = calc.RVAToOffset(table.DirectoryEntry.Name); | ||
|
||
stream.Seek(offset.ToInt64(), SeekOrigin.Begin); | ||
|
||
while (true) | ||
{ | ||
var b = await stream.ReadByteAsync().ConfigureAwait(false); | ||
|
||
if (b <= 0) | ||
break; | ||
|
||
builder.Append((char)b); | ||
} | ||
|
||
var name = builder.ToString(); | ||
var functions = new List<ImportLibraryFunction>(table.Count); | ||
|
||
foreach (var entry in table) | ||
{ | ||
ImportLibraryFunction function = null; | ||
|
||
if (entry.IsOrdinal) | ||
{ | ||
function = new ImportLibraryOrdinalFunction(entry, entry.Ordinal); | ||
} | ||
else | ||
{ | ||
var hintEntry = hnt.FirstOrDefault(e => e.Location.RelativeVirtualAddress == entry.Address); | ||
|
||
if (hintEntry != null) | ||
function = new ImportLibraryNamedFunction(entry, hintEntry); | ||
} | ||
|
||
if (function != null) | ||
functions.Add(function); | ||
} | ||
|
||
var library = new DelayedImportLibrary(functions.ToArray(), name); | ||
|
||
libraries.Add(library); | ||
} | ||
|
||
var imports = new DelayedImports(libraries.ToArray()); | ||
|
||
return imports; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.