加壳exe的方法--delphi实现

我命由我不由天空 2011-04-14 11:59:11
加精
加壳exe的方法--delphi实现 收藏
EXE文件加密器的原理:建立两个文件,一个用来添加资源到另外一个EXE文件里面,称为添加程序。另外一个被添加的EXE文件称为头文件。该程序的功能是把添加到自己里面的文件读出来。Windows下的EXE文件结构比较复杂,有的程序还有校验和,当发现自己被改变后会认为自己被病毒感染而拒绝执行。所以我们把文件添加到自己的程序里面,这样就不会改变原来的文件结构了。我们先写一个添加函数,该函数的功能是把一个文件当作一个流添加到另外一个文件的尾部。函数如下:
Function Cjt_AddtoFile(SourceFile,TargetFile:string):Boolean;
var
Target,Source:TFileStream;
MyFileSize:integer;
begin
try
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareExclusive);
Target:=TFileStream.Create(TargetFile,fmOpenWrite or fmShareExclusive);
try
Target.Seek(0,soFromEnd);//往尾部添加资源
Target.CopyFrom(Source,0);
MyFileSize:=Source.Size+Sizeof(MyFileSize);//计算资源大小,并写入辅程尾部
Target.WriteBuffer(MyFileSize,sizeof(MyFileSize));
finally
Target.Free;
Source.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
有了上面的基础,我们应该很容易看得懂这个函数。其中参数SourceFile是要添加的文件,参数TargetFile是被添加到的目标文件。比如说把 a.exe添加到b.exe里面可以:Cjt_AddtoFile('a.exe',b.exe');如果添加成功就返回True否则返回假。
根据上面的函数我们可以写出相反的读出函数:
Function Cjt_LoadFromFile(SourceFile,TargetFile :string):Boolean;
var
Source:TFileStream;
Target:TMemoryStream;
MyFileSize:integer;
begin
try
Target:=TMemoryStream.Create;
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareDenyNone);
try
Source.Seek(-sizeof(MyFileSize),soFromEnd);
Source.ReadBuffer(MyFileSize,sizeof(MyFileSize));//读出资源大小
Source.Seek(-MyFileSize,soFromEnd);//定位到资源位置
Target.CopyFrom(Source,MyFileSize-sizeof(MyFileSize));//取出资源
Target.SaveToFile(TargetFile);//存放到文件
finally
Target.Free;
Source.Free;
end;
except
Result:=false;
Exit;
end;
Result:=true;
end;
其中参数SourceFile是已经添加了文件的文件名称,参数TargetFile是取出文件后保存的目标文件名。比如说 Cjt_LoadFromFile('b.exe','a.txt');在b.exe中取出文件保存为a.txt。如果取出成功就返回True否则返回假。
打开Delphi,新建一个工程,在窗口上放上一个Edit控件Edit1和两个Button:Button1和Button2。Button的Caption属性分别设置为“确定”和“取消”。在Button1的Click事件中写代码:
var S:string;
begin
S:=ChangeFileExt(Application.ExeName,'.Cjt');
if Edit1.Text='790617' then
begin
Cjt_LoadFromFile(Application.ExeName,S);
{取出文件保存在当前路径下并命名"原文件.Cjt"}
Winexec(pchar(S),SW_Show);{运行"原文件.Cjt"}
Application.Terminate;{退出程序}
end
else
Application.MessageBox('密码不对,请重新输入!','密码错误',MB_ICONERROR+MB_OK);
编译这个程序,并把EXE文件改名为head.exe。新建一个文本文件head.rc,内容为: head exefile head.exe,然后把它们拷贝到Delphi的BIN目录下,执行Dos命令Brcc32.exe head.rc,将产生一个head.res的文件,这个文件就是我们要的资源文件,先留着。
我们的头文件已经建立了,下面我们来建立添加程序。
新建一个工程,放上以下控件:一个Edit,一个Opendialog,两个Button1的Caption属性分别设置为"选择文件"和"加密"。在源程序中添加一句:{$R head.res}并把head.res文件拷贝到程序当前目录下。这样一来就把刚才的head.exe跟程序一起编译了。
在Button1的Cilck事件里面写下代码:
if OpenDialog1.Execute then Edit1.Text:=OpenDialog1.FileName;
在Button2的Cilck事件里面写下代码:
var S:String;
begin
S:=ExtractFilePath(Edit1.Text);
if ExtractRes('exefile','head',S+'head.exe') then
if Cjt_AddtoFile(Edit1.Text,S+'head.exe') then
if DeleteFile(Edit1.Text) then
if RenameFile(S+'head.exe',Edit1.Text) then
Application.MessageBox('文件加密成功!','信息',MB_ICONINFORMATION+MB_OK)
else
begin
if FileExists(S+'head.exe') then DeleteFile(S+'head.exe');
Application.MessageBox('文件加密失败!','信息',MB_ICONINFORMATION+MB_OK)
end;
end;
其中ExtractRes为自定义函数,它的作用是把head.exe从资源文件中取出来。
Function ExtractRes(ResType, ResName, ResNewName : String):boolean;
var
Res : TResourceStream;
begin
try
Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
try
Res.SavetoFile(ResNewName);
Result:=true;
finally
Res.Free;
end;
except
Result:=false;
end;
end;
注意:我们上面的函数只不过是简单的把一个文件添加到另一个文件的尾部。实际应用中可以改成可以添加多个文件,只要根据实际大小和个数定义好偏移地址就可以了。比如说文件捆绑机就是把两个或者多个程序添加到一个头文件里面。那些自解压程序和安装程序的原理也是一样的,不过多了压缩而已。比如说我们可以引用一个LAH单元,把流压缩后再添加,这样文件就会变的很小。读出来时先解压就可以了。另外,文中EXE加密器的例子还有很多不完善的地方,比如说密码固定为"790617",取出EXE运行后应该等它运行完毕后删除等等。
...全文
2579 149 打赏 收藏 转发到动态 举报
写回复
用AI写文章
149 条回复
切换为时间正序
请发表友善的回复…
发表回复
许文君 2011-04-22
  • 打赏
  • 举报
