惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
A
About on SuperTechFans
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
P
Proofpoint News Feed
D
Docker
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
I
InfoQ
Recorded Future
Recorded Future
爱范儿
爱范儿
Last Week in AI
Last Week in AI
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
The Last Watchdog
The Last Watchdog
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
Schneier on Security
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
Martin Fowler
Martin Fowler
雷峰网
雷峰网
Latest news
Latest news
Scott Helme
Scott Helme
T
Tenable Blog
Vercel News
Vercel News
宝玉的分享
宝玉的分享
PCI Perspectives
PCI Perspectives
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
Attack and Defense Labs
Attack and Defense Labs
Spread Privacy
Spread Privacy
量子位
H
Heimdal Security Blog

博客园 - 曲滨*銘龘鶽

IE8 隐藏命令行参数 学习 ASP.NET mvc 第一天、也可能是最后一天 Visual Studio 2008 开启被遗忘的 “同步类视图”功能、附带一个MSDN 的 bug 实战 IE8 开发人员工具 windows 桌面开发 - 子类化控件(不用任何WinAPI),演示拦截Button的WM_LBUTTONDBLCLK 在真实项目中使用第三方或开源代的代码,组件,中间件,框架的基本规则 你有?项目设计开发阶段,甲方经常的要求看程序,而经常在做【假界面】【假程序】的情况吗? Windows Live Writer 测试 文档共享:罗斯文2007 (Northwind 2007),数据库文件,中文版本、英文版、英文表结构中文数据版 本机 MSDN Sorry, no topics were found for the selected link 问题,及临时解决方案 2008-9-6 文档共享:罗斯文2007 (Northwind 2007),数据库,微软最新的 Access 2007 样列数据库分析(中文/英文) C# 操作 XML 数据库类型、Oracle XMLType IE7,ie8说爱你不容易,企业应用困局 如何:在 Winform 动态启动、控制台命令行? 如何:使 comboBox 输入状态变成 readonly 方式;TextBox 只读时的效果; 设计模式学习:Model View Presenter (MVP) OneDay “屏幕任务”小软件 TaskScreen_080408 Oracle 使用数据泵 expdp impdp 导入导出数据库“表空间”文件 适合编程开发用的"宋体"和"新宋体"
C# 直接执行、调用本机代码、汇编代码 shell Native Code
曲滨*銘龘鶽 · 2008-05-11 · via 博客园 - 曲滨*銘龘鶽

本文讲述如何在 .net C# 中
坠入,执行调用本机代码、汇编代码 、shell Native Code

谁说,.net 不能直接使用本机代码汇编;本文将讲述如何实现这一技术;
不多说了上代码:

刚才发布的那个版本修改时出了问题被我删除了又发了一次,抱歉了先

