This module deals to manage firewall system. It includes iptables,firewalld and flask (FreeBSD). Currently handle IPtables and this library is able to develop for other firwalls like firwalld and flask.
cd AVAFirewall
pip install -r requirements.txt
pip install .
You can install this module and import to your module and use.Here is a sample of using avafirewall.
from AVAFirewall.avafirewall_interface import AVAFirewallInterface
obj = AVAFirewallInterface()
obj.create_chain(table_name, chain_name)
in the main directort run this command:
python -m unittest Tests.unit_test
AVAFirewall includes some method that describe bellow.
This method create chain input argument : table_name , chain_name output:
{'result': True, 'msg': msg}
sample code:
obj = AVAFirewallInterface()
obj.create_chain(table_name, chain_name)
This method rename chain
input argument : table_name , old_chain, new_chain
output:
{'result': True, 'msg': msg}
sample code:
obj = AVAFirewallInterface()
obj.rename_chain(table_name, chain_name, chain_rename)
This method delete chain
input argument : table_name , chain_name
output:
{'result': True, 'msg': msg}
sample code:
obj = AVAFirewallInterface()
obj.delete_chain(table_name, chain_rename)
Insert rule . It can insert in a specific position (rule number)(defualt position is 0)
input argument : Behow is a sample input for insert rule.
goto (chain_name) is mandatory.
For more information see document.
kwargs = {'table_name': 'FILTER', 'goto': 'chain2',
'protocol':'tcp','position':3}
kwargs['jump'] = {'name': 'DROP',
'chain': 'DROP'}
kwargs['match'] = [{'name': 'tcp',
'values': [{'key': 'dport',
'val': '22'}]
},
{'name': 'iprange',
'values': [{'key': 'src_range',
'val': '192.168.1.100-192.168.1.200'},
{'key': 'dst_range',
'val': '172.22.33.106'}
]
}
]
output:
{'result': True, 'msg': msg}
sample code:
kwargs = {'table_name': 'FILTER', 'goto': 'chain2',
'protocol':'tcp','position':3}
kwargs['jump'] = {'name': 'DROP',
'chain': 'DROP'}
kwargs['match'] = [{'name': 'tcp',
'values': [{'key': 'dport',
'val': '22'}]
},
{'name': 'iprange',
'values': [{'key': 'src_range',
'val': '192.168.1.100-192.168.1.200'},
{'key': 'dst_range',
'val': '172.22.33.106'}
]
}
]
result = obj_firewall.insert_rule(**kwargs)
Append rule at the end of the chain rules.
For more information see document.
input argument :
goto (chain_name) is mandatory.
kwargs = {'table_name': 'FILTER', 'goto': 'chain2',
'protocol':'tcp'}
kwargs['jump'] = {'name': 'DROP',
'chain': 'DROP'}
kwargs['match'] = [{'name': 'tcp',
'values': [{'key': 'dport',
'val': '22'}]
},
{'name': 'iprange',
'values': [{'key': 'src_range',
'val': '192.168.1.100-192.168.1.200'},
{'key': 'dst_range',
'val': '172.22.33.106'}
]
}
]
output:
{'result': True, 'msg': msg}
sample code:
kwargs = {'table_name': 'FILTER', 'goto': 'chain2',
'protocol':'tcp'}
kwargs['jump'] = {'name': 'DROP',
'chain': 'DROP'}
kwargs['match'] = [{'name': 'tcp',
'values': [{'key': 'dport',
'val': '22'}]
},
{'name': 'iprange',
'values': [{'key': 'src_range',
'val': '192.168.1.100-192.168.1.200'},
{'key': 'dst_range',
'val': '172.22.33.106'}
]
}
]
result = obj.append_rule(**kwargs)
Replace rule instead of a position (rule number).
For more information see document.
input argument :
goto (chain_name) is mandatory.
kwargs = {'table_name': 'FILTER', 'goto': 'chain2',
'protocol':'tcp'}
kwargs['jump'] = {'name': 'DROP',
'chain': 'DROP'}
kwargs['match'] = [{'name': 'tcp',
'values': [{'key': 'dport',
'val': '22'}]
},
{'name': 'iprange',
'values': [{'key': 'src_range',
'val': '192.168.1.100-192.168.1.200'},
{'key': 'dst_range',
'val': '172.22.33.106'}
]
}
]
output:
{'result': True, 'msg': msg}
sample code:
kwargs = {'table_name': 'FILTER', 'goto': 'testchain','source':'5.2.2.2',
'protocol':'udp', 'position':0}
kwargs['jump'] = {'chain': 'ACCEPT'}
kwargs['match'] = [{'name': 'comment',
'values': [{'key': 'comment',
'val': 'this is a test comment'}]
}
]
obj.replace_rule(**kwargs)
Delete rule with specific param that is indicated in input
input argument :
kwargs = {'table_name': 'FILTER', 'goto': 'testchain','source':'5.6.2.2',
'protocol':'udp'}
kwargs['jump'] = {'chain': 'ACCEPT'}
kwargs['match'] = [{'name': 'comment',
'values': [{'key': 'comment',
'val': 'this is a test comment'}]
}
output:
{'result': True, 'msg': msg}
sample code:
kwargs = {'table_name': 'FILTER', 'goto': 'testchain','source':'5.6.2.2',
'protocol':'udp'}
kwargs['jump'] = {'chain': 'ACCEPT'}
kwargs['match'] = [{'name': 'comment',
'values': [{'key': 'comment',
'val': 'this is a test comment'}]
}
]
obj.delete_rule(**kwargs)
By this method view all chains and rules in a specific table (default table is FILTER)
input argument : table_name (default is FILTER)
output:
{'result': True, 'msg': msg}
sample code:
obj.view_all(table_name)
- Table name in IPVS4 are :
- FILTER,
- NAT,
- MANGLE and
- RAW.
For IPv6 the tables are:
- FILTER,
- MANGLE,
- RAW and
- SECURITY.