question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add dll to import table

See original GitHub issue
  Can use AsmResolver to inject a dll function into the import table. For example, a simple 64bit dll:
code
#include "pch.h"
extern "C" __declspec(dllexport) void puts()
{
    MessageBoxA(0, "hi", "hello", 0);
    return;
}

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        puts();
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
I want to add the export function "puts" to the IAT of notepad.

Dll1.zip

1

2

Can you give some hints or piece of code

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Washi1337commented, Dec 5, 2021

Like I mentioned in the previous post, make sure you add both the import lookup as well as the import address table to your PE, and that you update the data directories accordingly.

(Incomplete) example:
// Read raw PE file. 
var file = PEFile.FromFile("notepad.exe");
var image = PEImage.FromFile(file);

// Create new import
var module = new ImportedModule("Dll.dll");
module.Symbols.Add(new ImportedSymbol(0, "puts"));
image.Imports.Add(module);

// Reconstruct import dirs
var buffer = new ImportDirectoryBuffer(file.OptionalHeader.Magic == OptionalHeaderMagic.Pe32);
foreach (var m in image.Imports)
    buffer.AddModule(m);

// Build up new section
var sectionBuilder = new SegmentBuilder();
sectionBuilder.Add(buffer.ImportAddressDirectory);
sectionBuilder.Add(buffer, 4);

var section = new PESection(".asmres",
    SectionFlags.ContentInitializedData | SectionFlags.MemoryRead | SectionFlags.MemoryWrite
    | SectionFlags.MemoryExecute);
 
section.Contents = sectionBuilder;

// Add it.
file.Sections.Add(section);

// Update data dirs.
file.AlignSections();
file.OptionalHeader.DataDirectories[(int) DataDirectoryIndex.ImportDirectory]
    = new DataDirectory(buffer.Rva, buffer.GetPhysicalSize());
file.OptionalHeader.DataDirectories[(int) DataDirectoryIndex.IatDirectory]
    = new DataDirectory(buffer.ImportAddressDirectory.Rva, buffer.ImportAddressDirectory.GetPhysicalSize());

// Save
file.Write("notepad2.exe");

https://asmresolver.readthedocs.io/en/latest/peimage/pe-building.html

0reactions
laommscommented, Dec 10, 2021

I use a new dll test the code in other pe, It doesn’t work.But it doesn’t matter, you can close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add an entry to the import table of PE
My goal is to add a new imported function from an external DLL to my EXE, and then with ollydbg insert new code...
Read more >
Add a hardcoded DLL dependency to any EXE
As mentioned in the previous post, a DLL entry in the IAT table needs to import at least one function. We will use...
Read more >
Add imports to DLL import table
I have a 64-bit windows DLL file. I want to have it import some functions (from a DLL that its not already importing)....
Read more >
Understanding the Import Address Table
It should be able to be used similar to iidKing in this respect to add new dlls to the import table. Note this...
Read more >
PE Import Table hijacking as a way of achieving persistence
We could as well ADD an import table entry, but this is a bit more difficult, introduces more changes into the target DLL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found