Skip to content

Instantly share code, notes, and snippets.

@Retro64XYZ
Created February 2, 2021 04:23
Show Gist options
  • Save Retro64XYZ/2281a1796ef8f9d0c2f3377f74d52650 to your computer and use it in GitHub Desktop.
Save Retro64XYZ/2281a1796ef8f9d0c2f3377f74d52650 to your computer and use it in GitHub Desktop.
Write Binary Defense IP to UFW
#!/usr/bin/python
import requests
import ipaddress
import pyufw as ufw # https://github.com/5tingray/pyufw
def cleanup_banlist( LIST ):
cleaned = []
for item in LIST:
try:
ipaddress.ip_address( item )
except:
pass
else:
cleaned.append(item)
return cleaned
def get_banlist( URL ):
ban_text = requests.get( URL ).text
ban_list = ban_text.splitlines()
return ban_list
# sudo ufw deny from {ip-address-here} to any
def add_rule_to_firewall( IP ):
ufw.add( "deny from {} to any" ).format( IP )
def main():
ban_list = get_banlist( 'https://www.binarydefense.com/banlist.txt' )
cleaned_list = cleanup_banlist( ban_list )
for ip in cleaned_list:
add_rule_to_firewall( ip )
print( "All items have been added" )
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment