public bool ConfigSNAT(FWDeviceForm fw_dev, string EthName, string devIP, string EthIP, bool add_delete) { string flag = ""; string configEth_bridge = ""; string configInfo = ""; string configEth_IP = ""; string sql_rule = ""; string rule = "iptables -t nat -A POSTROUTING -s " + devIP + " -o br0 -j SNAT --to-source " + fw_dev.getDev_IP(); if (add_delete) { flag = "NAT1"; configEth_bridge = "brctl delif br0 " + EthName;//先将网口从网桥上删除 configEth_IP = "ifconfig " + EthName + " " + EthIP + " netmask 255.255.255.0" + " up"; configInfo = flag + configEth_bridge + " && " + configEth_IP + " && " + rule; sql_rule = "INSERT INTO SNAT values " + "('" + fw_dev.getDev_IP() + "','" + devIP + "','" + EthName + "','" + EthIP + "','" + fw_dev.getDev_IP() + "')"; } else if (!add_delete) { flag = "NAT0"; configEth_bridge = "brctl addif br0 " + EthName; configEth_IP = "ifconfig " + EthName + " " + "0.0.0.0 up"; configInfo = flag + configEth_IP + " && " + configEth_bridge + " && " + rule; sql_rule = "DELETE FROM SNAT where (fwIP='" + fw_dev.getDev_IP() + "' and origin_devIP='" + devIP + "'" + " and EthName='" + EthName + "' and EthIP='" + EthIP + "' and NATIP='" + fw_dev.getDev_IP() + "')"; } fw_dev.setDev_port(22222); NATdb_operate.dboperate(sql_rule); SendInfo sendcmd = new SendInfo(fw_dev); return(sendcmd.SendConfigInfo(configInfo)); }
public bool configARPRules(string dev_IP, string gateIP, string gateMAC, bool addDelete) { this.devform.setDev_IP(dev_IP); string fwmac = dboperate.getFwMAC(dev_IP); bool result = false; string arpRules = null; string sqlRule = null; string rule0 = "arptables -A INPUT -i br0 --src-mac ! " + gateMAC + " -j DROP"; string rule1 = "arptables -A INPUT -i br0 --src-ip " + gateIP + " --src-mac ! " + gateMAC + " -j DROP"; string rule2 = "arptables -A OUTPUT --destination-mac ff:ff:ff:ff:ff:ff -j ACCEPT"; if (addDelete) { arpRules = "ARP1" + rule0 + " && " + rule1 + " && " + rule2; sqlRule = "INSERT INTO arp VALUES('" + fwmac + "','" + gateIP + "','" + gateMAC + "')"; } else if (!addDelete) { arpRules = "ARP0" + rule0 + " && " + rule1 + " && " + rule2; sqlRule = "DELETE FROM arp where fwMAC='" + fwmac + "' and gateIP='" + gateIP + "' and gateMAC='" + gateMAC + "'"; } SendInfo send = new SendInfo(devform); send.SendConfigInfo(arpRules); dboperate.dboperate(sqlRule); return(result); }
public bool ChangeWhiteLists(string dev_IP, string dst_IP, string src_IP, string dst_port, string src_port, bool log_record, bool add_delete) { this.devform.setDev_IP(dev_IP); WhiteLists lists = new WhiteLists(); lists.setIPAndPort(dst_IP, src_IP, dst_port, src_port); string flag = null; string sql_rule = ""; string whiteList_from_client_to_server0 = "iptables -A FORWARD -p tcp -s " + lists.getsrc_IP() + " -d " + lists.getdst_IP() + " --sport " + lists.getsrc_port() + " --dport " + lists.getdst_port() + " -j ACCEPT "; // string whiteList_from_client_to_server1 = "iptables -A FORWARD -p tcp -d" + wl.getSrc_IP() + "--sport" + wl.getPort(); if (add_delete) { flag = "DPI1"; sql_rule = "INSERT INTO whitelist values " + "('" + dev_IP + "','" + dst_IP + "','" + src_IP + "','" + dst_port + "','" + src_port + "')"; } else { flag = "DPI0"; sql_rule = "DELETE FROM whitelist where (dev_IP='" + dev_IP + "' and dst_IP='" + dst_IP + "' and src_IP='" + src_IP + "' and dst_port='" + dst_port + "' and src_port='" + src_port + "')"; } string changewl = flag + whiteList_from_client_to_server0; LISTdb_operate.dboperate(sql_rule); //DeviceForm devform = new DeviceForm(dev_IP, 22222); SendInfo sendcmd = new SendInfo(devform); return(sendcmd.SendConfigInfo(changewl)); }
public bool ConfigModbusTcpRules(ModbusTcpRulesForm mtrf, bool log_flag, bool add_delete) { // RulesDataProcess.ModbusTcpRulesDataProcess(mtrf); String dpi_pro = "modbusTcp"; string flag = null; string dpi_rules_from_master_to_slave0 = null; string sql_rule = null;; if (mtrf.getSrc_IP() == "any" & mtrf.getDst_IP() == "any") { dpi_rules_from_master_to_slave0 = "iptables" + " -A" + " " + "FORWARD" + " " + "-p tcp" + " " + "--dport" + " " + "502" + " " + "-m" + " " + dpi_pro + " " + "--data-addr" + " " + mtrf.getMin_addr() + ":" + mtrf.getMax_addr() + " " + "--modbus-func " + mtrf.getfunc() + " " + "--modbus-data " + mtrf.getMin_data() + ":" + mtrf.getMax_data() + " -j" + " " + "DROP"; } else if (mtrf.getSrc_IP() == "any" & mtrf.getDst_IP() != "any") { dpi_rules_from_master_to_slave0 = "iptables" + " -A" + " " + "FORWARD" + " " + "-p tcp" + " " + "--dport" + " " + "502" + " " + "-d" + " " + mtrf.getDst_IP() + " " + "-m" + " " + dpi_pro + " " + "--data-addr" + " " + mtrf.getMin_addr() + ":" + mtrf.getMax_addr() + " " + "--modbus-func " + mtrf.getfunc() + " " + "--modbus-data " + mtrf.getMin_data() + ":" + mtrf.getMax_data() + " -j" + " " + "DROP"; } else if (mtrf.getSrc_IP() != "any" & mtrf.getDst_IP() == "any") { dpi_rules_from_master_to_slave0 = "iptables" + " -A" + " " + "FORWARD" + " " + "-p tcp" + " " + "--dport" + " " + "502" + " " + "-s " + mtrf.getSrc_IP() + " " + "-m" + " " + dpi_pro + " " + "--data-addr" + " " + mtrf.getMin_addr() + ":" + mtrf.getMax_addr() + " " + "--modbus-func " + mtrf.getfunc() + " " + "--modbus-data " + mtrf.getMin_data() + ":" + mtrf.getMax_data() + " -j" + " " + "DROP"; } else { dpi_rules_from_master_to_slave0 = "iptables" + " -A" + " " + "FORWARD" + " " + "-p tcp" + " " + "--dport" + " " + "502" + " " + "-s " + mtrf.getSrc_IP() + " " + "-d" + " " + mtrf.getDst_IP() + " " + "-m" + " " + dpi_pro + " " + "--data-addr" + " " + mtrf.getMin_addr() + ":" + mtrf.getMax_addr() + " " + "--modbus-func " + mtrf.getfunc() + " " + "--modbus-data " + mtrf.getMin_data() + ":" + mtrf.getMax_data() + " -j" + " " + " DROP"; } //string dpi_rules_from_master_to_slave1 = "iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT"; string dpi_rules_from_master_to_slave_log = "iptables" + " -A" + " " + "FORWARD" + " " + "-p tcp" + " " + "--dport" + " " + "502" + " " + "-s " + mtrf.getSrc_IP() + " " + "-d" + " " + mtrf.getDst_IP() + " " + "-m" + " " + dpi_pro + " " + "--data-addr" + " " + mtrf.getMin_addr() + ":" + mtrf.getMax_addr() + " " + "--modbus-func " + mtrf.getfunc() + " " + "--modbus-data " + mtrf.getMin_data() + ":" + mtrf.getMax_data() + " -j" + " " + "LOG" + " " + "--log-prefix " + "\"" + "DROP&modbus&data_illegal " + "\""; if (add_delete == true) { flag = "DPI1"; sql_rule = "INSERT INTO modbustcp values " + "('" + devform.getDev_IP() + "','" + mtrf.getDst_IP() + "'" + ",'" + mtrf.getSrc_IP() + "','" + mtrf.getMin_addr() + "','" + mtrf.getMax_addr() + "','" + mtrf.getfunc() + "','" + mtrf.getMin_data() + "','" + mtrf.getMax_data() + "','ACCEPT','" + log_flag + "')"; } else if (add_delete == false) { flag = "DPI0"; sql_rule = "DELETE FROM modbustcp where (dst_IP='" + mtrf.getDst_IP() + "' and src_IP='" + mtrf.getSrc_IP() + "' and min_coiladdr='" + mtrf.getMin_addr() + "'" + " and max_coiladdr='" + mtrf.getMax_addr() + "' and functioncode='" + mtrf.getfunc() + "' and min_speed='" + mtrf.getMin_data() + "' and max_speed='" + mtrf.getMax_data() + "' and method='ACCEPT' and log='" + log_flag + "')"; } string rule = flag + dpi_rules_from_master_to_slave_log + " && " + dpi_rules_from_master_to_slave0; DPIdb_operate.dboperate(sql_rule); SendInfo sendcmd = new SendInfo(devform); return(sendcmd.SendConfigInfo(rule)); }
public bool AddCNCRules(string devIP, bool log_flag, int connlimit, string srcIP, string dstIP, string sport, string dport) { this.devform.setDev_IP(devIP); if (devform.getDev_IP() == "0.0.0.0") { return(false); } string rule1 = "iptables -A FORWARD -p tcp --syn"; if (srcIP != "") { rule1 = rule1 + " -s " + srcIP; } if (sport != "") { rule1 = rule1 + " --sport " + sport; } if (dstIP != "") { rule1 = rule1 + " -d " + dstIP; } if (dport != "") { rule1 = rule1 + " --dport " + dport; } rule1 = rule1 + " -m connlimit --connlimit-above " + Convert.ToString(connlimit); string rule = "CNC1" + rule1 + " -j DROP"; if (log_flag) { rule = rule + " && " + rule1 + " -j LOG"; } string sql_str = "INSERT INTO CNC VALUES " + "('" + devIP + "'," + log_flag.ToString() + ",'" + connlimit.ToString() + "','" + srcIP + "','" + dstIP + "','" + sport + "','" + dport + "')"; db_operate.dboperate(sql_str); SendInfo sendcmd = new SendInfo(devform); return(sendcmd.SendConfigInfo(rule)); }
public bool AddSTDRules(string devIP, bool log_flag, string protocol, string srcIP, string dstIP, string sport, string dport) { this.devform.setDev_IP(devIP); if (devform.getDev_IP() == "0.0.0.0") { return(false); } string rule1 = "iptables -A FORWARD -p " + protocol; if (srcIP != "") { rule1 = rule1 + " -s " + srcIP; } if (sport != "") { rule1 = rule1 + " --sport " + sport; } if (dstIP != "") { rule1 = rule1 + " -d " + dstIP; } if (dport != "") { rule1 = rule1 + " --dport " + dport; } string rule = "STD1" + rule1 + " -m state --state NEW -j ACCEPT"; if (log_flag) { rule = rule + " && " + rule1 + " -m state --state NEW -j LOG"; } string sql_str = "INSERT INTO STD VALUES " + "('" + devIP + "'," + log_flag.ToString() + ",'" + protocol + "','" + srcIP + "','" + dstIP + "','" + sport + "','" + dport + "')"; db_operate.dboperate(sql_str); SendInfo sendcmd = new SendInfo(devform); return(sendcmd.SendConfigInfo(rule)); }
public bool DefaultRouteConfig(string devIP, bool add_del_flag, string Iface, string gateway) { this.devform.setDev_IP(devIP); if (devform.getDev_IP() == "0.0.0.0") { return(false); } string rule; string sql_str; if (add_del_flag) { rule = "PRT1route add default "; sql_str = "INSERT INTO PRT VALUES " + "('" + devIP + "'," + "'default-route','" + "" + "','" + "" + "','" + "" + "','" + Iface + "','" + gateway + "')"; } else { rule = "PRT0route del default "; sql_str = "DELETE FROM PRT WHERE (devIP='" + devIP + "' and route_type='default-route'" + " and Iface='" + Iface + "' and gateway='" + gateway + "')"; } if (Iface != "") { rule = rule + " dev " + Iface; } if (gateway != "") { rule = rule + " gw " + gateway; } db_operate.dboperate(sql_str); SendInfo sendcmd = new SendInfo(devform); return(sendcmd.SendConfigInfo(rule)); }
public bool ApplicationProtocolControl(string devIP, string protocol, bool pro_status) { this.devform.setDev_IP(devIP); if (devform.getDev_IP() == "0.0.0.0") { return(false); } string port = protocol_port[protocol]; string rule1 = "iptables -A INPUT -p tcp --dport " + port + " -j ACCEPT && " + "iptables -A OUTPUT -p tcp --dport " + port + " -j ACCEPT && " + "iptables -A FORWARD -p tcp --sport " + port + " -j ACCEPT && " + "iptables -A FORWARD -p tcp --dport " + port + " -j ACCEPT"; string rule2 = "iptables -A INPUT -p tcp --dport " + port + " -j DROP && " + "iptables -A OUTPUT -p tcp --dport " + port + " -j DROP && " + "iptables -A FORWARD -p tcp --sport " + port + " -j DROP && " + "iptables -A FORWARD -p tcp --dport " + port + " -j DROP"; string rule; string sql_str; if (pro_status) { rule = "APC2" + rule1 + "#" + protocol; sql_str = "update APC set status='allow' where protocol='" + protocol + "'"; } else { rule = "APC2" + rule2 + "#" + protocol; sql_str = "update APC set status='forbid' where protocol='" + protocol + "'"; } db_operate.dboperate(sql_str); SendInfo sendcmd = new SendInfo(devform); return(sendcmd.SendConfigInfo(rule)); }