HelloWorld.cs的源码如下:
using System;
namespace HelloWorld
{
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("HelloWorld!");
Console.ReadLine();
}
}
}
下面给出命令行示例,这个其实在.net framework sdk文档里就可以找到,不过为了方便,还是复制了过来:
编译 HelloWorld.cs 以产生 HelloWorld.exe:
csc HelloWorld.cs
编译 HelloWorld.cs 以产生 HelloWorld.dll:
csc /target:library HelloWorld.cs
编译 HelloWorld.cs 并创建 MyHelloWorld.exe:
csc /out:MyHelloWorld.exe HelloWorld.cs
通过使用优化和定义 DEBUG 符号,编译当前目录中所有的 C# 文件。输出为 HelloWorld.exe:
csc /define:DEBUG /optimize /out:HelloWorld.exe *.cs
编译当前目录中所有的 C# 文件,以产生 HelloWorld.dll 的调试版本。不显示任何徽标和警告:
csc /target:library /out:HelloWorld.dll /warn:0 /nologo /debug *.cs
将当前目录中所有的 C# 文件编译为 Something.xyz(一个 DLL):
csc /target:library /out:Something.xyz *.cs
编译 HelloWorld.cs 以产生 HelloWorld.dll:
csc /target:library HelloWorld.cs
这个就是我们使用最多的一个命令,其实可以简单的写成csc /t:library File.cs,另外的一个写法是 csc /out:mycodebehind.dll /t:library mycodebehind.cs 这个可以自己指定输出的文件名。
csc /out:mycodebehind.dll /t:library mycodebehind.cs mycodebehind2.cs 这个的作用是把两个cs文件装到一个.dll文件里。
csc.exe的环境变量设置
C#环境变量设置:
1、在桌面右击[我的电脑]->[属性]->[高级]->[环境变量]
2、在下面的系统变量栏点击“新建”
3、变量名输入“csc”
4、变量值输入:“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727”
5、然后在系统变量列表框中双击“Path”
6、在变量名文本框的最后面加入“;%csc%”
--【反编译】用ildasm破解和用ilasm来重新编译.net
用ildasm破解和用ilasm来重新编译.net
(1)程序员用高级OO语言(C#,VB.Net,VC.Net,Cobol,Python..)编写代码,这些代码经过
.Net 编译器(比如Visual Studio.Net),编译成统一的MSIL(Microsoft Intermediate Language)。NET应用程序是以MSIL的形式出现的,只有在程序执行的时候才通过即时编译器JIT(Just-In-Time)被编译为本机代 码。
(2)程序执行的时候,由JIT装入程序的MSIL,JIT同时作了很多其他的工作(装载相应的运行库,安全检测。。) 最后JIT将转化成本地机器码(EXE或者DLL)。
(3)本地机器码被装入内存,开始执行。
就是通过这几个步骤,.net实现了编程语言无关(都转化成MSIL);平台无关(执行的
时候根据本地配置,生成相应的机器码);安全性(JIT里面做了很多检测)。
作为Cracker,可以用.Net Framework SDK自带的工具进行MSIL和EXE(DLL)之间的互换:
利用FrameworkSDK\Bin\目录下的ilasm和ildasm
(1)ilasm示例:(将msil编译成exe或者dll的机器码可执行文件)
下面的命令对 MSIL 文件 HelloWorld.il 进行汇编并产生可执行文件 HelloWorld.exe:
ilasm HelloWorld 或者 ilasm HelloWorld.il
下面的命令对 MSIL 文件 HelloWorld.il 进行汇编并产生 .dll 文件 myTestFile.dll:
ilasm HelloWorld /dll 或者 ilasm HelloWorld.il /dll
下面的命令对 MSIL 文件 HelloWorld.il 进行汇编并产生 .dll 文件 myNewTestFile.dll:
ilasm HelloWorld /dll /output:MyHelloWorld.dll
(2)ildasm示例(将exe或者dll的机器码可执行文件反汇编为msil)
下面的命令使 PE 文件 HelloWorld.exe 的元数据和反汇编代码显示在 Ildasm.exe 的默认 GUI 中:
ildasm HelloWorld.exe
下面的命令对 HelloWorld.exe 文件进行反汇编,并将结果 MSIL 汇编程程序文本存储在 HelloWorld.il 文件中:
ildasm MyFile.exe /output:HelloWorld.il
下面的命令对 HelloWorld.exe 文件进行反汇编,并将结果 MSIL 汇编程序文本显示到控制台窗口中:
ildasm HelloWorld.exe /text
如果文件 HelloWorld.exe 包含嵌入的托管和非托管资源,则下面的命令将产生以下 4 个文件:HelloWorld.il、HelloWorld.res、Icons.resources 和 Message.resources:
ildasm HelloWorld.exe /output:HelloWorld.il
下面的命令对 HelloWorld.exe 的 HelloWorld类中的 HelloWorld方法进行反汇编,并将输出显示到控制台窗口中:
ildasm /item:HelloWorld::HelloWorld MyFile.exe /text或者ildasm /item:HelloWorld::HelloWorld MyFile.exe /output:helloworld.il
在上面的示例中,可能有几个具有不同签名的 MyMethod 方法。下面的命令对返回类型为 void 且带有参数 int32 和 System.string 的 MyMethod 方法进行反汇编。
ildasm /item:"HelloWorld::HelloWorld(void(int32,class System.String))" HelloWorld.exe /text
--反编译的msil如下:
// Microsoft (R) .NET Framework IL Disassembler. Version 2.0.50727.42
// Copyright (c) Microsoft Corporation. All rights reserved.
// Metadata version: v2.0.50727
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 2:0:0:0
}
.assembly HelloWorld
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module HelloWorld.exe
// MVID: {61123790-7F56-4795-B1EA-DBDDD487C69A}
.imagebase 0x00400000
.file alignment 0x00002000
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x02FE0000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit HelloWorld.HelloWorld
extends [mscorlib]System.Object
{
.method public hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "HelloWorld!"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: call string [mscorlib]System.Console::ReadLine()
IL_0011: pop
IL_0012: ret
} // end of method HelloWorld::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method HelloWorld::.ctor
} // end of class HelloWorld.HelloWorld
// =============================================================
// *********** 反汇编完成 ***********************
// 警告: 创建了 Win32 资源文件 helloworld.res
如上的 中间语言给我们的感受是它很类似于早先的汇编语言,但它具有了对象定义和操作的功能。我们可以看到它定义并实现了一个继承自System.Object 的HelloWorld类及两个函数:Main()和.ctor()。其中.ctor()是HelloWorld类的构造函数,我们没有在源代码定义构造函数,但C#的编译器为我们添加了它。你还可以看到C#编译器也强制HelloWorld类继承System.Object类,虽然这个我们也没有指定。
那么PE文件是怎么执行的呢?下面是一个典型的C#/.NET应用程序的执行过程:
1.用户执行编译器输出的应用程序(PE文件),操作系统载入PE文件,以及其他的DLL(.NET动态连接库)。
2.操作系统装载器根据前面PE文件中的可执行文件头跳转到程序的入口点。显然,操作系统并不能执行中间语言,该入口点也被设计为跳转到mscoree.dll(.NET平台的核心支持DLL)的_ CorExeMain()函数入口。
3.CorExeMain()函数开始执行PE文件中的中间语言代码。这里的执行的意思是通用语言运行时按照调用的对象方法为单位,用即时编译器将中间语言编译成本地机二进制代码,执行并根据需要存于机器缓存。
4.程序的执行过程中,垃圾收集器负责内存的分配,释放等管理功能。
5. 程序执行完毕,操作系统卸载应用程序。
动态编译:
using System;
using System.Reflection;
using System.Globalization;
using Microsoft.CSharp;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
///
/// c#
///
///
static void Main(string[] args)
{
// 1.CSharpCodePrivoder
CSharpCodeProvider bjCSharpCodePrivoder = new CSharpCodeProvider();
// 2.ICodeComplier,已经过时,可以直接调用CompileAssemblyFromSource
// ICodeComplier bjICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
// 3.CompilerParameters
CompilerParameters bjCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");//引用dll
objCompilerParameters.CompilerOptions = "/filealign:512";// /filealign:512 /ptimize
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
// 4.CompilerResults,编译
CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, new string[] { GenerateCode() });
if (cr.Errors.HasErrors)
{
Console.WriteLine("编译错误:");
foreach (CompilerError err in cr.Errors)
{
Console.WriteLine(err.ErrorText);
}
}
else
{
// 通过反射,调用HelloWorld的实例
Assembly bjAssembly = cr.CompiledAssembly;
object bjHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");
MethodInfo bjMI = objHelloWorld.GetType().GetMethod("OutPut");
// 调用执行
Console.WriteLine(objMI.Invoke(objHelloWorld, null));
}
Console.ReadLine();
}
static string GenerateCode()
{
StringBuilder sb = new StringBuilder();
sb.Append("using System;");
sb.Append(Environment.NewLine);
sb.Append("namespace DynamicCodeGenerate");
sb.Append(Environment.NewLine);
sb.Append("{");
sb.Append(Environment.NewLine);
sb.Append(" public class HelloWorld");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" public string OutPut()");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" return \"Hello world!\";");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append("}");
string code = sb.ToString();
Console.WriteLine(code);
Console.WriteLine();
return code;
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26025686/viewspace-701520/ ,如需转载,请注明出处,否则将追究法律责任。