字符串混淆技术应用 设计一个字符串混淆程序 可混淆.NET程序集中的字符串

原文:字符串混淆技术应用 设计一个字符串混淆程序 可混淆.NET程序集中的字符串

关于字符串的研究,目前已经有两篇。

原理篇:字符串混淆技术在.NET程序保护中的应用及如何解密被混淆的字符串 

实践篇:字符串反混淆实战 Dotfuscator 4.9 字符串加密技术应对策略

今天来讲第三篇,如何应用上面所学内容,设计一个字符串混淆程序。

先设计一个控制台程序,它是将要被我混淆的程序集文件:

public static void Main()
{
        try
        {
            RunSnippet();
        }
        catch (Exception e)
        {
            string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
            Console.WriteLine(error);
        }
        finally
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
}
 

代码是Snippet Compiler 的标准模板,在控制台上打印一段字符串。这里,有二个字符串常量,是我需要对它进行加密的地方。

不能直接改源代码,而且要以程序的方式来操作程序集。要能修改.net程序集,现在能找到的方法是Mono.Cecil,最新的版本是0.9.5.0。

先设计混淆算法,是个很简单的Base64代码转换,这一步可以深入挖掘,做更复杂的混淆算法。

public static string StringDecode(string text1)
{
    return Encoding.UTF8.GetString(Convert.FromBase64String(text1));
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

要把这段代码转化为IL代码,用Mono.Cecil来注入到制定的程序集中,先来看看翻译成IL之后的代码

.method public hidebysig static string StringDecode(string) cil managed
{
    .maxstack 8
    L_0000: call class [mscorlib]System.Text.Encoding [mscorlib]System.Text.Encoding::get_UTF8()
    L_0005: ldarg.0
    L_0006: call uint8[] [mscorlib]System.Convert::FromBase64String(string)
    L_000b: callvirt instance string [mscorlib]System.Text.Encoding::GetString(uint8[])
    L_0010: ret
}

再对比Mono.Cecil的语法例子,把上面的IL代码,翻译成C#代码

MethodDefinition new_method = new MethodDefinition("StringDecode", attr, asm.MainModule.Import(typeof(string)));
ParameterDefinition para = new ParameterDefinition(asm.MainModule.Import(typeof(string)));
new_method.Parameters.Add(para);
tp.Methods.Add(new_method);

new_method.Body.MaxStackSize = 8;
MethodReference mr;
ILProcessor worker = new_method.Body.GetILProcessor ();

mr = asm.MainModule.Import(typeof(Encoding).GetMethod("get_UTF8"));
worker.Append(worker.Create(OpCodes.Call, mr));

worker.Append(worker.Create(OpCodes.Ldarg_0));

mr = asm.MainModule.Import(typeof(Convert).GetMethod("FromBase64String"));
worker.Append(worker.Create(OpCodes.Call, mr));

mr = asm.MainModule.Import(typeof(Encoding).GetMethod("GetString", new Type[] { typeof(Byte[]) }));
worker.Append(worker.Create(OpCodes.Callvirt, mr));
worker.Append(worker.Create(OpCodes.Ret));

 

再次,我样要搜索目标程序集中的所有字符串,把它转化成Base64的字符串编码,于是遍历IL指令,进行转化

List<Instruction>  actionInsert=new List<Instruction> ();
foreach (Instruction ins in entry_point.Body.Instructions)
{
       if (ins.OpCode.Name == "ldstr")
       {
                Console.WriteLine("Find target instruction, start modify..");
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes (Convert.ToString (ins.Operand));
                ins.Operand = Convert.ToBase64String (bytes);

                actionInsert.Add(ins);
      }
}
 
 

最后,我们把原来的指令替换成字符串混淆算法调用

for (int i = 0; i < actionInsert.Count; i++)
{
         mr = asm.MainModule.Import(new_method);
         worker = entry_point.Body.GetILProcessor();
         worker.InsertAfter(actionInsert[i], worker.Create(OpCodes.Call, mr));
}
 
 

最后保存程序集,用.net Reflector 载入程序集,如下图所示,字符串常量已经变成了方法调用:

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

这样,增加了代码反编译的难度,字符串的含义完全被替换成一堆无意义的字符串。

Mono.Cecil最新的版本中,API有变化,本篇程序代码中应用到的读取程序集和写入程序集

string path = @"C:\Users\Administrator\Desktop\CPP\Default.exe";
AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(path);
MethodDefinition entry_point = asm.EntryPoint;

path = @"C:\Users\Administrator\Desktop\CPP\DefaultSecury.exe";
asm.MainModule.Write(path);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

关于字符串混淆算法,下面列举几个我找到的混淆算法,加密强度会高一些:

static string stringEncrypt(string string_0)
{
    char[] chArray;
    char[] chArray1 = chArray = string_0.ToCharArray();
    while (true)
    {
        int num;
        int length = chArray1.Length;
        if (length <= 0)
        {
            break;
        }
        chArray1[num = length + -1] = (char) (chArray[num] - ‘?‘);
    }
    return string.Intern(new string(chArray));
}
 

下面是Dotfuscator的混淆算法,还加了盐,强度提升不少。

static string GetString(string source, int salt)
 {
     int index = 0;
     char[] data = source.ToCharArray();
     salt += 0xe74d6d7; // This const data generated by dotfuscator
     while (index < data.Length)
     {
         char key = data[index];
         byte low = (byte)((key & ‘\x00ff‘) ^ salt++);
         byte high = (byte)((key >> 8) ^ salt++);
         data[index] = (char)((low << 8 | high));
         index++;
     }
     return string.Intern(new string(data));
 }
 

经过混淆后的字符串,完全看不出原文的含义。比如下面的代码片段,都有些怀疑它使用的字符集,有点像中东国家的语言

 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

再来看一个代码中有中文的例子,这是一段用户登陆代码,它加密后的字符看起来更不可理解。

如果要给字符串混淆加盐,只需要简单的修改上面的代码,添加一个临时变量,再增加到调用的混淆算法中。

全文代码以NUnit测试方法写成,单元测试配合Resharper真是好用,可以节省大量的代码,一个方法即可作为入口程序启动运行。

给开发和测试带多很多方便。

时间: 2024-08-03 07:42:14

字符串混淆技术应用 设计一个字符串混淆程序 可混淆.NET程序集中的字符串的相关文章

python2设计一个用户登录

1 #设计一个可以输入3次的登陆程序 2 #用户登录 3 import getpass 4 flag = 3 5 while flag>0 : 6 _usr_ =raw_input('please enter your username:') 7 _pword_ =getpass.getpass('please enter your password:')#密文输入 8 if _usr_ =='admin' and _pword_=='123456': 9 print('Login succes

字符串混淆技术在.NET程序保护中的应用及如何解密被混淆的字符串

原文:字符串混淆技术在.NET程序保护中的应用及如何解密被混淆的字符串 Visual Studio提供的Dotfuscator保护程序,可以对用户代码中包含的字符串进行加密.比如下面的例子,为了找到这个程序的注册算法,用.NET Reflector加载程序集后,发现代码中的字符串,都变成这种形式的: Assembly executingAssembly = Assembly.GetExecutingAssembly(); ArrayList list = new ArrayList(); str

设计一个字符串类String(C++练习题)

要求:设计一个字符串类String,可以求字符串长度,可以连接两个串(如,s1="计算机",s2="软件",s1与s2连接得到"计算机软件"),并且重载"="运算符进行字符串赋值,编写主程序实现:s1="计算机科学",s2="是发展最快的科学!",求s1和s2的串长,连接s1和s2 #include "stdafx.h" #include <iostream&g

御安全浅析安卓开发代码混淆技术

御安全浅析安卓开发代码混淆技术[关键词:代码混淆,Android应用加固,移动应用保护,APP保护,御安全] 提高native代码的安全性有什么好办法吗?答案是肯定的,今天我们就来介绍一种有效对抗native层代码分析的方法--代码混淆技术.随着移动互联网的快速发展,应用的安全问题不断涌现出来,于是越来越多的应用开发者将核心代码由java层转到native层,以对抗成熟的java逆向分析工具,然而如果native层的代码如果没有进行任何保护,还是比较容易被逆向分析工作者获取其运行逻辑,进而完成应

字符串 映射相应的 函数 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke

http://blog.csdn.net/qustdong/article/details/7267258 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke 标签: delphiintegerfunctionobjectsoapclass 2012-02-17 11:46 1139人阅读 评论(0) 收藏 举报  分类: Delphi(24)  首先看一段Delphi帮助中的介绍(After Delphi 6 ): Returns the a

Android代码混淆技术

Android混淆是Android开发者经常使用的一种用于防止被反编译的常见手法.Android开发基于java语言的,很容易被别人反编译出来,一下就相当于裸奔了,特别是用于商业用途的时候,防止反编译是必要的措施.而Android混淆的确可以保证Android源代码的一定安全. Android混淆技术 Java类名.方法名混淆 Dalvik字节码包含了大量的调试信息,如类名.方法名.字段名.参数名.变量名等,使用反编译工具可以还原这些信息.由于类名.方法名等通常都会遵循一定的命名规范,破解者很容

Oracle字符串行专列(字符串聚合技术)

原文链接:http://oracle-base.com/articles/misc/string-aggregation-techniques.php 1     String Aggregation Techniques 字符串聚合技术 On occasion it is necessary to aggregate data from a number ofrows into a single row, giving a list of data associated with a spec

【字符串处理算法】字符串包含的算法设计及C代码实现【转】

转自:http://blog.csdn.net/zhouzhaoxiong1227/article/details/50679587 版权声明:本文为博主原创文章,对文章内容有任何意见或建议,欢迎与作者单独交流,作者QQ(微信):245924426. 一.需求描述 输入一个由数字构成的字符串,编写程序将该字符串转换为整数并输出. 例如,如果输入的字符串是“12345”,那么输出的整数是12345.注意,不要使用C语言的库函数atoi. 二.算法设计 我们都知道,如果给定一个整数123,那么其表示

ProGuard代码混淆技术详解

前言 受<APP研发录>启发,里面讲到一名Android程序员,在工作一段时间后,会感觉到迷茫,想进阶的话接下去是看Android系统源码呢,还是每天继续做应用,毕竟每天都是画UI和利用MobileAPI处理Json还是蛮无聊的,做着重复的事情,没有技术的上提升空间的.所以,根据里面提到的Android应用开发人员所需要精通的20个技术点,写篇文章进行总结,一方面是梳理下基础知识和巩固知识,另一方面也是弥补自我不足之处. 那么,今天就来讲讲ProGuard代码混淆的相关技术知识点. 内容目录