回复
这也算加壳?也就修改了个PE头,勉强算捆绑。
milunhailili 2011-04-21
  • 打赏
  • 举报
回复
这还加精?这是很多年前 陈景韬 的文章吧..看那变量命名就知道了.Cjt....
alibaba_2011 2011-04-21
  • 打赏
  • 举报
回复
啥?加壳
无条件为你 2011-04-21
  • 打赏
  • 举报
回复
楼主你这只能算是流的一般应用。算是新手级的代码。
真正的加壳是不生成新exe的。
我写过类似rar的软件,可以把多个任意类型的文件合并生成一个文件,用的时候可以提取。其实就是写文件的时候记住每个流位置和流大小,定义一个结构,再把结构写入。
zlcp520 2011-04-20
  • 打赏
  • 举报
回复
内容存入剪贴板
a67564226 2011-04-20
  • 打赏
  • 举报
回复
这个好像一压缩就没有了
comewisdom 2011-04-20
  • 打赏
  • 举报
回复
呵呵,这个壳两分钟就会被搞定
edwardliqi 2011-04-19
  • 打赏
  • 举报
回复
标记一下,以后会用到
chendeman2010 2011-04-19
  • 打赏
  • 举报
回复
试试啦 看是不是有用
lixingchenqq_2008 2011-04-19
  • 打赏
  • 举报
回复
我一点都不想收藏。。。
loverholly 2011-04-19
  • 打赏
  • 举报
回复
仅仅只是加密外壳吗?容易破解不?有什么算法没有?
rhsy445566 2011-04-19
  • 打赏
  • 举报
回复
好东西啊!就要支持
minjimm 2011-04-19
  • 打赏
  • 举报
