是否可以从C#中执行x86汇编序列?

是否可以从C#中执行x86汇编序列?,第1张

是否可以从C#中执行x86汇编序列

为了反驳Brian的主张,从leppie的答案链接中重写了代码:

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());        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5150335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存