Giter Site home page Giter Site logo

pyufw's Introduction

pyufw

A python wrapper for UFW (Uncomplicated FireWall), a wrapper for iptables.

Install

pyufw is avaliable from PyPi. You can download it using pip:

$ pip3 install pyufw

Also make sure ufw is installed. Depending on your distribution the package may be named ufw or python-ufw.

Documentation

Your script will have to be run with root privilages. Upon importing the module the ufw security checks will start and you may see some warning messages. The following checks will commence:

  • is setuid or setgid (for non-Linux systems)
  • checks that script is owned by root
  • checks that every component in absolute path are owned by root
  • warn if script is group writable
  • warn if part of script path is group writable
import pyufw as ufw

Enable the firewall

Enables the ufw firewall and enables on boot.

ufw.enable()

Disable the firewall

Disables the ufw firewall and disables on boot.

ufw.disable()

Reset the firewall

Returns the firewall to it's install defaults. incoming=deny, outgoing=allow, routed=reject
The default rules are:

  • allow SSH
  • allow to 224.0.0.251 app mDNS
ufw.reset()

Get status

Retuns a dict. Status is either 'active' or 'inactive'. If the firewall is active the default policies and rules list will also be included.

ufw.status()
{ 
   'status':'active',
   'default':{ 
      'incoming':'deny',
      'outgoing':'allow',
      'routed':'reject'
   },
   'rules':{ 
      1:'allow out on tun0',
      2:'allow in on tun0'
   }
}
{
    'status': 'inactive'
}

Set defaults

Set the default policies for incoming, outgoing and routed. Policies to choose from are allow, deny and reject.

ufw.default(incoming='deny', outgoing='allow', routed='reject')

Add rule

Add or Insert a rule. To insert a rule you can specify a rule number but this is optional.
Check out man ufw for rule syntax.
Returns the raw iptables rule added (incase your interested)

ufw.add("allow 22")
ufw.add("allow 22", number=3)
"allow -p all --dport 22 -j ACCEPT both"

Delete rule

Delete a rule. You can specify the rule itself, the rule number or the string * to delete all rules.

ufw.delete("allow 22")
ufw.delete(3)
ufw.delete('*')

Get rules

Get a list of the current rules. Returns a dict with the rule numbers as the index.

ufw.get_rules()
{ 
   1:'allow out on tun0',
   2:'allow in on tun0',
   3:'allow 22'
}

Show listening

Returns an array of listening ports, applications and rules that apply.
Array contains a series of tuples of the following structure:
(str transport, str listen_address, int listen_port, str application, dict rules)

ufw.show_listening()
[
    ('tcp', '*', '22', 'openssh', {
        3: 'allow 22'
    }), 
    ('tcp', '*', '57621', 'spotify', {}), 
    ('udp', '*', '1900', 'spotify', {}), 
    ('udp', '224.0.0.251', '5353', 'chrome', {}), 
    ('udp', '224.0.0.251', '5353', 'chrome', {}), 
    ('udp', '*', '68', 'dhclient', {})
]

Set Logging

Set the ufw logging level. Choose from: 'on', 'off', 'low', 'medium', 'high', 'full'. Check out man ufw for more info on logging.

ufw.set_logging('on')

Get raw iptables output

The following resources mirror the ufw cli commands and return the same unformatted string outputs. Maybe more useful for debugging.

ufw.show_raw()
ufw.show_builtins()
ufw.show_before_rules()
ufw.show_user_rules()
ufw.show_logging_rules()

Similar cool projects

pyufw's People

Contributors

5tingray avatar marcosbernal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.