回复
看来还是心理简单点好啊
lmnpasmn 2011-04-19
  • 打赏
  • 举报
回复
试试没什么吧
zxj268 2011-04-19
  • 打赏
  • 举报
回复
没有用过这么高级的方法
youyidianmimang 2011-04-19
  • 打赏
  • 举报
回复
这个东西 我5,6年前也收藏过;呵呵
[Quote=引用 18 楼 notebook800 的回复:]
这也叫加壳?

这好像是病毒程序的做法吧。感染别人的exe
[/Quote]
张见 2011-04-18
  • 打赏
  • 举报
回复
过来学习学习
jzq832 2011-04-18
  • 打赏
  • 举报
回复
不太懂,学习一下
xyghzhb 2011-04-18
  • 打赏
  • 举报
回复
学习下。厉害
wuyuxin9898946 2011-04-18
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>会议系统——管理员菜单</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="../style/zTreeStyle.css" type="text/css" />
<style type="text/css">
body
{
background-color: white;
margin: 0;
padding: 0;
text-align: center;
}
div, p, table, th, td
{
list-style: none;
margin: 0;
padding: 0;
color: #333;
font-size: 12px;
font-family: dotum, Verdana, Arial, Helvetica, AppleGothic, sans-serif;
}
</style>

<script type="text/javascript" src="../js/jquery-1.4.2.js"></script>

<script type="text/javascript" src="../js/jquery-ztree-2.5.js"></script>

<script language="JavaScript" type="text/javascript">
<!--
var zTree;
var demoIframe;

var setting = {
isSimpleData: true,
treeNodeKey: "id",
treeNodeParentKey: "pId",
showLine: true,
root:{
isRoot:true,
nodes:[]
}
};

zNodes =[
{ id:2, pId:0, name:"会议管理系统管理", open:true},

{ id:22, pId:2, name:"账号管理", open:true},
{ id:221, pId:22, name:"会议账号生成", "url":"AccountBuild.htm", "target":"testIframe"},
{ id:222, pId:22, name:"会议账号管理", "url":"AccountManage.htm", "target":"testIframe"},


{ id:24, pId:2, name:"黑白名单管理", open:true},
{ id:241, pId:24, name:"主叫黑名单管理", "url":"InitiativeBLManage.htm", "target":"testIframe"},
{ id:242, pId:24, name:"被叫黑名单管理", "url":"PassiveBLManage.htm", "target":"testIframe"},

{ id:25, pId:2, name:"查询统计", open:true},
{ id:251, pId:25, name:"话单查询", "url":"RecordList.htm", "target":"testIframe"},

{ id:26, pId:2, name:"logo管理", open:true},
{ id:261, pId:26, name:"logo管理", "url":"LogoManage.htm", "target":"testIframe"},
{ id:27, pId:2, name:"操作员管理", open:true},
{ id:257, pId:27, name:"添加操作员", "url":"RecordList.htm", "target":"testIframe"}

];

$(document).ready(function(){
setting.expandSpeed = ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast";
zTree = $("#tree").zTree(setting, zNodes);
var nodes = zTree.getNodes();
zTree.selectNode(nodes[2].nodes[0].nodes[0]);

demoIframe = $("#testIframe");

});

function loadReady() {
var h = demoIframe.contents().find("body").height();
if (h < 600) h = 600;
demoIframe.height(h);
}

//-->
</script>

</head>
<body>
<table border="0" height="800px" align="left">
<tr>
<td width="230px" align="left" valign="top" style="border-right: #999999 1px dashed">
<ul id="tree" class="tree" style="width: 230px; overflow: auto;">
</ul>
</td>
<td width="770px" align="left" valign="top">
<iframe id="testIframe" name="testIframe" frameborder="0" scrolling="auto" width="1000px"
height="1000px" src="Default.htm" onload="loadReady();"></iframe>
</td>
</tr>
</table>
</body>
</html>
加载更多回复(59)

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