C# Execute assembly sequence and other os function
2013-01-10 21:33
323 查看
1,execute Assembly sequence
using System; using System.Reflection; class Program { public delegate uint Ret1ArgDelegate(uint arg1); static uint PlaceHolder1(uint arg1) { return 0; } public static byte[] asmBytes = new byte[] { 0x89,0xD0, // MOV EAX,EDX 0xD1,0xC8, // ROR EAX,1 0xC3 // RET }; unsafe static void Main(string[] args) { fixed(byte* startAddress = &asmBytes[0]) // Take the address of our x86 code { // Get the FieldInfo for "_methodPtr" Type delType = typeof(Delegate); FieldInfo _methodPtr = delType.GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance); // Set our delegate to our x86 code Ret1ArgDelegate del = new Ret1ArgDelegate(PlaceHolder1); _methodPtr.SetValue(del, (IntPtr)startAddress); // Enjoy uint n = (uint)0xFFFFFFFC; n = del(n); Console.WriteLine("{0:x}", n); } } }
using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace DynamicX86 { class Program { const uint PAGE_EXECUTE_READWRITE = 0x40; const uint MEM_COMMIT = 0x1000; [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect); private delegate int IntReturner(); static void Main(string[] args) { List<byte> bodyBuilder = new List<byte>(); bodyBuilder.Add(0xb8); bodyBuilder.AddRange(BitConverter.GetBytes(42)); bodyBuilder.Add(0xc3); byte[] body = bodyBuilder.ToArray(); IntPtr buf = VirtualAlloc(IntPtr.Zero, (uint)body.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE); Marshal.Copy(body, 0, buf, body.Length); IntReturner ptr = (IntReturner)Marshal.GetDelegateForFunctionPointer(buf, typeof(IntReturner)); Console.WriteLine(ptr()); } } }
相关文章推荐
- Mixed mode assembly is built against version 'v1.1.4322' of the runtime and...问题——C# DirectXSound
- How to Determine if a file is a .Net assembly (in Delphi and C#)
- C# Issues and Solutions -- Method get_XXX from assembly YYYY does not have an implementation
- CLR Via C# 3rd 阅读摘要 -- Chapter 23 – Assembly Loading and Reflection
- [WASM] Create and Run a Native WebAssembly Function
- c# - PInvoke marshal and FunctionPtr
- The execute sequence when using the WHERE and GROUP BY Clauses Together
- C++'s Function pointer and C#'s delegate
- EFetch for Sequence and other Molecular Biology Databases (please see http://eutils.ncbi.nlm.nih.gov
- intermediate javascript: assign a function with its parameter to a variable and execute later
- Mixed mode assembly is built against version 'v1.1.4322' of the runtime and...问题——C# DirectXSound
- c# - FunctionPointer/IntPtr and delegate conver...
- Mixed mode assembly is built against version 'v1.1.4322' of the runtime and...问题——C# DirectXSound
- ASP.NET(c#) MesageBox JS Alert and Other Debug tools
- Mixed mode assembly is built against version 'v1.1.4322' of the runtime and...问题——C# DirectXSound
- C#反射之Assembly---CLR/c++可以通过反射调用c#类库
- VB.NET and C# Comparison
- C# Interview Questions and Answers
- Codeforces 743B-Chloe and the sequence
- Codeforces 894C:Marco and GCD Sequence(构造)