@@ -19,6 +19,7 @@ class Script
1919 public static AssemblyWriter assemblyWriter ;
2020 public static Assembly assembly ;
2121 public static MethodDef currentMethod ;
22+ public static StackFrame [ ] mainFrames ;
2223 public static List < TypeDef > junkType = new List < TypeDef > ( ) ;
2324
2425 [ STAThread ]
@@ -59,7 +60,9 @@ static public void Main(string[] args)
5960 assembly = Assembly . LoadFrom ( path ) ;
6061 Console . WriteLine ( "[+] Wait..." ) ;
6162
62- Memory . Hook ( typeof ( StackFrame ) . GetMethod ( "SetMethodBase" , BindingFlags . Instance | BindingFlags . NonPublic ) , typeof ( Script ) . GetMethod ( "Hook2" , BindingFlags . Instance | BindingFlags . Public ) ) ;
63+ mainFrames = new StackTrace ( ) . GetFrames ( ) ;
64+
65+ Memory . Hook ( typeof ( StackTrace ) . GetMethod ( "CaptureStackTrace" , BindingFlags . Instance | BindingFlags . NonPublic ) , typeof ( Script ) . GetMethod ( "Hook3" , BindingFlags . Instance | BindingFlags . Public ) ) ;
6366
6467 var types = assemblyWriter . moduleDef . GetTypes ( ) ;
6568 var list = ( types as IList < TypeDef > ) ?? types . ToList < TypeDef > ( ) ;
@@ -82,7 +85,6 @@ static public void Main(string[] args)
8285
8386 if ( method == null )
8487 Console . WriteLine ( "[!] Couldn't find InvokeMethod" ) ;
85-
8688 InvokeDelegates ( list , method , fieldValue ) ;
8789
8890 new StringDecrypter ( assembly ) . ReplaceStrings ( list ) ;
@@ -189,6 +191,29 @@ public void Hook2(MethodBase mb)
189191 else
190192 typeof ( StackFrame ) . GetField ( "method" , BindingFlags . Instance | BindingFlags . NonPublic ) . SetValue ( this , mb ) ;
191193 }
194+
195+ public void Hook3 ( int iSkip , bool fNeedFileInfo , Thread targetThread , Exception e )
196+ {
197+ ///////////////////////////////////////////////////////////////////////////////////////////////
198+ // FrameCount | 2 | int //
199+ // METHODS_TO_SKIP | 0 | int //
200+ // frames | {System.Diagnostics.StackFrame[6]} | System.Diagnostics.StackFrame[] //
201+ // m_iMethodsToSkip | 4 | int //
202+ // m_iNumOfFrames | 2 | int //
203+ ///////////////////////////////////////////////////////////////////////////////////////////////
204+ typeof ( StackFrame ) . GetField ( "method" , BindingFlags . Instance | BindingFlags . NonPublic ) . SetValue ( mainFrames . Last ( ) , assembly . Modules . FirstOrDefault < Module > ( ) . ResolveMethod ( currentMethod . MDToken . ToInt32 ( ) ) ) ;
205+
206+ var mainFramesList = mainFrames . ToList ( ) ;
207+
208+ for ( int i = mainFramesList . Count ( ) ; i < 6 ; i ++ )
209+ mainFramesList . Add ( mainFrames . Last ( ) ) ;
210+ for ( int i = mainFramesList . Count ( ) ; i > 6 ; i -- )
211+ mainFramesList . Remove ( mainFramesList . First ( ) ) ;
212+
213+ typeof ( StackTrace ) . GetField ( "frames" , BindingFlags . Instance | BindingFlags . NonPublic ) . SetValue ( this , mainFramesList . ToArray ( ) ) ;
214+ typeof ( StackTrace ) . GetField ( "m_iMethodsToSkip" , BindingFlags . Instance | BindingFlags . NonPublic ) . SetValue ( this , 4 ) ;
215+ typeof ( StackTrace ) . GetField ( "m_iNumOfFrames" , BindingFlags . Instance | BindingFlags . NonPublic ) . SetValue ( this , 2 ) ;
216+ }
192217}
193218
194219internal class StringDecrypter
0 commit comments