iSafer_Option.pas
资源名称:00011511.rar [点击查看]
上传用户:xiuanze55
上传日期:2017-08-03
资源大小:1080k
文件大小:44k
源码类别:
Delphi控件源码
开发平台:
Delphi
- unit iSafer_Option;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, ComCtrls, ExtCtrls, StdCtrls, ShellAPI, Registry, IniFiles,
- ScktComp, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
- Sockets,StrUtils,IdTrivialFTPBase, Menus, Buttons, Winsock, ImgList;
- type
- TForm_Option = class(TForm)
- PageControl1: TPageControl;
- TabSheet_FWRule: TTabSheet;
- TabSheet_Level: TTabSheet;
- TabSheet_FWLog: TTabSheet;
- TabSheet_Folder: TTabSheet;
- GroupBox_Folder: TGroupBox;
- ListView_Folder: TListView;
- GroupBox_FWRule: TGroupBox;
- ListView_FWRule: TListView;
- GroupBox_Level: TGroupBox;
- Label1: TLabel;
- Label_Level0: TLabel;
- Label_Level1: TLabel;
- Label_Level2: TLabel;
- TrackBar_Level: TTrackBar;
- TabSheet_Port: TTabSheet;
- GroupBox_Port: TGroupBox;
- ListView_Port: TListView;
- ListView_PortTemp: TListView;
- Path_List: TListView;
- LogPopupMenu: TPopupMenu;
- mnuAddIPRule: TMenuItem;
- N1: TMenuItem;
- Display1: TMenuItem;
- mnuAddPathRule: TMenuItem;
- N2: TMenuItem;
- mnuDirection: TMenuItem;
- mnuPermission: TMenuItem;
- mnuApplicationPath: TMenuItem;
- mnuBytesReceived: TMenuItem;
- mnuBytesSent: TMenuItem;
- mnuSocketNumber: TMenuItem;
- mnuClearLog: TMenuItem;
- mnuHostName: TMenuItem;
- ProgressBar: TProgressBar;
- BmBtnAddNew: TBitBtn;
- BmtBtnDelete: TBitBtn;
- BmBtnFolderView: TBitBtn;
- BmBtnReload: TBitBtn;
- BmBtnFolderStop: TBitBtn;
- BmBtnFolderInfo: TBitBtn;
- BmBtnPortDefault: TBitBtn;
- BmBtnPortBackDoor: TBitBtn;
- BmBtnPortAll: TBitBtn;
- FWImageList: TImageList;
- BmBtnCancelSecuiry: TBitBtn;
- BmBtnApplySecurity: TBitBtn;
- ImageListForAppPath: TImageList;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- GroupBox1: TGroupBox;
- Label5: TLabel;
- Label_Level3: TLabel;
- GroupBox2: TGroupBox;
- ListView_FWLog: TListView;
- procedure UpdateFolderInfo(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure TabSheet_FolderShow(Sender: TObject);
- // 焊救 荐霖 汲沥 - 包访 窃荐
- procedure TabSheet_LevelShow(Sender: TObject);
- procedure TrackBar_LevelChange(Sender: TObject);
- // 规拳寒 肺弊 - 包访 窃荐
- // 傍蜡 弃歹 包府 - 包访 窃荐
- procedure ListView_FolderSelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- procedure Btn_FolderReloadClick(Sender: TObject);
- // 器飘 胶牡 - 包访 窃荐
- procedure ListView_FWRuleEnter(Sender: TObject);
- procedure Path_ListEnter(Sender: TObject);
- procedure ListView_FWRuleSelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- procedure mnuAddIPRuleClick(Sender: TObject);
- procedure mnuAddPathRuleClick(Sender: TObject);
- procedure mnuDirectionClick(Sender: TObject);
- procedure mnuApplicationPathClick(Sender: TObject);
- procedure mnuBytesReceivedClick(Sender: TObject);
- procedure mnuBytesSentClick(Sender: TObject);
- procedure mnuSocketNumberClick(Sender: TObject);
- procedure mnuPermissionClick(Sender: TObject);
- procedure ListView_FWLogEnter(Sender: TObject);
- procedure mnuClearLogClick(Sender: TObject);
- procedure mnuHostNameClick(Sender: TObject);
- procedure BmBtnAddNewClick(Sender: TObject);
- procedure BmtBtnDeleteClick(Sender: TObject);
- procedure BmBtnApplySecurityClick(Sender: TObject);
- procedure BmBtnCancelSecuiryClick(Sender: TObject);
- procedure BmBtnFolderViewClick(Sender: TObject);
- procedure BmBtnFolderInfoClick(Sender: TObject);
- procedure BmBtnFolderStopClick(Sender: TObject);
- procedure BmBtnReloadClick(Sender: TObject);
- procedure BmBtnPortDefaultClick(Sender: TObject);
- procedure BmBtnPortBackDoorClick(Sender: TObject);
- procedure BmBtnPortAllClick(Sender: TObject);
- procedure mnuRuleAddnewClick(Sender: TObject);
- procedure DeleteClick(Sender: TObject);
- procedure mnuRuleChangePermisionClick(Sender: TObject);
- { Private declarations }
- public
- FDriverLoaded: BOOL;
- bPortScanning: Boolean;
- ServerSocket:TServerSocket;
- //DDT
- procedure TrapMSG(var MSGX: TMessage);
- //DDT
- {
- Procedure FirewallCallback(name : pchar;
- messageBuf : pointer; messageLen : dword;
- answerBuf : pointer ; answerLen : dword); stdcall;
- }
- end;
- //DDT
- LogInf=record
- mTime: string[30];
- mDirection:string[10];
- mPermit:String[10];
- mIP:string;
- mHostName:shortstring;
- mPort:String[10];
- mPath:shortstring;
- mToltalRec:String[30];
- mToltalSen:String[30];
- mTotalRecSen:String[30];
- mSockNo:String[20];
- end;
- //DDT
- const
- MSGUDP = 'UDP / RemoteIP: %3d.%3d.%3d.%3d / LocalPort: %5d';
- MSGTCP = 'TCP / RemoteIP: %3d.%3d.%3d.%3d / LocalPort: %5d';
- MSGICMP = 'ICMP / RemoteIP: %3d.%3d.%3d.%3d / Type : %5d / Code: %5d';
- //DDT
- LogBuffSize=1024;
- //DDT
- var
- Form_Option: TForm_Option;
- OS: TOSVersionInfo;
- ChosenRuleType: Integer;
- isLogStarted: Boolean;
- //DDT
- Logs: array[1..LogBuffSize] of LogInf ;
- LogID: Integer;
- mainHWND: HWND;
- //DDT
- //update on march 17, imagelist for logging.
- logImages: TImageList;
- logPaths:TStringlist;
- const PATH_RULE_TYPE: Integer =1;
- const IP_RULE_TYPE: Integer=0;
- const REQUEST_TIMEOUT:Integer=1000;
- implementation
- uses iSafer_Main, iSafer_Resource, iSafer_FWRule,
- PSMFWRule, PSMFWLog,madCodeHook, FWDebug;//iShieldUnit_95, iShieldUnit_NT
- {$R *.dfm}
- function NameFromIP(ip:string;var hostname:shortstring): Boolean;
- var
- WSAData: TWSAData;
- InetAddr: u_long;
- HostEntPtr: PHostEnt;
- retVal:Boolean;
- len: Integer;
- begin
- WSAStartUp( $101, WSAData );
- try
- InetAddr := inet_addr(PChar(ip));
- if InetAddr = SOCKET_ERROR then
- raise Exception.Create( 'Invalid address entered' );
- HostEntPtr := GetHostByAddr( @InetAddr, len, AF_INET );
- if HostEntPtr = NIL then
- raise Exception.Create( 'WinSock error: ' + IntToStr( WSAGetLastError() ) );
- // Insert hostname into list
- hostname := String( HostEntPtr^.h_name );
- retVal:=True;
- except
- on E: Exception do begin
- retVal:=False;
- end;
- end;
- Result:=retVal;
- end;
- {Implement Log process call back function}//20040226
- Procedure PSMFW_Callback(name : pchar;
- messageBuf : pointer; messageLen : dword;
- answerBuf : pointer ; answerLen : dword); stdcall;
- Var
- HMapMutex: THandle;
- LogItems:TPSMFWLog;
- //ListItem: TListItem;
- //strTmp:String;
- //NumberOflogLine: Integer;
- MyLogID:Integer;
- begin
- //Check where there is space character
- //at the beginning of the line
- if(String(messageBuf)[1]=' ') then Exit;
- MyLogID:=-1;
- HMapMutex := CreateMutex(nil, false, pchar('PSMFirewallApplication'));
- if HMapMutex <> 0 then begin
- if WaitForSingleObject(HMapMutex,100) = WAIT_OBJECT_0 then begin
- LogID:=(LogID mod LogBuffSize) + 1;
- MyLogID:=LogID;
- end;
- ReleaseMutex(HMapMutex);
- CloseHandle(HMapMutex);
- end;
- if MyLogID>=0 then
- with LogItems do begin
- LogItems:= TPSMFWLog.Create;
- LogItems.AssignLogItems(messageBuf);
- Logs[MyLogID].mTime:=mTime;
- Logs[MyLogID].mDirection:=mDirection;
- Logs[MyLogID].mPermit:=mPermit;
- Logs[MyLogID].mIP:=mIP;
- Logs[MyLogID].mHostName:=mHostName;
- Logs[MyLogID].mPort:=mPort;
- Logs[MyLogID].mPath:=mPath;
- Logs[MyLogID].mToltalRec:=mToltalRec;
- Logs[MyLogID].mToltalSen:=mToltalSen;
- Logs[MyLogID].mTotalRecSen:=mTotalRecSen;
- Logs[MyLogID].mSockNo:=mSockNo;
- Free;
- PostMessage(mainHWND,WM_USER,MyLogID,MyLogID);
- end;//with LogItems do begin
- end;
- //DDT20040226
- procedure TForm_Option.TrapMSG(var MSGX: TMessage);
- var
- ListItem: TListItem;
- begin
- case MSGX.Msg of
- WM_USER://New log arrived
- begin
- if Form_Option.ListView_FWLog.Items.Count>500 then
- begin
- mnuClearLogClick(Nil);
- end;
- Form_Option.ListView_FWLog.Items.BeginUpdate;
- ListItem:=Form_Option.ListView_FWLog.Items.Add;
- ListItem.ImageIndex:=6;
- //display log information
- with Logs[MSGX.WParam] do
- begin
- //if(not NameFromIP(mIP,mHostName)) then mHostName:= mIP;
- if(logPaths.IndexOf(mPath)<0) then //This Path is not added
- begin
- ListItem.ImageIndex:=logImages.AddIcon(Form_FWRule.GetICON(mPath));
- logPaths.Add(LowerCase(mPath));
- end
- else //path is existing
- begin
- ListItem.ImageIndex:=logPaths.IndexOf(LowerCase(mPath));
- end;
- ListItem.Caption:=mTime;
- ListItem.SubItems.Add(mDirection);
- //if mPermit=0 then strTmp:='DENY'
- //else strTmp:='ALLOW';
- ListItem.SubItems.Add(mPermit);
- ListItem.SubItems.Add(mIP);
- ListItem.SubItems.Add(mHostName);
- ListItem.SubItems.Add(mPort);
- ListItem.SubItems.Add(mPath);
- ListItem.SubItems.Add(mToltalRec);
- ListItem.SubItems.Add(mToltalSen);
- ListItem.SubItems.Add(mTotalRecSen);
- ListItem.SubItems.Add(mSockNo);
- end;
- Form_Option.ListView_FWLog.Items.EndUpdate;
- if((Form_Option.ListView_FWLog.Selected=Nil)) then
- ListItem.MakeVisible(TRue);
- end;
- WM_USER+1://Reserved
- else WndProc(MSGX);
- end;
- end;
- //DDT
- // 傍蜡 弃歹 沥焊 舅酒郴扁
- procedure TForm_Option.UpdateFolderInfo(Sender: TObject);
- var
- i, j: Integer;
- sTemp: TStrings;
- valueName, valuePath, valueRemark, strTemp: String;
- valueSize: Integer;
- valueBuf: PChar;
- Registry, Registry2: TRegistry;
- ListItem: TListItem;
- begin
- ListView_Folder.Items.Clear;
- // Windows NT/2000 拌凯
- Registry:= TRegistry.Create;
- Registry.RootKey:= HKEY_LOCAL_MACHINE;
- if Registry.OpenKey('SYSTEMCurrentControlSetServiceslanmanserverShares', False) then begin
- sTemp:= TStringList.Create;
- Registry.GetValueNames(sTemp);
- for i:=0 to sTemp.Count-1 do begin
- valueName:= sTemp.Strings[i];
- valueSize:= Registry.GetDataSize(valueName);
- GetMem(ValueBuf, ValueSize);
- try
- if Registry.ReadBinaryData(ValueName, ValueBuf^, ValueSize) <> 0 then begin
- for j:=0 to valueSize-1 do begin
- if valueBuf[j] = #0 then valueBuf[j]:= '|';
- end;
- valuePath:= '';
- strTemp:= valueBuf;
- if Pos('|Path=', strTemp) > 0 then begin
- Delete(strTemp, 1, Pos('|Path=', strTemp) + Length('|Path=') - 1);
- valuePath:= Copy(strTemp, 1, Pos('|', strTemp) - 1);
- end;
- valueRemark:= '';
- strTemp:= valueBuf;
- if Pos('|Remark=', strTemp) > 0 then begin
- Delete(strTemp, 1, Pos('|Remark=', strTemp) + Length('|Remark=') - 1);
- valueRemark:= Copy(strTemp, 1, Pos('|', strTemp) - 1);
- end;
- ListItem:= ListView_Folder.Items.Add;
- ListItem.ImageIndex:=8;
- ListItem.Caption:= valueName;
- ListItem.SubItems.Add(valuePath);
- ListItem.SubItems.Add(valueRemark);
- end;
- finally
- FreeMem(ValueBuf);
- end;
- end;
- sTemp.Free;
- Registry.CloseKey;
- end;
- Registry.Free;
- // Windows 95/98 拌凯
- Registry:= TRegistry.Create;
- Registry.RootKey:= HKEY_LOCAL_MACHINE;
- if Registry.OpenKey('SoftwareMicrosoftWindowsCurrentVersionNetworkLanMan', False) then begin
- sTemp:= TStringList.Create;
- Registry.GetKeyNames(sTemp);
- for i:=0 to sTemp.Count-1 do begin
- valueName:= sTemp.Strings[i];
- Registry2:= TRegistry.Create;
- Registry2.RootKey:= HKEY_LOCAL_MACHINE;
- if Registry2.OpenKey('SoftwareMicrosoftWindowsCurrentVersionNetworkLanMan' + valueName, False) then begin
- valuePath:= Registry2.ReadString('Path');
- valueRemark:= Registry2.ReadString('Remark');
- ListItem:= ListView_Folder.Items.Add;
- ListItem.Caption:= valueName;
- ListItem.SubItems.Add(valuePath);
- ListItem.SubItems.Add(valueRemark);
- Registry2.CloseKey;
- end;
- Registry2.Free;
- end;
- sTemp.Free;
- Registry.CloseKey;
- end;
- Registry.Free;
- end;
- procedure TForm_Option.FormCreate(Sender: TObject);
- //var
- // bCheck: Boolean;
- begin
- //DDT
- LogID:=0;
- mainHWND:=self.Handle;
- WindowProc:=TrapMSG;//Change the WindowProc to User's proc.
- //DDT
- // 函荐 檬扁汲沥
- FDriverLoaded:= False;
- bPortScanning:= False;
- // 滚瓢 劝己拳 惑怕 檬扁拳
- BmBtnFolderView.Enabled:= False;
- BmBtnFolderInfo.Enabled:= False;
- BmBtnFolderStop.Enabled:= False;
- // 扁夯 其捞瘤 汲沥
- PageControl1.ActivePage:= TabSheet_FWRule;
- // OS 滚傈 沥焊 舅酒郴扁
- ZeroMemory(@OS,SizeOf(OS));
- OS.dwOSVersionInfoSize:=SizeOf(OS);
- GetVersionEx(OS);
- // 鸥捞赣 檬扁拳
- //Timer_Init.Enabled:= True;
- BmBtnApplySecurity.Enabled:=False;
- isLogStarted:=False;
- //Init log-image list.
- logImages:=TImageList.Create(Self);
- logPaths:=TStringList.Create;
- logImages.AddIcon(NIl);
- logPaths.Add('%$default$%');
- ListView_FWLog.SmallImages:= logImages;
- ServerSocket:=TServerSocket.Create(Form_Option);
- end;
- {
- Initial
- +Read rule set from Registry and Display it
- Feb 12, 2004
- LuuTruongHuy
- }
- procedure TForm_Option.FormShow(Sender: TObject);
- var
- fwRule:TPSMFWRule;
- ruleList:TStringList;
- ListItem: TListItem;
- i: Integer;
- //for IP rules
- fromIP: string;
- toIP: string;
- fromPort: Integer;
- toPort: Integer;
- //for Path rules
- Path: string;
- Permit: Integer;
- bCheck: boolean;
- imIndex:Integer;
- begin
- //Start FireWall Log here--huynote
- bCheck:= CreateIpcQueue('PSMFirewall', PSMFW_Callback);
- if(not bCheck)then
- Showmessage('Error when starting logging Firewall!')
- else
- isLogStarted:=True;
- ruleList:=TStringList.Create;
- fwRule.GetIPRuleList(ruleList);
- //For IP Rule
- for i:=0 to ruleList.Count-1 do
- begin
- fwRule.ExtractIPRule(ruleList[i],fromIP,toIP,fromPort,toPort,Permit);
- ListItem:= ListView_FWRule.Items.Add;
- ListItem.ImageIndex:=5;
- ListItem.Caption:= fromIp+' ~'+toIP;
- ListItem.SubItems.Add(Format('%d ~%d',[fromPort,toPort])) ;
- ListItem.SubItems.Add('TCP/UDP');
- if( permit=1) then
- ListItem.SubItems.Add('ALLOW')
- else
- ListItem.SubItems.Add('DENY');
- end;
- //For Path Rule
- ruleList.Clear;
- ImageListForAppPath.Clear;
- fwRule.GetPathRuleList(ruleList);
- for i:=0 to ruleList.Count-1 do
- begin
- fwRule.ExtractPathRule(ruleList[i],path, Permit);
- imIndex:=ImageListForAppPath.AddIcon(Form_FWRule.GetICON(path));
- ListItem:= Path_List.Items.Add;
- ListItem.Caption:= path;
- ListItem.ImageIndex:= imIndex;
- if permit=1 then
- ListItem.SubItems.Add('ALLOW')
- //ListItem.SubItems.i
- else
- ListItem.SubItems.Add('DENY');
- end;
- ruleList.Free;
- //For security level.
- TrackBar_Level.Position:=2-fwRule.GetSecurityLevel;
- TrackBar_LevelChange(nil);
- end;
- {close form options}
- procedure TForm_Option.FormDestroy(Sender: TObject);
- begin
- // 肺弊沤祸 辆丰
- if isLogStarted then begin
- DestroyIpcQueue('PSMFirewall');
- isLogStarted:=False;
- end;
- while bPortScanning do begin
- Sleep(100);
- Application.ProcessMessages;
- end;
- logImages.Free;
- logPaths.Free;
- end;
- procedure TForm_Option.FormClose(Sender: TObject;
- var Action: TCloseAction);
- var
- HMapMutex:THandle;
- begin
- //Hide form
- Action:=caHide; //TRR
- if isLogStarted then begin
- //Stop Logging function.
- DestroyIpcQueue('PSMFirewall');
- isLogStarted:=False;
- end;
- ListView_FWRule.Clear;
- HMapMutex := CreateMutex(nil, false, pchar('PSMFirewallApplication'));
- if HMapMutex <> 0 then begin
- if WaitForSingleObject(HMapMutex,100) = WAIT_OBJECT_0 then
- begin
- ListView_FWLog.Clear;
- end;
- end;
- ReleaseMutex(HMapMutex);
- CloseHandle(HMapMutex);
- Path_List.Clear;
- while bPortScanning do begin
- Sleep(100);
- Application.ProcessMessages;
- end;
- end;
- {
- Show Shared Folder List View
- }
- procedure TForm_Option.TabSheet_FolderShow(Sender: TObject);
- begin
- UpdateFolderInfo(self);
- end;
- {
- Show tab-page of choosing security level.
- }
- procedure TForm_Option.TabSheet_LevelShow(Sender: TObject);
- begin
- ;
- end;
- procedure TForm_Option.TrackBar_LevelChange(Sender: TObject);
- var
- nLevel: Integer;
- begin
- nLevel:= 2 - TrackBar_Level.Position;
- if (nLevel = 0) then begin
- Label_Level0.Caption:= 'Low';
- Label_Level2.Caption:= '- All IP/Ports are allow except:';
- Label_Level3.Caption:= '- All dened IP/Ports in the rule set are blocked';
- end else if (nLevel = 1) then begin
- Label_Level0.Caption:= 'Medium';
- Label_Level2.Caption:= '- Allow only Well - Known Ports';
- Label_Level3.Caption:= '- All dened IP/Ports in the rule set are blocked'
- end else if (nLevel = 2) then begin
- Label_Level0.Caption:= 'High';
- Label_Level2.Caption:= '- Block all IP/Ports except:';
- Label_Level3.Caption:= '- All allowed IP/Ports in the rule set are allowed'
- end;
- //Btn_Level_OK.Enabled:= True;
- BmBtnApplySecurity.Enabled:=True;
- end;
- {
- Select Shared foler in list view
- }
- procedure TForm_Option.ListView_FolderSelectItem(Sender: TObject;
- Item: TListItem; Selected: Boolean);
- begin
- if ListView_Folder.ItemIndex < 0 then begin
- BmBtnFolderView.Enabled:= False;
- BmBtnFolderInfo.Enabled:= False;
- BmBtnFolderStop.Enabled:= False;
- end else begin
- BmBtnFolderView.Enabled:= True;
- BmBtnFolderInfo.Enabled:= True;
- BmBtnFolderStop.Enabled:= True;
- end;
- end;
- {
- For shared folder
- }
- {
- procedure TForm_Option.Btn_FolderViewClick(Sender: TObject); // 弃歹 郴侩 焊扁
- var
- nIndex: Integer;
- strPath: String;
- begin
- nIndex:= ListView_Folder.ItemIndex;
- if nIndex >= 0 then begin
- strPath:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
- ShellExecute(Handle,'open','explorer',PChar(strPath),'',SW_SHOWNORMAL);
- end;
- end;
- }
- {
- For shared folder
- }
- {
- For shared folder
- }
- procedure TForm_Option.Btn_FolderReloadClick(Sender: TObject); // 货肺 绊魔
- begin
- UpdateFolderInfo(self);
- end;
- procedure TForm_Option.ListView_FWRuleEnter(Sender: TObject);
- begin
- ChosenRuleType:=IP_RULE_TYPE;
- end;
- procedure TForm_Option.Path_ListEnter(Sender: TObject);
- begin
- ChosenRuleType:=PATH_RULE_TYPE;
- end;
- procedure TForm_Option.ListView_FWRuleSelectItem(Sender: TObject;
- Item: TListItem; Selected: Boolean);
- begin
- end;
- {Add IP Rule from Log list by selecting add IP rule on popup menu}
- procedure TForm_Option.mnuAddIPRuleClick(Sender: TObject);
- var
- //selectedIndex: Integer;
- //Rule Items
- //time:string;
- ip: string;
- //port: integer;
- sPort:String;
- permission: integer;
- //SelItem:TListItem;
- //strTmp:String;
- fwRule:TPSMFWRule;
- begin
- if ListView_FWLog.Selected= nil then
- MessageBox(Self.Handle,'Select one item in log list first','PSM Firewall',MB_OK or MB_ICONEXCLAMATION)
- else
- begin
- ip:= ListView_FWLog.Selected.SubItems[2]; //IP column
- sPort:= ListView_FWLog.Selected.SubItems[4]; //Port column
- if CompareText('DENY',ListView_FWLog.Selected.SubItems[1])=0 then
- Permission:=0
- else Permission:=1;
- //ShowMessage(ip);
- //Form_FWRule.MaskEdit_sIP.Text:=fwRule.IPStd(PChar(ip));
- Form_FWRule.SetIPFrom(fwRule.IPStd(PChar(ip)));
- Form_FWRule.MaskEdit_sPort.Text:=Format('%5s',[sPort]);
- Form_FWRule.Radio_IP_Single.Checked:=TRUE;
- Form_FWRule.Radio_Port_Single.Checked:=TRUE;
- if Permission=1 then
- Form_FWRule.Radio_Access_Allow.Checked:=True
- else
- Form_FWRule.Radio_Access_Deny.Checked:=True;
- Form_FWRule.FWRule.ActivePage:=Form_FWRule.IpRulePage;
- Form_FWRule.Show;
- end;
- end;
- {Add Path Rule From Log List}
- procedure TForm_Option.mnuAddPathRuleClick(Sender: TObject);
- var
- path: string;
- permission: integer;
- begin
- if ListView_FWLog.Selected= nil then
- MessageBox(Self.Handle,'Select one item in log list first','PSM Firewall',MB_OK or MB_ICONEXCLAMATION)
- else
- begin
- path:= ListView_FWLog.Selected.SubItems[5]; //Path column
- if CompareText('DENY',ListView_FWLog.Selected.SubItems[1])=0 then
- Permission:=0
- else Permission:=1;
- //ShowMessage(path);
- Form_FWRule.PathEdit.Text:=LowerCase(path);
- if Permission=1 then
- Form_FWRule.Permission.ItemIndex:=0
- else
- Form_FWRule.Permission.ItemIndex:=1;
- Form_FWRule.FWRule.ActivePage:=Form_FWRule.PathRulePage;
- Form_FWRule.Show;
- end;
- end;
- {display or hide direction column}
- procedure TForm_Option.mnuDirectionClick(Sender: TObject);
- begin
- if mnuDirection.Checked then
- begin
- mnuDirection.Checked:= False;
- ListView_FWLog.Columns[1].Width:=0;
- end
- else
- begin
- mnuDirection.Checked:= TRue;
- ListView_FWLog.Columns[1].Width:=70;
- end;
- end;
- {display application path or not}
- procedure TForm_Option.mnuApplicationPathClick(Sender: TObject);
- begin
- if mnuApplicationpath.Checked then
- begin
- mnuApplicationpath.Checked:= False;
- ListView_FWLog.Columns[6].Width:=0;
- end
- else
- begin
- mnuApplicationpath.Checked:= True;
- ListView_FWLog.Columns[6].Width:=250;
- end;
- end;
- {display or hide bytes received}
- procedure TForm_Option.mnuBytesReceivedClick(Sender: TObject);
- begin
- if mnuBytesReceived.Checked then
- begin
- mnuBytesReceived.Checked:= False;
- ListView_FWLog.Columns[7].Width:=0;
- end
- else
- begin
- mnuBytesReceived.Checked:= True;
- ListView_FWLog.Columns[7].Width:=70;
- end;
- end;
- procedure TForm_Option.mnuBytesSentClick(Sender: TObject);
- begin
- if mnuBytesSent.Checked then
- begin
- mnuBytesSent.Checked:= False;
- ListView_FWLog.Columns[8].Width:=0;
- end
- else
- begin
- mnuBytesSent.Checked:= True;
- ListView_FWLog.Columns[8].Width:=70;
- end;
- end;
- procedure TForm_Option.mnuSocketNumberClick(Sender: TObject);
- begin
- if mnuSocketNumber.Checked then
- begin
- mnuSocketNumber.Checked:= False;
- ListView_FWLog.Columns[10].Width:=0;
- //ListView_FWLog.Columns[10].//
- end
- else
- begin
- mnuSocketNumber.Checked:= True;
- ListView_FWLog.Columns[10].Width:=50;
- end;
- end;
- procedure TForm_Option.mnuPermissionClick(Sender: TObject);
- begin
- if mnuPermission.Checked then
- begin
- mnuPermission.Checked:= False;
- ListView_FWLog.Columns[2].Width:=0;
- end
- else
- begin
- mnuPermission.Checked:= True;
- ListView_FWLog.Columns[2].Width:=70;
- end;
- end;
- procedure TForm_Option.ListView_FWLogEnter(Sender: TObject);
- begin
- if(ListView_FWLog.Selected<>Nil) then
- ListView_FWLog.Selected.MakeVisible(False);
- end;
- procedure TForm_Option.mnuClearLogClick(Sender: TObject);
- //var
- //HMapMutex: THandle;
- begin
- //HMapMutex := CreateMutex(nil, false, pchar('PSMFirewallApplication'));
- //if HMapMutex <> 0 then begin
- // if WaitForSingleObject(HMapMutex,100) = WAIT_OBJECT_0 then
- // begin
- ListView_FWLog.Visible:=False;
- ListView_FWLog.Items.BeginUpdate;
- ListView_FWLog.Items.Clear;
- ListView_FWLog.Items.EndUpdate;
- ListView_FWLog.Visible:=True;
- // end;
- //end;
- //ReleaseMutex(HMapMutex);
- //CloseHandle(HMapMutex);
- end;
- procedure TForm_Option.mnuHostNameClick(Sender: TObject);
- begin
- if mnuHostName.Checked then
- begin
- mnuHostName.Checked:= False;
- ListView_FWLog.Columns[4].Width:=0;
- end
- else
- begin
- mnuHostName.Checked:= True;
- ListView_FWLog.Columns[4].Width:=150;
- end;
- end;
- {
- Add new Rule, This function displays rule editor dialog
- }
- procedure TForm_Option.BmBtnAddNewClick(Sender: TObject);
- begin
- Form_FWRule.ShowModal;
- end;
- {
- remove Rules from display list
- Huy marked.
- }
- procedure TForm_Option.BmtBtnDeleteClick(Sender: TObject);
- var
- nIndex: Integer;
- //strFileName: String;
- //bSuccess: Boolean;
- //TempFile: TextFile;
- //Huy
- RuleIdentify: String;
- //StrTmp:String;
- FromIP,ToIP:string;
- FWRule: TPSMFWRule;
- Delim: Integer;
- //h:String;
- begin
- nIndex:= ListView_FWRule.ItemIndex;
- if((nIndex >= 0) and (ChosenRuleType=IP_RULE_TYPE)) then begin
- if Application.MessageBox('Delete the selected IP/Port rule?', 'PSM Firewall', MB_YESNO) = IDYES then begin
- {Remove from Registry}
- RuleIdentify:= ListView_FWRule.Items.Item[nIndex].Caption;
- Delim:=Pos('~',RuleIdentify);
- FromIP:=Trim(LeftStr(RuleIdentify,Delim-1));
- ToIP:=Trim(RightStr(RuleIdentify,Length(RuleIdentify)-Delim));
- //AnsiReplaceStr(RuleIdentify,'~','|');
- FWRule.RemoveIPRule(PChar(FromIP),PChar(ToIP));
- {Remore from Displaying List}
- ListView_FWRule.Items.Delete(nIndex);
- if(Form_Main.bFWStart)then
- ApplyFWStatus(1);
- end;
- end else begin //No rule is selected in IP Rule list, then try with Path List
- nIndex:= Path_List.ItemIndex;
- if(( nIndex>=0) and (ChosenRuleType=PATH_RULE_TYPE)) then
- begin
- if Application.MessageBox('Delete the selected application rule?', 'PSM Firewall', MB_YESNO) = IDYES then begin
- //delete from registry
- RuleIdentify:= Path_List.Items.Item[nIndex].Caption;
- FWRule.RemovePathRule(Pchar(RuleIdentify));
- //delre from List
- Path_List.Items.Delete(nIndex);
- if(Form_Main.bFWStart)then
- ApplyFWStatus(1);
- end;
- end
- else
- Application.MessageBox('Select the rule first!', 'iSafer', MB_OK);
- end;
- end;
- {
- Apply sercurity level
- }
- procedure TForm_Option.BmBtnApplySecurityClick(Sender: TObject);
- var
- nLevel: Integer;
- fwRuler:TPSMFWRule;
- begin
- nLevel:= 2 - TrackBar_Level.Position;
- fwRuler.SetSecurityLevel(nLevel);
- BmBtnApplySecurity.Enabled:= False;
- if(Form_Main.bFWStart)then
- ApplyFWStatus(1);
- end;
- {
- Cancel apply sercurity level
- }
- procedure TForm_Option.BmBtnCancelSecuiryClick(Sender: TObject);
- var
- nLevel: Integer;
- fwRule:TPSMFWRule;
- begin
- nLevel:=fwRule.GetSecurityLevel;
- TrackBar_Level.Position:= 2 - nLevel;
- BmBtnApplySecurity.Enabled:= False;
- end;
- procedure TForm_Option.BmBtnFolderViewClick(Sender: TObject);
- var
- nIndex: Integer;
- strPath: String;
- begin
- nIndex:= ListView_Folder.ItemIndex;
- if nIndex >= 0 then begin
- strPath:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
- ShellExecute(Handle,'open','explorer',PChar(strPath),'',SW_SHOWNORMAL);
- end;
- end;
- procedure TForm_Option.BmBtnFolderInfoClick(Sender: TObject);
- var
- nIndex: Integer;
- strItem: String;
- ExecInfo: TShellExecuteInfo;
- begin
- nIndex:= ListView_Folder.ItemIndex;
- if nIndex >= 0 then begin
- strItem:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
- FillChar(ExecInfo, SizeOf(ExecInfo), 0);
- ExecInfo.cbSize := SizeOf(TShellExecuteInfo);
- ExecInfo.fMask := SEE_MASK_INVOKEIDLIST or SEE_MASK_FLAG_NO_UI or SEE_MASK_NOCLOSEPROCESS;
- ExecInfo.lpFile := PChar(strItem);
- ExecInfo.lpVerb := 'Properties';
- ExecInfo.nShow := SW_SHOWDEFAULT;
- ShellExecuteEx(@ExecInfo);
- end;
- end;
- procedure TForm_Option.BmBtnFolderStopClick(Sender: TObject);
- type
- tFuncNT = function(ServerName, NetName: LPWSTR; Reserved: DWORD): DWord; stdcall;
- tFunc95 = function(ServerName, NetName: PChar; Reserved: DWORD): Integer; stdcall;
- var
- nIndex: Integer;
- strNameNT: WideString;
- strName95: String;
- strPath, strTemp: String;
- DLLHandle: THandle;
- MyFuncNT: TFuncNT;
- MyFunc95: TFunc95;
- begin
- nIndex:= ListView_Folder.ItemIndex;
- if nIndex >= 0 then begin
- strNameNT:= ListView_Folder.Items.Item[nIndex].Caption;
- strName95:= ListView_Folder.Items.Item[nIndex].Caption;
- strPath:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
- strTemp:= strName95 + ' -- Unshare the selected folder?';
- if Application.MessageBox(PChar(strTemp), 'iSafer', MB_YESNO) = IDYES then begin
- if OS.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
- // Windows NT/2000 拌凯
- DLLHandle:= LoadLibrary('netapi32.dll');
- if DLLHandle < 32 then exit;
- @MyFuncNT:= GetProcAddress(DLLHandle, 'NetShareDel');
- if not (@MyFuncNT = nil) then begin
- MyFuncNT('', PWideChar(strNameNT), 0);
- end;
- FreeLibrary(DLLHandle);
- end else begin
- // Windows 95/98 拌凯
- DLLHandle:= LoadLibrary('svrapi.dll');
- if DLLHandle < 32 then exit;
- @MyFunc95:= GetProcAddress(DLLHandle, 'NetShareDel');
- if not (@MyFunc95 = nil) then begin
- MyFunc95('', PChar(strName95), 0);
- end;
- FreeLibrary(DLLHandle);
- end;
- UpdateFolderInfo(self); // 傍蜡 弃歹 沥焊 舅酒郴扁
- end;
- end;
- end;
- procedure TForm_Option.BmBtnReloadClick(Sender: TObject);
- begin
- UpdateFolderInfo(self);
- end;
- procedure TForm_Option.BmBtnPortDefaultClick(Sender: TObject);
- var
- i, nPortCount: Integer;
- PortNumber:Integer;
- currTh:Integer;
- ListItem: TListItem;
- begin
- nPortCount:=0;
- ListView_PortTemp.Width:= 0;
- ListView_PortTemp.Height:= 0;
- ListView_PortTemp.Visible:= True;
- for i:=0 to ListView_PortTemp.Items.Count-1 do
- if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Default' then begin
- inc(nPortCount);
- end;
- ProgressBar.Min:=0;
- ProgressBar.Max:=100;
- ProgressBar.Position:=0;
- bPortScanning:= True;
- ListView_Port.Items.BeginUpdate;
- ListView_Port.Items.Clear;
- currTh:=0;
- for i:=0 to ListView_PortTemp.Items.Count-1 do begin
- if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Default' then
- begin
- ProgressBar.Position := Round(100*currTh/nPortCount);
- PortNumber:= StrToInt(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- try
- // Trying to open serversocket
- ServerSocket.Port := PortNumber;
- ServerSocket.Open();
- ServerSocket.Close();
- ListItem:= ListView_Port.Items.Add;
- ListItem.ImageIndex:=7;
- ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
- ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- ListItem.SubItems.Add('Close');
- except
- ListItem:= ListView_Port.Items.Add;
- ListItem.ImageIndex:=7;
- ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
- ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- ListItem.SubItems.Add(' Open ');
- end; // try/except
- inc(currTh);
- end;
- end;
- bPortScanning:= False;
- ListView_Port.Items.EndUpdate;
- ListView_PortTemp.Visible:= False;
- ProgressBar.Position :=0;
- end;
- procedure TForm_Option.BmBtnPortBackDoorClick(Sender: TObject);
- var
- i, nPortCount: Integer;
- PortNumber:Integer;
- currTh:Integer;
- ListItem: TListItem;
- begin
- //Count the number of backdoor port
- nPortCount:=0;
- ListView_PortTemp.Width:= 0;
- ListView_PortTemp.Height:= 0;
- ListView_PortTemp.Visible:= True;
- for i:=0 to ListView_PortTemp.Items.Count-1 do
- if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Backdoor' then begin
- inc(nPortCount);
- end;
- ProgressBar.Min:=0;
- ProgressBar.Max:=100;
- ProgressBar.Position:=0;
- bPortScanning:= True;
- ListView_Port.Items.BeginUpdate;
- ListView_Port.Items.Clear;
- currTh:=0;
- for i:=0 to ListView_PortTemp.Items.Count-1 do begin
- if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Backdoor' then
- begin
- ProgressBar.Position := Round(100*currTh/nPortCount);
- PortNumber:= StrToInt(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- try
- // Trying to open serversocket
- ServerSocket.Port := PortNumber;
- ServerSocket.Open();
- ServerSocket.Close();
- ListItem:= ListView_Port.Items.Add;
- ListItem.ImageIndex:=7;
- ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
- ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- ListItem.SubItems.Add('Close');
- except
- ListItem:= ListView_Port.Items.Add;
- ListItem.ImageIndex:=7;
- ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
- ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- ListItem.SubItems.Add(' Open ');
- end; // try/except
- inc(currTh);
- end;
- end;
- bPortScanning:= False;
- ListView_Port.Items.EndUpdate;
- ListView_PortTemp.Visible:= False;
- ProgressBar.Position :=0;
- end;
- procedure TForm_Option.BmBtnPortAllClick(Sender: TObject);
- var
- i, nPortCount: Integer;
- PortNumber:Integer;
- currTh:Integer;
- ListItem: TListItem;
- begin
- nPortCount:=0;
- ListView_PortTemp.Width:= 0;
- ListView_PortTemp.Height:= 0;
- ListView_PortTemp.Visible:= True;
- for i:=0 to ListView_PortTemp.Items.Count-1 do
- inc(nPortCount);
- ProgressBar.Min:=0;
- ProgressBar.Max:=100;
- ProgressBar.Position:=0;
- bPortScanning:= True;
- ListView_Port.Items.BeginUpdate;
- ListView_Port.Items.Clear;
- currTh:=0;
- for i:=0 to ListView_PortTemp.Items.Count-1 do begin
- //if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Backdoor' then
- ProgressBar.Position := Round(100*currTh/nPortCount);
- PortNumber:= StrToInt(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- // Trying to open serversocket
- try
- ServerSocket.Port := PortNumber;
- ServerSocket.Open();
- ServerSocket.Close();
- ListItem:= ListView_Port.Items.Add;
- ListItem.ImageIndex:=7;
- ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
- ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- ListItem.SubItems.Add('Close');
- except
- ListItem:= ListView_Port.Items.Add;
- ListItem.ImageIndex:=7;
- ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
- ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
- ListItem.SubItems.Add(' Open ');
- end;//Try
- inc(currTh);
- //end;
- end;
- bPortScanning:= False;
- ListView_Port.Items.EndUpdate;
- ListView_PortTemp.Visible:= False;
- ProgressBar.Position :=0;
- end;
- procedure TForm_Option.mnuRuleAddnewClick(Sender: TObject);
- begin
- if(ChosenRuleType=IP_RULE_TYPE) then
- begin
- Form_FWRule.FWRule.ActivePage:=Form_FWRule.IpRulePage;
- Form_FWRule.ShowModal;
- end
- else if (ChosenRuleType=PATH_RULE_TYPE) then
- begin
- Form_FWRule.FWRule.ActivePage:=Form_FWRule.PathRulePage;
- Form_FWRule.ShowModal;
- end;
- end;
- procedure TForm_Option.DeleteClick(Sender: TObject);
- begin
- BmtBtnDeleteClick(nil);
- end;
- procedure TForm_Option.mnuRuleChangePermisionClick(Sender: TObject);
- var
- FWRule :TPSMFWRule;
- RuleIdentify:string;
- nIndex:Integer;
- Delim:Integer;
- FromIP:string;
- ToIP:string;
- FromPort:string;
- ToPort:string;
- PortRange:string;
- SlectedItem:TListItem;
- Permision:String;
- PermissionValue:Integer;
- strmsg:string;
- begin
- if(ChosenRuleType=IP_RULE_TYPE) then
- begin
- nIndex:= ListView_FWRule.ItemIndex;
- if(nIndex>=0) then
- begin
- SlectedItem:= ListView_FWRule.Items.Item[nIndex];
- RuleIdentify:= SlectedItem.Caption;
- Delim:=Pos('~',RuleIdentify);
- FromIP:=Trim(LeftStr(RuleIdentify,Delim-1));
- ToIP:=Trim(RightStr(RuleIdentify,Length(RuleIdentify)-Delim));
- PortRange:= SlectedItem.SubItems[0];
- Permision:= SlectedItem.SubItems[2];
- Delim:=Pos('~',PortRange);
- FromPort:=Trim(LeftStr(PortRange,Delim-1));
- ToPort:=Trim(RightStr(PortRange,Length(PortRange)-Delim));
- if(Permision='DENY') then PermissionValue:=0
- else PermissionValue:=1;
- strmsg:='IP Rule : '#13#10'IP range: [From '+ FromIP+' , To '+ ToIP+']'#13#10;
- strmsg:=strmsg+'Port range: ['+FromPort+', '+ ToPort+']'#13#10;
- strmsg:=strmsg+'Permission: '+ Permision+''#13#10;
- strmsg:=strmsg+'**********************'#13#10;
- if(Permision='DENY') then
- strmsg:=strmsg+'Change permission to (Allow)?'
- else strmsg:=strmsg+'Change permission to (Deny)?';
- if Application.MessageBox(PChar(strmsg),'PSM Firewall', MB_YESNO) = IDYES then
- begin
- if(Permision='DENY') then
- begin
- FWRule.ModifyIPrule(Pchar(fromIP),PChar(toIP),StrToInt(fromPort),strtoint(toPort),StrToInt(fromPort),strtoint(toPort),1);
- SlectedItem.SubItems[2]:='ALLOW';
- end
- else
- begin
- FWRule.ModifyIPrule(Pchar(fromIP),PChar(toIP),StrToInt(fromPort),strtoint(toPort),strtoint(fromPort),strtoint(toPort),0) ;
- SlectedItem.SubItems[2]:='DENY' ;
- end;
- if(Form_Main.bFWStart) then
- ApplyFWStatus(1);
- end
- // Application.MessageBox('No rule is selected', 'PSM Firewall', MB_OK);
- end;
- end
- else if(ChosenRuleType=PATH_RULE_TYPE) then
- begin
- nIndex:= Path_List.ItemIndex;
- if(nIndex>=0) then
- begin
- SlectedItem:=Path_List.Items.Item[nIndex];
- RuleIdentify:= SlectedItem.Caption;
- Permision:= SlectedItem.SubItems[0];
- strmsg:='Application rule : '#13#10'program path = '+RuleIdentify+' '#13#10;
- strmsg:=strmsg+'Permission: '+ Permision+''#13#10;
- strmsg:=strmsg+'**********************'#13#10;
- if(Permision='DENY') then
- strmsg:=strmsg+'Change permission to (Allow)?'
- else strmsg:=strmsg+'Change permission to (Deny)?';
- if Application.MessageBox(PChar(strmsg),'PSM Firewall', MB_YESNO) = IDYES then
- begin
- if(Permision='DENY') then
- begin
- FWRule.ModifyPathrule(Pchar(RuleIdentify),1) ;
- SlectedItem.SubItems[0]:='ALLOW';
- end
- else
- begin
- FWRule.ModifyPathrule(Pchar(RuleIdentify),0) ;
- SlectedItem.SubItems[0]:='DENY' ;
- end;
- if(Form_Main.bFWStart) then
- ApplyFWStatus(1);
- end;
- end
- else
- begin
- Application.MessageBox('No rule is selected', 'PSM Firewall', MB_OK);
- end;
- end;
- end;
- end.