PSMFWRule.pas
上传用户:xiuanze55
上传日期:2017-08-03
资源大小:1080k
文件大小:14k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {
  2.      PSM firewall rule.
  3.      Created on Feb, 09 2004
  4.      by Luu Truong Huy <lhuy@psmkorea.co.kr>
  5. }
  6. unit PSMFWRule;
  7. interface
  8. uses  Windows, Registry,SysUtils,Classes,StrUtils;
  9. type TPSMFWRule=class
  10. private
  11.      function MakePathRule(Path: PChar;IsPermit:Integer): string;
  12.      function MakeIPRule(FromIP:PChar; ToIP: PChar;FromPort:Integer;ToPort:Integer;IsPermit:Integer):string;
  13.      function PortStd(port:Integer): string  ;
  14. protected
  15. public
  16.      function IPStd(IP : Pchar): string;
  17.      {defind functions for IP rules}
  18.      procedure AddIPRule(FromIP:PChar; ToIP: PChar;
  19.                          FromPort:Integer;ToPort:Integer;IsPermit:Integer);
  20.      procedure RemoveIPRule(FromIP:PChar; ToIP: PChar);
  21.      //procedure ModifyIPrule(FromIP:PChar; ToIP: PChar;
  22.      //                    FromPort:Integer;ToPort:Integer;IsPermit:Integer);
  23.      procedure ModifyIPrule(FromIP:PChar; ToIP: PChar;
  24.                          oldFromPort:Integer;oldToPort:integer;
  25.                          FromPort:Integer;ToPort:Integer;IsPermit:Integer);
  26.      Procedure GetIPRuleList( var ipRuleList: TStringList);
  27.      procedure ExtractIPRule(rule:string; var fromIP: string;var toip:String;
  28.                                                 var fromPort: Integer;var toPort:Integer;
  29.                                                 var Permit:Integer);
  30.      function IPRuleExisted(FromIP:PChar; ToIP: PChar;
  31.                          FromPort:Integer;ToPort:Integer;IsPermit:Integer): Boolean;
  32.      {defind functions for Path rules}
  33.      procedure AddPathRule(Path: PChar; IsPermit: Integer);
  34.      function PathRuleExisted(Path: PChar; IsPermit: Integer):Boolean;
  35.      procedure RemovePathRule(Path: PChar);
  36.      procedure ModifyPathrule(Path: PChar; NewPermitValue: Integer);
  37.      Procedure GetPathRuleList( var pathRuleList: TStringList);
  38.      procedure ExtractPathRule(rule: string; var path: string; var permitted: integer);
  39.      {Security level Functions}
  40.      procedure SetSecurityLevel(level: Integer);
  41.      function GetSecurityLevel(): Integer;
  42. end;
  43. var
  44.   ROOT_IP_RULE: PChar   ='SOFTWAREPSMFirewallRules';
  45.   ROOT_PATH_RULE: PChar ='SOFTWAREPSMFirewallRules1';
  46.   PSM_FW_ROOT:Pchar='SOFTWAREPSMFirewall';
  47. implementation
  48. {make IP be standard format like xxx.xxx.xxx.xxx}
  49. function TPSMFWRule.IPStd(IP : Pchar): string;
  50. var
  51.      IpItems: TStringList;
  52.      strTmp,strTmp1: PChar;
  53.      I: Integer;
  54.      sTmp1:string;
  55. begin
  56.      IpItems:=TStringList.Create;
  57.      ExtractStrings(['.'],[' '],IP,IpItems);
  58.      for i:=0 to IpItems.Count-1 do
  59.      begin
  60.           sTmp1:= Trim(IpItems[i]);
  61.           strTmp1:=PChar(AnsiReplaceStr(Format('%3s',[sTmp1]),' ','0'));
  62.           IpItems[i]:=strTmp1;
  63.      end;
  64.      IpItems.Delimiter:='.';
  65.      Result:= IpItems.DelimitedText;
  66.      IpItems.Free;
  67. end;
  68. function TPSMFWRule.PortStd(port:Integer): string  ;
  69. var
  70.      FormatedPorts:string[5];
  71. begin
  72.      FormatedPorts:=Format('%5d',[Port]);
  73.      FormatedPorts:=Pchar(AnsiReplaceStr((FormatedPorts),' ','0'));
  74.      result:= FormatedPorts;
  75. end;
  76. function TPSMFWRule.MakePathRule(Path: PChar;IsPermit:Integer): string;
  77. var
  78.      strTmp: String;
  79. begin
  80.     if IsPermit<>0 then IsPermit:=1;
  81.     strTmp:=AnsiReplaceStr(Format('%1d',[IsPermit]),' ','0');
  82.     strTmp:=Format('%s|%s',[Path,strTmp]);
  83.     Result:= PChar(strTmp);
  84. end;
  85. function TPSMFWRule.MakeIPRule(FromIP:PChar; ToIP: PChar;
  86.                          FromPort:Integer;ToPort:Integer;IsPermit:Integer):string;
  87. var
  88.      FormatedPorts:string[11];
  89.      strTmp: string[255];
  90.      Buffer: array[0..250] of char;
  91. begin
  92.      if IsPermit<>0 then IsPermit:=1;
  93.      FromIP:=Pchar(IPStd(FromIP));
  94.      ToIP:=Pchar(IPStd(ToIP));
  95.      FormatedPorts:=Format('%5d|%5d',[FromPort,ToPort]);
  96.      FormatedPorts:=Pchar(AnsiReplaceStr((FormatedPorts),' ','0'));
  97.      StrFmt(Pchar(@strTmp),'%s|%s|%s|%1d|%1d',[FromIP,ToIP,FormatedPorts,0,IsPermit]);
  98.      Result:= PChar(@strTmp);
  99. end;
  100. procedure TPSMFWRule.AddIPRule(FromIP:PChar; ToIP: PChar;
  101.                          FromPort:Integer;ToPort:Integer;IsPermit:Integer);
  102. var
  103.      Reg: TRegistry;
  104.      IsOK: Boolean;
  105. begin
  106.      Reg := TRegistry.Create;
  107.      FromIP:=PChar(IPStd(FromIP));
  108.      ToIP:=PChar(IPStd(ToIP));
  109.      try
  110.           Reg.RootKey:=HKEY_LOCAL_MACHINE;
  111.           IsOk:=Reg.OpenKey(ROOT_IP_RULE,True);
  112.           if IsOK then
  113.                begin
  114.                     Reg.WriteString(MakeIPRule(FromIP,ToIP,FromPort,ToPort,IsPermit),'');
  115.                     Reg.CloseKey;
  116.                end;
  117.      finally
  118.           Reg.Free;
  119.      end;
  120. end;
  121. function TPSMFWRule.IPRuleExisted(FromIP:PChar; ToIP: PChar;
  122.                          FromPort:Integer;ToPort:Integer;IsPermit:Integer): Boolean;
  123. var
  124.      Reg: TRegistry;
  125.      IsOK: Boolean;
  126.      IsFound:Boolean;
  127. begin
  128.      Reg := TRegistry.Create;
  129.      IsFound:=False;
  130.      FromIP:=PChar(IPStd(FromIP));
  131.      ToIP:=PChar(IPStd(ToIP));
  132.      try
  133.           Reg.RootKey:=HKEY_LOCAL_MACHINE;
  134.           IsOk:=Reg.OpenKey(ROOT_IP_RULE,True);
  135.           if IsOK then
  136.                begin
  137.                     IsFound:=Reg.ValueExists(MakeIPRule(FromIP,ToIP,FromPort,ToPort,IsPermit));
  138.                     Reg.CloseKey;
  139.                end;
  140.      finally
  141.           Reg.Free;
  142.      end;
  143.      Result:=isFound;
  144. end;
  145. procedure TPSMFWRule.RemoveIPRule(FromIP:PChar; ToIP: PChar);
  146. var
  147.      Reg: TRegistry;
  148.      KeyNameList: TStringList;
  149.      Index: Integer;
  150.      RuleIdendify,StrTmp:String;
  151.      isOK: Boolean;
  152. begin
  153.      Reg := TRegistry.Create;
  154.      KeyNameList:=TStringList.Create;
  155.      FromIP:=PChar(IPStd(FromIP));
  156.      ToIP:=PChar(IPStd(ToIP));
  157.      RuleIdendify:=Format('%s|%s',[FromIP,ToIP]);
  158.      try
  159.         Reg.RootKey:=HKEY_LOCAL_MACHINE;
  160.         IsOk:=Reg.OpenKey(ROOT_IP_RULE,False);
  161.         if isOK then
  162.         begin
  163.           Reg.GetValueNames(KeyNameList);
  164.           for Index:=0 to  KeyNameList.Count-1 do
  165.           begin
  166.                StrTmp:= PChar(KeyNameList[Index]);
  167.                if Pos(RuleIdendify,StrTmp)>=1 then
  168.                begin
  169.                     Reg.DeleteValue(StrTmp);
  170.                     Reg.CloseKey;
  171.                     Break;
  172.                end;
  173.           end;
  174.        end;
  175.      finally
  176.           Reg.Free;
  177.           KeyNameList.Free;
  178.      end;
  179. end;
  180. procedure TPSMFWRule.ModifyIPrule(FromIP:PChar; ToIP: PChar;
  181.                          oldFromPort:Integer;oldToPort:integer;
  182.                          FromPort:Integer;ToPort:Integer;IsPermit:Integer);
  183. var
  184.      Reg: TRegistry;
  185.      KeyNameList: TStringList;
  186.      Index: Integer;
  187.      RuleIdendify,StrTmp:String;
  188.      isOK: Boolean;
  189.      //strTmp :string;
  190. begin
  191.      Reg := TRegistry.Create;
  192.      KeyNameList:=TStringList.Create;
  193.      FromIP:=PChar(IPStd(FromIP));
  194.      ToIP:=Pchar(IPStd(ToIP));
  195.      strTmp:=PortStd(oldFromPort)+'|'+PortStd(oldToPort);
  196.      RuleIdendify:=Format('%s|%s|%s',[FromIP,ToIP,strTmp]);
  197.      try
  198.         Reg.RootKey:=HKEY_LOCAL_MACHINE;
  199.         IsOk:=Reg.OpenKey(ROOT_IP_RULE,False);
  200.         if isOK then
  201.         begin
  202.              Reg.GetValueNames(KeyNameList);
  203.              for Index:=0 to  KeyNameList.Count-1 do
  204.              begin
  205.                  StrTmp:= KeyNameList[Index];
  206.                  if Pos(RuleIdendify,StrTmp)>=1 then
  207.                  begin
  208.                     Reg.DeleteValue(StrTmp);
  209.                     Reg.WriteString(MakeIPRule(FromIP,ToIP,FromPort,ToPort,IsPermit),'');
  210.                     Reg.CloseKey;
  211.                     Break;
  212.                 end;
  213.              end;
  214.         end;
  215.      finally
  216.           Reg.Free;
  217.           KeyNameList.Free;
  218.      end;
  219. end;
  220. procedure TPSMFWRule.AddPathRule(Path: PChar; IsPermit: Integer);
  221. var
  222.      Reg: TRegistry;
  223.      IsOK: Boolean;
  224. begin
  225.      Reg := TRegistry.Create;
  226.      try
  227.           Reg.RootKey:=HKEY_LOCAL_MACHINE;
  228.           IsOk:=Reg.OpenKey(ROOT_PATH_RULE,True);
  229.           if IsOK then
  230.                begin
  231.                     Reg.WriteString(MakePathRule(Path,IsPermit),'');
  232.                     Reg.CloseKey;
  233.                end;
  234.      finally
  235.           Reg.Free;
  236.      end;
  237. end;
  238. function TPSMFWRule.PathRuleExisted(Path: PChar; IsPermit: Integer):Boolean;
  239. var
  240.      Reg: TRegistry;
  241.      IsOK: Boolean;
  242.      IsFound:Boolean;
  243. begin
  244.      Reg := TRegistry.Create;
  245.      IsFound:=False;
  246.      try
  247.           Reg.RootKey:=HKEY_LOCAL_MACHINE;
  248.           IsOk:=Reg.OpenKey(ROOT_PATH_RULE,True);
  249.           if IsOK then
  250.                begin
  251.                     IsFound:=Reg.ValueExists(MakePathRule(Path,IsPermit));
  252.                     Reg.CloseKey;
  253.                end;
  254.      finally
  255.           Reg.Free;
  256.      end;
  257.      Result:=IsFound;
  258. end;
  259. procedure TPSMFWRule.RemovePathRule(Path: PChar);
  260. var
  261.      Reg: TRegistry;
  262.      IsOK: Boolean;
  263.      KeyNameList: TStringList;
  264.      Index: Integer;
  265.      StrTmp: PChar;
  266. begin
  267.      Reg := TRegistry.Create;
  268.      KeyNameList:=TStringList.Create;
  269.      try
  270.           Reg.RootKey:= HKEY_LOCAL_MACHINE;
  271.           IsOk:=Reg.OpenKey(ROOT_PATH_RULE,False);
  272.           if IsOK then
  273.                begin
  274.                     Reg.GetValueNames(KeyNameList);
  275.                     for Index:=0 to  KeyNameList.Count-1 do
  276.                     begin
  277.                          StrTmp:= PChar(KeyNameList[Index]);
  278.                          if (Pos(Path,StrTmp)>= 1) then
  279.                          begin
  280.                               Reg.DeleteValue(StrTmp);
  281.                               Reg.CloseKey;
  282.                               Break;
  283.                          end;
  284.                     end;
  285.                end;
  286.      finally
  287.           Reg.Free;
  288.           KeyNameList.Free;
  289.      end;
  290. end;
  291. procedure TPSMFWRule.ModifyPathrule(Path: PChar; NewPermitValue: Integer);
  292. var
  293.      Reg: TRegistry;
  294.      KeyNameList: TStringList;
  295.      Index: Integer;
  296.      StrTmp: PChar;
  297.      IsOK: Boolean;
  298. begin
  299.      Reg := TRegistry.Create;
  300.      KeyNameList:=TStringList.Create;
  301.      try
  302.         Reg.RootKey:= HKEY_LOCAL_MACHINE;
  303.         IsOk:=Reg.OpenKey(ROOT_PATH_RULE,False);
  304.         if(IsOK) then
  305.         begin
  306.           Reg.GetValueNames(KeyNameList);
  307.           for Index:=0 to  KeyNameList.Count-1 do
  308.           begin
  309.                StrTmp:= PChar(KeyNameList[Index]);
  310.                if (Pos(Path,StrTmp)>= 1)  then
  311.                begin
  312.                     Reg.DeleteValue(StrTmp);
  313.                     Reg.WriteString(MakePathRule(Path,NewPermitValue),'');
  314.                     Reg.CloseKey;
  315.                     Break;
  316.                end;
  317.             end;
  318.        end;
  319.      finally
  320.           Reg.Free;
  321.           KeyNameList.Free;
  322.      end;
  323. end;
  324. procedure TPSMFWRule.SetSecurityLevel(level:Integer);
  325. var
  326.      Reg: TRegistry;
  327.      isOK:Boolean;
  328. begin
  329.      Reg := TRegistry.Create;
  330.      if level<0 then level:=0;
  331.      if level>2 then level:=2;
  332.      try
  333.           Reg.rootKey:= HKEY_LOCAL_MACHINE;
  334.           IsOk:=Reg.OpenKey(PSM_FW_ROOT,TRUE);
  335.           if(IsOk) then
  336.           begin
  337.                 Reg.WriteInteger('Level',level);
  338.                 Reg.CloseKey;
  339.           end;
  340.      finally
  341.                Reg.Free;
  342.      end;
  343. end;
  344. function TPSMFWRule.GetSecurityLevel(): Integer;
  345. var
  346.      Reg: TRegistry;
  347.      isOK:Boolean;
  348.      retValue:Integer;
  349. begin
  350.      Reg := TRegistry.Create;
  351.      try
  352.           Reg.rootKey:= HKEY_LOCAL_MACHINE;
  353.           IsOk:=Reg.OpenKey(PSM_FW_ROOT,TRUE);
  354.           if(IsOk) then
  355.           begin
  356.                if(Reg.ValueExists('Level')) then
  357.                 retValue:=Reg.ReadInteger('Level')
  358.                else
  359.                   begin
  360.                       Reg.WriteInteger('Level',1);
  361.                       retValue:=1;
  362.                   end;
  363.                 Reg.CloseKey;
  364.           end;
  365.      finally
  366.                Reg.Free;
  367.      end;
  368.      Result:= retValue;
  369. end;
  370. {Implement functions that get the list of rules}
  371. Procedure TPSMFWRule.GetIPRuleList( var ipRuleList: TStringList);
  372. var
  373.      Reg: TRegistry;
  374.      isOK: Boolean;
  375. begin
  376.      reg:=TRegistry.Create;
  377.      try
  378.           Reg.RootKey:= HKEY_LOCAL_MACHINE;
  379.           IsOk:=Reg.OpenKey(ROOT_IP_RULE,TRUE);
  380.           if(IsOk) then
  381.           begin
  382.                Reg.GetValueNames(ipRuleList);
  383.           end;
  384.           Reg.CloseKey;
  385.      finally
  386.           Reg.Free;
  387.      end;
  388. end;
  389. Procedure TPSMFWRule.GetPathRuleList( var pathRuleList: TStringList);
  390. var
  391.      Reg: TRegistry;
  392.      isOK: Boolean;
  393. begin
  394.      reg:=TRegistry.Create;
  395.      try
  396.           Reg.RootKey:= HKEY_LOCAL_MACHINE;
  397.           IsOk:=Reg.OpenKey(ROOT_PATH_RULE,TRUE);
  398.           if(IsOk) then
  399.           begin
  400.                Reg.GetValueNames(pathRuleList);
  401.           end;
  402.           Reg.CloseKey;
  403.      finally
  404.           Reg.Free;
  405.      end;
  406. end;
  407. procedure   TPSMFWRule.ExtractPathRule(rule: string; var path: string; var permitted: integer);
  408. var
  409.      intPos: integer;
  410. begin
  411.      intPos:=Pos('|',rule);
  412.      path:=LeftStr(rule,intPos-1);
  413.      permitted:=StrtoInt(RightStr(rule,Length(rule)- intPos));
  414. end;
  415. procedure TPSMFWRule.ExtractIPRule(rule:string; var fromIP: string; var toip:String;
  416.                                                 var fromPort: Integer;var toPort:Integer;
  417.                                                 var Permit:Integer);
  418. var
  419.      strList:TStringList;
  420. begin
  421.      strList:=TSTringList.Create;
  422.      ExtractStrings(['|'],[' '],PChar(rule),strList);
  423.      fromIP:=strList[0];
  424.      toIp:=strList[1];
  425.      fromPort:=strtoint(strList[2]);
  426.      toPort:=strtoint(strList[3]);
  427.      //Item in 4th position is reserved
  428.      permit:=strtoint(strList[5]);
  429.      strList.Free;
  430. end;
  431. end.