用Delphi5.0实现注册表监视

类别:Delphi 点击:0 评论:0 推荐:
用Delphi5.0实现注册表监视


中南大学湘雅二医院信息中心 朱洪涛

 

随着Internet的不断普及,网络安全越来越受到人们的重视。除了计算机病毒以外,网上不断出现的的各类黑客软件、远程控制软件等,更让人们对自己的机器越来越不放心。而这类软件的多样性及不断更新等,使得单靠一些防病毒软件已不能完全保护自己的机器。

有没有什么好的方法防止来历不明的软件安装在自己的机器上呢?答案就是密切注意系统关键文件的变化。大家都知道,一个程序如果要在Windows启动时自动运行,一般有三种方法: 1.在开始菜单的[启动]组中加入快捷方式 2.在Win.ini中加入相关项目 3.在注册表的 HKEY_Local_Machine\SoftWare\Microsoft\Windows\CurrentVersion\Run主键下加入指向自己的键值。 而第一种方法太明显,很容易发现。所以一般的黑客程序使用后两种方法启动自己。笔者在此介绍一个自己编写的简单的注册表监视器,用于实时监视注册表中键值的变化,以发现不明来历的程序。读者有兴趣的可以在此基础上进一步完善。 程序设计思路 本程序用Delphi5.0开发。Delphi是Borland公司出品的快速可视化Windows程序开发工具,功能强大,易于使用。程序中通过一个定时器来实现每隔一定时间对注册表比较一次。程序在启动时保留一份原始的注册表相关键值的数据备份,然后定时和当前的键值进行比较,如果发现变化,则提示用户查看。 程序实现 1、在Delphi中建立一个新的Project,将Form1改名为FormMain 2、在FormMain上放置一个定时器控件TTimer,将Project保存为PiRegWatch.Dpr 3、修改PiRegWatch.Dpr中的代码:  Application.Initialize; Application.CreateForm(TFormMain, FormMain); //使主窗口启动时不显示 Application.ShowMainForm:=False; Application.Run; 在FormMain中增加几个对象。 对象     类型      说明 Reg     Tregistry    用于注册表的访问 IniFile    TiniFile    用于保存原始注册表数据 Log     TstringList   用于记录变化的日志 RegKeys   TstringList   用于存放Run分支下的主键名 4、在FormMain:OnCreate事件中保留原始注册表数据,主要代码如下: …… self.Reg:=TRegistry.Create; with self.Reg do begin RootKey:=HKEY_Local_Machine; If OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',false) then begin RegKeys:=TStringList.Create; GetValueNames(RegKeys); //取得Run下面的所有主键名 if not self.IniFile.SectionExists('RunList') then //如果没有保存过数据 begin for i:=0 to Regkeys.Count-1 do //保存原始数据 if (self.Reg.GetDataType(RegKeys.Strings[i])=rdString) or(self.Reg.GetDataType(RegKeys.Strings[i])=rdExpandString) then begin value:=self.Reg.ReadString(RegKeys.Strings[i]); self.IniFile.WriteString('RunList',RegKeys.Strings[i],value); end; end; end; end; …… 5、在TTimer1.OnTmer事件中加入比较注册表的代码。主要代码如下: procedure TFormMain.Timer1Timer(Sender: TObject); var i:integer; RegVal,IniVal:string; begin self.Timer1.Enabled:=False; self.Reg.GetValueNames(RegKeys); for i:=0 to RegKeys.Count-1 do //检查新加的和已修改了的键值 if (self.Reg.GetDataType(RegKeys.Strings[i])=rdString) or (self.Reg.GetDataType(RegKeys.Strings[i])=rdExpandString) then begin RegVal:=self.Reg.ReadString(RegKeys.Strings[i]); IniVal:=self.IniFile.ReadString('RunList',RegKeys.Strings[i],''); if RegVal<>IniVal then begin self.LogMsg('Item Add:'+RegKeys.Strings[i]+'='+RegVal); self.IniFile.WriteString('RunList',RegKeys.Strings[i],RegVal); try //提示用户 SendMsg('ABC','','注册表被改变:新增项目'+RegKeys.Strings[i]+'='+RegVal); finally end; end; end; self.IniFile.ReadSection('RunList',RegKeys); for i:=0 to RegKeys.Count-1 do //检查已被删除的键值 begin IniVal:=self.IniFile.ReadString('RunList',RegKeys.Strings[i],''); if self.Reg.ValueExists(RegKeys.Strings[i]) and ((self.Reg.GetDataType(RegKeys.Strings[i])=rdString) or (self.Reg.GetDataType(RegKeys.Strings[i])=rdExpandString) ) then RegVal:=self.Reg.ReadString(RegKeys.Strings[i]) else RegVal:=''; if (IniVal<>'') and (RegVal='') then begin self.LogMsg('Item Del:'+RegKeys.Strings[i]+'='+IniVal); self.IniFile.DeleteKey('RunList',RegKeys.Strings[i]); try SendMsg('ABC','','注册表被改变:项目删除'+RegKeys.Strings[i]+'='+IniVal); finally end; end; end; self.IniFile.UpdateFile; self.Timer1.Enabled:=True; end; 6、在FormMain:OnClose事件中进行对象释放及必要的清理工作 procedure TFormMain.FormClose(Sender: TObject; var Action:TCloseAction); begin if Assigned(self.Reg) then self.Reg.Free; if Assigned(self.IniFile) then self.IniFile.Free; if Assigned(self.LogFile) then self.LogFile.Free; if Assigned(self.RegKeys) then self.RegKeys.Free; end; 经过实际运行,该程序在发现来历不明的程序方面确实能起到一定的作用。当然,它的功能也很单一,如果要进一步完善,增加监视系统其他关键文件的变化,则效果会更好。希望能与有兴趣的读者交流。 下载源代码。

本文地址:http://com.8s8s.com/it/it5692.htm