PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/web/html/function_sec_filter.php

http://windowsfw.googlecode.com/
PHP | 542 lines | 445 code | 58 blank | 39 comment | 127 complexity | 444c2a84204d8d1a072a76daecc6ccdd MD5 | raw file
  1. <?php
  2. include_once "function_obj_analyse.php";
  3. include_once "function_base_xml_class.php";
  4. class CSecFilter extends base_xml_class
  5. {
  6. function __construct()
  7. {
  8. $this->root_path = "/MINI/SECURITY/POLICY/FILTERS";
  9. $this->node_name = "FILTER";
  10. $this->key_array = array("Name", "InDev", "OutDev", "SrcAddr", "DstAddr", "Pro_L7", "Filter_Word","Mark_Set", "Log",
  11. "Srv", "Time", "Target", "Enabled");
  12. }
  13. function Apply()
  14. {
  15. $iptables = "/usr/local/bin/iptables ";
  16. $log = '';
  17. $command = $iptables. " -F USERFILTER ; ";
  18. $command.= $iptables. " -t nat -F ANTIVIRUS ; ";
  19. $ext_command = $iptables. " -F PROXYSERVER ;";
  20. $ret_list = $this->get_list();
  21. foreach ($ret_list as $ret)
  22. {
  23. // ------ Enabled ------
  24. if ($ret['Enabled'] != 1) {
  25. continue;
  26. }
  27. // ------ Table ------
  28. $table = " -A USERFILTER ";
  29. // ------ Active ------
  30. $operate = "";
  31. $value_xml = "";
  32. $value_xml = $ret['Target'];
  33. if ($value_xml == "DROP" || $value_xml == "ACCEPT"){
  34. $operate = " -j ". $value_xml;
  35. $log = ' -j LOG --log-prefix "FILTER_LOG" ';
  36. }
  37. else if ($value_xml == "FILTER") // key work filter.
  38. {
  39. $operate = " -m string --algo bm --string ". base64_decode($ret['Filter_Word']). " ";
  40. $log = ' -j LOG --log-prefix "KEYWORD_LOG" ';
  41. }
  42. else if ($value_xml == "MARK") // mark some policy.
  43. {
  44. $table = " -t mangle -A PREROUTING ";
  45. $operate = " -j MARK --set-mark ". $ret['Mark_Set']. " ";
  46. $log = ' -j LOG --log-prefix "SETMARK_LOG" ';
  47. }
  48. else if ($value_xml == "ANTI_VIRUS")
  49. {
  50. $table = " -t nat -A ANTIVIRUS ";
  51. if ($ret['Srv'] == "UE9QMw==" || // POP3
  52. $ret['Srv'] == "U01UUA==") // SMTP
  53. {
  54. $operate = " -j REDIRECT --to-port 8110 ";
  55. $ext_command .= $iptables. " -A PROXYSERVER -p tcp --dport 8110 -j ACCEPT;";
  56. //print "[POP3||SMTP]". $ext_command;
  57. }
  58. if ($ret['Srv'] == "SFRUUA==") // HTTP
  59. {
  60. $operate = " -j REDIRECT --to-port 8080 ";
  61. $ext_command .= $iptables. " -A PROXYSERVER -p tcp --dport 8080 -j ACCEPT;";
  62. //print "[HTTP]". $ext_command;
  63. }
  64. if ($ret['Srv'] == "RlRQ") // FTP
  65. {
  66. $operate = " -j REDIRECT --to-port 2121 ";
  67. $ext_command .= $iptables. " -A PROXYSERVER -p tcp --dport 2121 -j ACCEPT;";
  68. //print "[FTP]". $ext_command;
  69. }
  70. $log = ' -j LOG --log-prefix "VIRUS_LOG" ';
  71. }
  72. // ------ In dev ------
  73. $value_xml = "";
  74. $value_xml = $ret['InDev'];
  75. $indev = ($value_xml == '' || $value_xml == 'All')?(" "):(" -i ". $value_xml. " ");
  76. // ------ Out dev ------
  77. $value_xml = "";
  78. $value_xml = $ret['OutDev'];
  79. $outdev = ($value_xml == ''|| $value_xml == 'All')?(" "):(" -o ". $value_xml. " ");
  80. // ------ Protocol for L7 ------
  81. $value_xml = "";
  82. $value_xml = $ret['Pro_L7'];
  83. if ($value_xml == '' || $value_xml == 'All') {
  84. $pro_l7 = "";
  85. }
  86. else {
  87. include_once "function_obj_protocol.php";
  88. $pro = new CProtocol ();
  89. $pro_tmp = $pro->get_item_by_name($value_xml);
  90. foreach ($pro_tmp as $pl){
  91. $pro_l7 = " -m layer7 --l7proto ". $pl['Protocol']. " ";
  92. }
  93. }
  94. // ------ Service ------
  95. $value_xml = "";
  96. $value_xml = $ret['Srv'];
  97. $srv_list = GetServiceList($value_xml);
  98. //print_r($srv_list);
  99. // ------ Source address ------
  100. $value_xml = "";
  101. $value_xml = $ret['SrcAddr'];
  102. $src_list = GetSourceIpList($value_xml);
  103. //print_r($src_list);
  104. // ------ Des address ------
  105. $value_xml = "";
  106. $value_xml = $ret['DstAddr'];
  107. $dst_list = GetDestIpList($value_xml);
  108. //print_r($dst_list);
  109. $value_xml = "";
  110. $value_xml = $ret['Time'];
  111. $time = GetTimeString($value_xml);
  112. //print $time;
  113. foreach ($srv_list as $srv) {
  114. foreach( $src_list as $src){
  115. foreach ($dst_list as $dst){
  116. if ($ret['Log'] == 1){
  117. $command .= $iptables. $table. $time. $indev. $src. $srv. $outdev. $pro_l7. $dst. $log. ";";
  118. }
  119. $command .= $iptables. $table. $time. $indev. $src. $srv. $outdev. $pro_l7. $dst. $operate. ";";
  120. }
  121. }
  122. }
  123. }
  124. //print $command;
  125. //print $ext_command;
  126. $ret = shell_exec($command);
  127. $ret = shell_exec($ext_command);
  128. return $ret;
  129. }
  130. }
  131. $root_path = "/MINI/SECURITY/POLICY/FILTERS";
  132. $root_path_tmp = "/MINI/SECURITY/POLICY/FILTERS";
  133. $iptables = "/usr/local/bin/iptables ";
  134. function GetXmlSecObjectByName($name)
  135. {
  136. $ret_array = GetXmlSecObjectList();
  137. foreach ($ret_array as $ret)
  138. {
  139. if ($ret['Name'] == $name)
  140. return $ret;
  141. }
  142. return NULL;
  143. }
  144. function GetXmlSecObjectByNumber($n)
  145. {
  146. global $root_path_tmp;
  147. $query_string = $root_path_tmp. "/FILTER"."[". $n. "]";
  148. $key_array = array("Name", "InDev", "OutDev", "SrcAddr", "DstAddr", "Pro_L7", "Filter_Word","Mark_Set", "Log",
  149. "Srv", "Time", "Target", "Enabled");
  150. $list = GetAttributList($query_string, $key_array);
  151. $list[0]["Filter_Word"] = base64_decode($list[0]["Filter_Word"]);
  152. $r = $list[0];
  153. return $r;
  154. }
  155. function GetXmlSecFilterList()
  156. {
  157. global $root_path_tmp;
  158. $query_string = $root_path_tmp. "/FILTER";
  159. $key_array = array("Name", "InDev", "OutDev", "SrcAddr", "DstAddr", "Pro_L7", "Filter_Word", "Mark_Set", "Log",
  160. "Srv", "Time", "Target", "Enabled");
  161. $list = GetAttributList($query_string, $key_array);
  162. for ($i = 0; $i < count($list); $i ++)
  163. {
  164. $list[$i]["Filter_Word"] = base64_decode($list[$i]["Filter_Word"]);
  165. }
  166. return $list;
  167. }
  168. function GetXmlNumberSecFilters()
  169. {
  170. global $root_path;
  171. $query_string = $root_path. "/FILTER";
  172. return GetNumberOfNode($query_string);
  173. }
  174. function DelXmlSpecialOneSecObject($name)
  175. {
  176. global $root_path;
  177. $query_string = $root_path. "/FILTER[@Name='". $name. "']";
  178. DelSpecialNode($query_string);
  179. ApplySecFilterRule();
  180. }
  181. function AppendXmlSecOneFilter($in_array)
  182. {
  183. global $root_path;
  184. $node_name = "FILTER";
  185. $query_string = $root_path;
  186. $in_array['FilterType'] = "1";
  187. $in_array['Filter_Word'] = base64_encode($in_array['Filter_Word']);
  188. AppendAllAttrOfNode($query_string, $node_name, $in_array);
  189. ApplySecFilterRule();
  190. }
  191. function InsertXmlSecOneFilter($number, $in_array)
  192. {
  193. global $root_path;
  194. $node_name = "FILTER";
  195. $query_string_from = $root_path. '/FILTER['. $number. ']';
  196. //print $query_string_from;
  197. InsertOneNode($query_string_from, $node_name, $in_array);
  198. ApplySecFilterRule();
  199. }
  200. function SetXmlSecOneFilter($in_array, $number)
  201. {
  202. global $root_path;
  203. $in_array['Type'] = "1";
  204. $query_string = $root_path. "/FILTER[". $number. "]";
  205. $in_array['Filter_Word'] = base64_encode($in_array['Filter_Word']);
  206. EditAllAttrOfNode($query_string, $in_array);
  207. ApplySecFilterRule();
  208. }
  209. function MoveXmlOneSecFilter($from, $to)
  210. {
  211. global $root_path;
  212. $query_string_from = $root_path. "/FILTER". "[". $from. "]";
  213. $query_string_to= $root_path. "/FILTER". "[". $to. "]";
  214. $query_string_number= $root_path. "/FILTER";
  215. if ($from == $to)
  216. return SUCCESS;
  217. $number = GetXmlNumberSecFilters();
  218. if ($from > $number || $to > $number)
  219. return ERR_VALUE;
  220. if ($from > $to)
  221. {
  222. MoveFrontOneNode($query_string_from, $query_string_to);
  223. }
  224. else
  225. {
  226. $number = GetXmlNumberSecFilters();
  227. if ($to < $number)
  228. {
  229. $query_string_to= $root_path. "/FILTER". "[". ($to + 1 ). "]";
  230. MoveFrontOneNode($query_string_from, $query_string_to);
  231. }
  232. else
  233. {
  234. MoveEndOneNode($query_string_from, $query_string_to);
  235. }
  236. }
  237. ApplySecFilterRule();
  238. return SUCCESS;
  239. }
  240. function DelXmlAllSecFilters()
  241. {
  242. global $root_path;
  243. $query_string = $root_path. "/FILTER";
  244. DelSpecialNode($query_string);
  245. FlushAllFilterRule();
  246. }
  247. function FlushAllFilterRule()
  248. {
  249. $iptables = "/usr/local/bin/iptables ";
  250. $command = "";
  251. $command1 = $iptables. " -t nat -F USERFILTER; ";
  252. $command2 = $iptables. " -t mangle -F PREROUTING; ";
  253. $command = $command1. $command2;
  254. //print $command;
  255. $ret = shell_exec($command);
  256. return $ret;
  257. }
  258. function FlushProxyServerFilterRule()
  259. {
  260. $iptables = "/usr/local/bin/iptables ";
  261. $command = "";
  262. $command = $iptables. " -F PROXYSERVER;";
  263. //print $command;
  264. $ret = shell_exec($command);
  265. return $ret;
  266. }
  267. function ApplySecFilterRule()
  268. {
  269. include_once "function_sys_license.php";
  270. $license = CheckSysLicenseForRule();
  271. //$license = TRUE;
  272. if ($license == FALSE)
  273. {
  274. return ;
  275. }
  276. FlushAllFilterRule();
  277. FlushProxyServerFilterRule();
  278. ApplySecFilterRule_One();
  279. return ;
  280. }
  281. function ApplySecFilterRule_One()
  282. {
  283. $iptables = "/usr/local/bin/iptables ";
  284. $ret_list = GetXmlSecFilterList();
  285. $log = '';
  286. //$key_array = array("Name", "InDev", "OutDev", "SrcAddr", "DstAddr",
  287. // "Srv", "Time", "Target", "Enabled");
  288. $command = "";
  289. foreach ($ret_list as $ret)
  290. {
  291. //print $ret['Enabled'];
  292. if ($ret['Enabled'] == 0)
  293. {
  294. continue;
  295. }
  296. if ($ret['Log'] == 1){
  297. $log = ' -j LOG --log-prefix "Mini FILTER" ';
  298. }
  299. else
  300. $log = '';
  301. // ------ Table ------
  302. $table = " -A USERFILTER ";
  303. // ------ Active ------
  304. $value_xml = "";
  305. $value_xml = $ret['Target'];
  306. if ($value_xml == "ANTI_VIRUS")
  307. {
  308. if ($ret['Srv'] == "AntiVirus_POP3" || $ret['Srv'] == "AntiVirus_SMTP")
  309. {
  310. $operate = " -j REDIRECT --to-port 8110 ";
  311. //$ext_command .= $iptables. " -A PROXYSERVER -p tcp --dport 8110 -j ACCEPT;";
  312. }
  313. if ($ret['Srv'] == "AntiVirus_HTTP")
  314. {
  315. $operate = " -j REDIRECT --to-port 8080 ";
  316. //$ext_command .= $iptables. " -A PROXYSERVER -p tcp --dport 8080 -j ACCEPT;";
  317. }
  318. if ($ret['Srv'] == "AntiVirus_FTP")
  319. {
  320. $operate = " -j REDIRECT --to-port 2121 ";
  321. //$ext_command .= $iptables. " -A PROXYSERVER -p tcp --dport 2121 -j ACCEPT;";
  322. }
  323. }
  324. else if ($value_xml == "FILTER") // key work filter.
  325. {
  326. $operate = " -m string --algo bm --string ". $ret['Filter_Word']. " ";
  327. }
  328. else if ($value_xml == "MARK") // mark some policy.
  329. {
  330. $operate = " -j MARK --set-mark ". $ret['Mark_Set']. " ";
  331. $table = " -t mangle -A PREROUTING ";
  332. }
  333. else
  334. $operate = " -j ". $value_xml;
  335. // ------ In dev ------
  336. $value_xml = "";
  337. $value_xml = $ret['InDev'];
  338. $indev = ($value_xml == '' || $value_xml == 'All')?(" "):(" -i ". $value_xml. " ");
  339. // ------ Out dev ------
  340. $value_xml = "";
  341. $value_xml = $ret['OutDev'];
  342. $outdev = ($value_xml == ''|| $value_xml == 'All')?(" "):(" -o ". $value_xml. " ");
  343. // ------ Protocol for L7 ------
  344. $value_xml = "";
  345. $value_xml = $ret['Pro_L7'];
  346. if ($value_xml == '' || $value_xml == 'All')
  347. {
  348. $pro_l7 = "";
  349. }
  350. else
  351. {
  352. include_once "function_obj_protocol.php";
  353. $pro = new CProtocol ();
  354. $pro_tmp = $pro->get_item_by_name($value_xml);
  355. $pro_l7 = " -m layer7 --l7proto ". $pro_tmp['Protocol']. " ";
  356. //print $pro_l7;
  357. }
  358. // ------ Service ------
  359. $value_xml = "";
  360. $value_xml = $ret['Srv'];
  361. if ($value_xml == '' || $value_xml == 'All')
  362. {
  363. $srvtype = "";
  364. }
  365. else
  366. {
  367. $src_srv_port = "";
  368. $dst_srv_port = "";
  369. $srv_ret_list = GetRealServiceValueByName($value_xml);
  370. foreach ($srv_ret_list as $srv_ret)
  371. {
  372. if ($srv_ret['Protocol'] == '')
  373. continue;
  374. if ($srv_ret['Protocol'] == '1')
  375. {
  376. $srvtype = " -p icmp ";
  377. }
  378. else if ($srv_ret['Protocol'] == '6' || $srv_ret['Protocol'] == '17' )
  379. {
  380. if ($srv_ret['SrcStartPort'] != "" || $srv_ret['SrcEndPort'] != "" )
  381. {
  382. $src_srv_port .= " --sport ";
  383. $src_srv_port .= $srv_ret['SrcStartPort'];
  384. $src_srv_port .= ":";
  385. $src_srv_port .= $srv_ret['SrcEndPort'];
  386. $src_srv_port .= " ";
  387. }
  388. if ($srv_ret['DstStartPort'] != "" || $srv_ret['DstEndPort'] != "" )
  389. {
  390. $dst_srv_port .= " --dport ";
  391. $dst_srv_port .= $srv_ret['DstStartPort'];
  392. $dst_srv_port .= ":";
  393. $dst_srv_port .= $srv_ret['DstEndPort'];
  394. $dst_srv_port .= " ";
  395. }
  396. if ($srv_ret['Protocol'] == '6')
  397. {
  398. $srvtype = " -p tcp ";
  399. }
  400. if ($srv_ret['Protocol'] == '17')
  401. {
  402. $srvtype = " -p udp ";
  403. }
  404. $srvtype .= $src_srv_port. $dst_srv_port;
  405. }
  406. else // Other integer !
  407. {
  408. $srvtype = " -p ". $srv_ret['Protocol']. " ";
  409. }
  410. }
  411. }
  412. // ------ Source address ------
  413. $value_xml = "";
  414. $value_xml = $ret['SrcAddr'];
  415. if ($value_xml == ''|| $value_xml == 'All')
  416. {
  417. $src_temp = array ("IpString" => " ", "IpType" => -1);
  418. $src_ret_list = array( $src_temp);
  419. }
  420. else
  421. {
  422. $src_ret_list = GetRealNetValueByName($value_xml);
  423. }
  424. // ------ Des address ------
  425. $value_xml = "";
  426. $value_xml = $ret['DstAddr'];
  427. if ($value_xml == ''|| $value_xml == 'All')
  428. {
  429. $dst_temp = array ("IpString" => " ", "IpType" => -1);
  430. $dst_ret_list = array( $dst_temp);
  431. }
  432. else
  433. {
  434. $dst_ret_list = GetRealNetValueByName($value_xml);
  435. }
  436. foreach ($src_ret_list as $src_ret)
  437. {
  438. if ($src_ret["IpString"] == NULL)
  439. {
  440. continue;
  441. }
  442. if ($src_ret["IpType"] == 1 || $src_ret["IpType"] == 2)
  443. {
  444. $srcaddr = " -s ".$src_ret["IpString"];
  445. }
  446. else if ($src_ret["IpType"] == 0)
  447. {
  448. $srcaddr = " -m iprange --src-range ". $src_ret["IpString"];
  449. }
  450. else
  451. {
  452. $srcaddr = " ";
  453. }
  454. foreach ($dst_ret_list as $dst_ret)
  455. {
  456. if ($dst_ret["IpString"] == NULL)
  457. {
  458. continue;
  459. }
  460. if ($dst_ret["IpType"] == 1 || $dst_ret["IpType"] == 2)
  461. {
  462. $dstaddr = " -d ".$dst_ret["IpString"];
  463. }
  464. else if ($dst_ret["IpType"] == 0)
  465. {
  466. $dstaddr = " -m iprange --dst-range ". $dst_ret["IpString"];
  467. }
  468. else
  469. {
  470. $dstaddr = " ";
  471. }
  472. if ($ret['Log'] == 1){
  473. $command .= $iptables. $table. $indev. $srcaddr. $srvtype. $pro_l7. $outdev. $dstaddr. $log. ";";
  474. }
  475. $command .= $iptables. $table. $indev. $srcaddr. $srvtype. $pro_l7. $outdev. $dstaddr. $operate. ";";
  476. }
  477. }
  478. }
  479. //print $command;
  480. $ret = shell_exec($command);
  481. //$ret = shell_exec($ext_command);
  482. return $ret;
  483. }
  484. ?>