/*
    
    执行调用本机代码、汇编代码 shell Native Code

    解释
        本例中 IntPtr 其实是相当于 C 语言中的 (void *) 指向任何类型的指针,
            就是一个地址 32 位系统就是个 Int32,本例相当与一个函数指针
 
    核心技术流程
        变量:
            【本机代码字节数组】 byte[] codeBytes ; 一段加法本机代码
            【函数指针】 IntPtr handle ; 指向本机函数开始地址的变量
        流程:
            
        >> 给 【函数指针】划分非托管内存 ; 使用 Marshal.AllocHGlobal(codeBytes.Length) 
                划分大小等同于本机代码字节总数    因为函数本身还不存在于内存中所以先开辟一块内存;
                以便放置函数。
                
        >> 将 【本机代码字节数组】中的字节写入 【函数指针】 ;
                Marshal.Copy(codeBytes,0,handle,codeBytes.Length);
 
        
        >> 使用 Marshal.GetDelegateForFunctionPointer 【函数指针】 强制转换为托管委托 DelegateAdd;
                因为这时 handle 内的字节数组已经是内存中的一段本机方法的代码
                handle 的“起始地址”就相当于一个“本机函数的入口地址”
                所以可以成功转换为对应的委托
 
        >> 调用 委托 ;
 
        >> 释放本机句柄;Marshal.FreeHGlobal(this._handle);

修改记录
    2008-5-11 8:07 曲滨
        >> 基本实现预期功能
        [!] 明天进行优化
 
    2008-5-12 15:54 曲滨
        [E] 优化完成
        [N] 加入 NativeCodeHelper 类便于使用

*/
namespace NShellNativeCode
{
    
using System;
    
using System.Collections.Generic;
    
using System.Text;
    
using System.Runtime.InteropServices;
    
using System.IO;
    
using System.Diagnostics;
    
using System.Reflection;
    
    
delegate int AddProc(int p1, int p2);
    
class Program
    {
        
static void Main(string[] args)
        {
            
            
//一段加法函数本机代码;后面注释是给会 asm 看官看的
            
//笔者本身也不是太明白汇编,简单的 10行8行的还可以
            
            
byte[] codeBytes = { 
                  
0x8B0x440x240x08    // mov eax,[esp+08h]
                , 0x8B0x4C0x240x04    // mov ecx,[esp+04h]
                , 0x030xC1                // add    eax,ecx
                , 0xC3                        // ret
                };
                        
            
/*
            上面的字节数组,就是下面函数的本机代码;
            int add(int x,int y) {
                return x+y;
            } 
             
            
*/

            IntPtr handle 

= IntPtr.Zero;
            handle 
= Marshal.AllocHGlobal(codeBytes.Length);
            
try
            {

                Marshal.Copy(codeBytes, 

0, handle, codeBytes.Length);
                                
                AddProc add
                   
= Marshal.GetDelegateForFunctionPointer(handle, typeof(AddProc)) as AddProc;
                
                
int r = add(19761);

                Console.WriteLine(

"本机代码返回:{0}", r);

            }

finally
            {
                Marshal.FreeHGlobal(handle);
            }
             
            
//本演示内包含的已经封装好的 本机字节代码,转换委托通用类
            
//打开注释就可以用了;
            
            
/*
            using (NativeCodeHelper helper = new NativeCodeHelper(codeBytes)) 
            {
                AddProc add = helper.ToDelegate<AddProc>();
                Type t =  add.Method.DeclaringType;
                int r = add(1976,1);
                Console.WriteLine("本机代码返回:{0}",r);
            }
            
*/
                    
            
            
//Console.ReadLine();
        }    
             
        
    }
/*
    结束语
        已知问题
            1)在操作系统打开 DEP 保护的情况下,这类代码会不灵;
                我没有测试,有兴趣的可以试验一下,估计是不会好用的;
            
            2)如果要在 本机代码 中调用 Win API 函数,因为在不同系统不同版本中 
                Win API 的地址是不同的; 
                要有一些编写 shell code 能力于黑客技术关系密切这里不做详细描述
    
        本文技术的适用范围
            >> 遗留系统,C/C++ 的某些算法、尤其汇编形式的是如果懒的改成.net 可以直接吧二进制copy 
                出来直接调用、不过需要C/VC、反汇编、汇编有点了解要不没法Copy;
 
            >> 有些代码不想被反编译,给破解者增加些破解难度、郁闷有可能会改你代码的人
                实用性有多少看官自己感觉吧,因为技术这东西是相对的
                如果你的程序中到处都是这类代码是很难维护的,就是熟悉汇编的人
                这种东西多了也很郁闷的、本机代码远比汇编难看的多  

            >> 忽悠小朋友
                把我的代码直接copy倒你的项目里,一点都不改,要算int加法的时候都这么用
                如果有小朋友看见一定会感觉你很 Cool
        
        重要声明:
            这种本机代码方式如果应用倒真实项目中一定要项目负责人的同意的情况下,否则出现
        任何人事问题,或刑事问题与本文作者无关;
            如
                >> 在真实项目中使用本文技术,在代码中坠入逻辑炸弹者;
                >> 在真实项目中使用本文技术,拒不上缴本机字节代码对应的源代码者;
                >> 在真实项目或共享软件中,捆绑病毒代码者;

*//// <summary>
    
/// 用于将本机代码 byte 数组转换为 .net 委托
    
/// </summary>
    
/// <remarks>
    
/// 实现了 IDisposable 使用了非托管资源 使用时不要忘记释放 
    
/// </remarks>
    public class NativeCodeHelper:IDisposable
    {
        
        
private bool _disposed = false;
        
private byte[] _codeBytes = {};
        
private IntPtr _handle = IntPtr.Zero;
        
        
public NativeCodeHelper(byte[] codeBytes)
        {
             
this._codeBytes =  codeBytes; 
        }
        
        
/// <summary>
        
/// 把byte数字转换为本机类型的指针 主要处理 this._handle
        
/// </summary>
        private void CreateHandle()
        {
            
if (_handle == IntPtr.Zero)
            {
                _handle 
= Marshal.AllocHGlobal( this._codeBytes.Length);
                Marshal.Copy(_codeBytes, 
0, _handle, _codeBytes.Length);
            }        
        }
        
/// <summary>
        
/// 转换为指定的委托
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <returns></returns>
        public T ToDelegate<T>() where T:class
        {
            
this.CreateHandle();
            
            
//把指针转换为 委托方法
            T result = Marshal.GetDelegateForFunctionPointer(_handle, typeof(T)) as T;            
            
            
return result;

        }

#region IDisposable 成员
        
        
~NativeCodeHelper()
        {
            Dispose(
false);
        }
        
        
public void Dispose()
        {
            Dispose(
true);        
            GC.SuppressFinalize(
this);

        }

private void Dispose(bool disposing)
        {
            
if (disposing)
            {
                
//给调用者忘记 Dispose 释放的提示
                MethodBase mb = System.Reflection.MethodBase.GetCurrentMethod();
                Type t 
= mb.DeclaringType;
                
                Trace.WriteLine(
"not Dispose"
                , 
"" + t + "." + mb );
            }
            
            
            
if (!this._disposed)
            {
                
if (disposing)
                {
                    
//释放.net 需要 Dispose 的对象                    
                }

                Marshal.FreeHGlobal(

this._handle);
                _handle 
= IntPtr.Zero;
            }
            _disposed 
= true;
        }        
#endregion
    }
}

end cnblogs 2008-5-11
程序为命令行程序,
最终输出结果:
    本机代码返回:1977

本文代码下载