Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

How to inject code using DnLib?

Hi, I have a problem.

I am injecting code with my following method.

Method :

Code
Module Anti_Debug
        Sub Execute2(ByVal GetAssembly As ModuleDef)
            Dim typeModule = ModuleDefMD.Load(GetType(Testting).Module)
            Dim cctor = GetAssembly.GlobalType.FindOrCreateStaticConstructor()
            Dim typeDef = typeModule.ResolveTypeDef(MDToken.ToRID(GetType(Testting).MetadataToken))
            Dim members = InjectHelper.Inject(typeDef, GetAssembly.GlobalType, GetAssembly)
            Dim init = CType(members.Single(Function(method) method.Name = "InitializeTestingDebug"), MethodDef)
            cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init))

            For Each MDDef In GetAssembly.GlobalType.Methods
                If Not MDDef.Name = ".ctor" Then Continue For
                GetAssembly.GlobalType.Remove(MDDef)
                Exit For
            Next
        End Sub
    End Module

Class to Inject:

Code
Module Testting

    Private Sub InitializeTestingDebug()
        MsgBox("Test Fun.")
    End Sub

End Module

This works fine.




The problem I have is when the Class to Inject has declarations / Properties outside the methods. if it has any declarations outside the Sub InitializeTestingDebug (), Crahstea my generated app.

Example :

Code
Module Testting

    Private _LogResult As String = String.Empty
    Public ReadOnly Property LogResult As String
        Get
            Return _LogResult
        End Get
    End Property

    Private Sub InitializeTestingDebug()
        MsgBox("Test Fun.")
    End Sub

End Module




It also doesn't work correctly when I call some function from the method Sub InitializeTestingDebug ()

Example :

Code
Module Testting

    Private Sub InitializeTestingDebug()
        MsgBox(WelcomeMessage)
    End Sub

    Private Function WelcomeMessage() As String
        Return "Test Fun."
    End Function

End Module





Someone help me fix this problem, please.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by arsiumJun 28, 2022 09:52 PM 3 293 Jun 28, 2022 09:52 PM
Last Post: StarZ
[C#] by trucoJan 3, 2022 04:07 AM 2 258 Jan 3, 2022 04:07 AM
Last Post: truco
[C#] by JoshuaSmithSep 21, 2021 11:50 AM 1 221 Sep 21, 2021 11:50 AM
Last Post: Donkey Death Jam