2016-06-11 76 views
2

最近我發現架構師經常使用的.net程序錯誤地實現了一個函數。所以我成功地使用ILSpy和Reflexil 以修改二進制文件的靜態方式修補了。但是,當新的次要版本發佈時,您需要修補它並再次移除StrongNameCheck,這是令人煩惱的。 (順便說一句,作者認爲這是一個功能,而不是一個bug)是否有可能在運行中修補dotnet函數

希望,程序完全支持程序集作爲插件。而我的目標是公共類非公共靜態成員函數可以直接由插件調用。有沒有辦法在運行中修補功能

我通常在非託管C++中使用一些APIHook技巧,但dotnet實際上是另一回事。在這種情況下,我希望在我的程序集卸載後修改仍然有效(所以更類似於補丁,而不是鉤子)。

+0

我從來沒有用過這個,但... ... http://www.codeproject.com/Articles/463508/NET-CLR-Injection-Modify-IL-Code-during-Run-time – jdphenix

+0

看看[這個] (http://www.outercurve.org/blog/2014/03/25/Strongnaming-I-Do-Not-Think-It-Means-What-You-Thin)的文章。 –

回答

1

不要猴子補丁代碼。將功能添加到您的代碼庫並調用該函數。或者編寫一個包裝底層程序集的適配器類,這很簡單。

如果代碼的作者認爲它不是一個bug,那麼它可能在那裏,因爲你不明白的原因,可能是任何錯誤修復的一部分。

+0

加1用於包裝的適配器類。總的來說,這整個職位是正確的錢! – series0ne

+0

謝謝。這實際上是在特定情況下的性能下降。該補丁涉及某些算法的替換。但我同意修補代碼可能會導致未知的錯誤。 –

2

是的,你可以使用代碼注入。但是你需要知道一些MSIL。還有一個叫做Mono.Cecil的圖書館。

這裏是一個示例代碼

 Console.WriteLine("> INJECTING INTO 12345.EXE..." + Environment.NewLine); 
     AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(@"C:\dummy.exe"); 
     var writeLineMethod = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }); 
     var writeLineRef = asm.MainModule.Import(writeLineMethod); 
     var pStartMethod = typeof(Process).GetMethod("Start", new Type[] { typeof(string) }); 
     var pStartRef = asm.MainModule.Import(pStartMethod); 

     foreach (var typeDef in asm.MainModule.Types) 
     { 
      foreach (var method in typeDef.Methods) 
      { 
       //Let's push a string using the Ldstr Opcode to the stack 
       method.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldstr, "INJECTED!")); 

       //We add the call to the Console.WriteLine() method. It will read from the stack 
       method.Body.Instructions.Insert(1, Instruction.Create(OpCodes.Call, writeLineRef)); 

       //We push the path of the executable you want to run to the stack 
       method.Body.Instructions.Insert(2, Instruction.Create(OpCodes.Ldstr, @"calc.exe")); 

       //Adding the call to the Process.Start() method, It will read from the stack 
       method.Body.Instructions.Insert(3, Instruction.Create(OpCodes.Call, pStartRef)); 

       //Removing the value from stack with pop 
       method.Body.Instructions.Insert(4, Instruction.Create(OpCodes.Pop)); 
      } 
     } 
     asm.Write("12345.exe"); //Now we just save the new assembly 
+1

謝謝。你提出了一種自動修改二進制文件的方式,我認爲它是可以的(因爲我對Mono.Cecil瞭解不多)。 –

相關問題