是否可以用python编写防火墙? [英] Is it possible to write a firewall in python?

查看:260
本文介绍了是否可以用python编写防火墙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用python编写防火墙?说会阻止所有流量吗?

Is it possible to write a firewall in python? Say it would block all traffic?

推荐答案

是的,是的.

我有一些Python代码,可以使用nfqueue与Linux iptables交互以执行防火墙职责.我可以在iptables中使用如下规则:

I have some Python code that interacts with Linux iptables to perform firewalling duties, using nfqueue. I can use a rule in iptables that looks like:

iptables -A INPUT -j NFQUEUE --queue-num 1

然后有一些类似以下的Python代码:

And then have some Python code that looks like:

import nfqueue
from dpkt import ip

q = None

def cb(dummy, payload):
    # make decision about if the packet should be allowed. in this case, drop everything:
    payload.set_verdict(nfqueue.NF_DROP)

q = nfqueue.queue()
q.open()
q.bind()
q.set_callback(cb)
q.create_queue(1)

q.try_run()

这是上面的代码基于的一个很好的文字:

Here is a nice write up that the above code is based on:

http://blog.yancomm.net/2011/05/nfqueue-packet-mangling-with-python.html

这篇关于是否可以用python编写防火墙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