nfqueue
和C的libipq比起来,支持python的nfqueue会显得强大很多,特别是和scapy结合起来用的时候。
首先需要说明的是在iptables中的target除了之前提到的五项(ACCEPT,DROP,RETURN,QUEUE,other_chain)之外,还有一个叫NFQUEUE
,它是QUEUE的扩展。相比于QUEUE,它可以由用户指定不同的queue number。
在使用nfqueue之前,需要安装如下的包:
$ sudo aptitude install libnetfilter-queue-dev
$ sudo aptitude install nfqueue-bindings-python
$ sudo aptitude install python-scapy
之后就可以采用python对NFQUEUE进行操作了。
假设我们将封包从主机A(192.168.1.1
)传输到主机B(192.168.1.2
)时,需要对封包进行分析,如果是TCP协议的包,并且其flags为 ACK|PSH 的话,则将其payload进行修改(比如替换成“hack”):
首先,需要先在主机A中对iptables进行操作:
$ sudo iptables -A OUTPUT -d 192.168.1.2 -p tcp -j NFQUEUE
然后利用下面的代码:
<span class="line-number">1</span> <span class="line-number">2</span> <span class="line-number">3</span> <span class="line-number">4</span> <span class="line-number">5</span> <span class="line-number">6</span> <span class="line-number">7</span> <span class="line-number">8</span> <span class="line-number">9</span> <span class="line-number">10</span> <span class="line-number">11</span> <span class="line-number">12</span> <span class="line-number">13</span> <span class="line-number">14</span> <span class="line-number">15</span> <span class="line-number">16</span> <span class="line-number">17</span> <span class="line-number">18</span> <span class="line-number">19</span> <span class="line-number">20</span> <span class="line-number">21</span> <span class="line-number">22</span> <span class="line-number">23</span> <span class="line-number">24</span> <span class="line-number">25</span> <span class="line-number">26</span> <span class="line-number">27</span> <span class="line-number">28</span> <span class="line-number">29</span> <span class="line-number">30</span> <span class="line-number">31</span> <span class="line-number">32</span> <span class="line-number">33</span> <span class="line-number">34</span> <span class="line-number">35</span> <span class="line-number">36</span> <span class="line-number">37</span> | |
这里用到了scapy
这个非常牛逼的模块,它可以直接通过如IP()
,TCP()
等直接对包进行解释和操作,非常方便,具体的可以参看它的文档。这里只是说明下它的安装方式:
$ wget scapy.net
$ mv index.html scapy-latest.zip
$ chmod +x scapy-latest.zip
$ mv scapy-latest.zip /usr/local/bin/scapy
然后就可以运行:
$ sudo scapy
直接开启scapy的交互模式了。
转载自:http://blog.csdn.net/langeldep/article/details/8788360