码迷,mamicode.com
首页 > 编程语言 > 详细

Python Ethical Hacking - Intercepting and Modifying Packets

时间:2019-09-01 16:16:08      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:payload   fun   env   OWIN   style   cep   import   pytho   tput   

INTERCEPTING & MODIFYING PACKETS

Scapy can be used to:

  • Create packets.
  • Analyze packets.
  • Send/receive packets.

But it can‘t be used to intercept packets/flows.

CLASSIC MITM SCENARIO

技术图片

 

 

 MITM - SNIFFING DATA

技术图片

 

 

 MITM - MODIFYING DATA

技术图片

 

 

 

技术图片

 

 

 技术图片

 

 

 

 技术图片

 

 

 1. Execute the command - iptables to capture the packets into a queue.

iptables -I INPUT -d 10.0.0.0/24 -j NFQUEUE --queue-num 1

技术图片

 

 2. Access the Packets queue.

Install the module netfilterqueue first.

pip3 install -U git+https://github.com/kti/python-netfilterqueue

技术图片

 

 

 

3. Write the Python script to intercept and process the packets.

#!/usr/bin/env python
from netfilterqueue import NetfilterQueue


def process_packet(packet):
    print(packet)
    packet.accept()


queue = NetfilterQueue()
queue.bind(1, process_packet)
try:
    queue.run()
except KeyboardInterrupt:
    print(‘‘)

技术图片

 

 We can also drop the packets through function packet.drop().

4. Use the following command to stop the packet capturing.

iptables --flush

 

 

Converting Packets to Scapy Packets

1. Execute the iptables command to capture the OUTPUT and INPUT packets.

iptables -I OUTPUT -j NFQUEUE --queue-num 0

iptables -I INPUT -j NFQUEUE --queue-num 0

技术图片

 

 2. Execute the following Python script to process the captured packets.

#!/usr/bin/env python
from netfilterqueue import NetfilterQueue


def process_packet(packet):
    print(packet)
    packet.accept()


queue = NetfilterQueue()
queue.bind(0, process_packet)
try:
    queue.run()
except KeyboardInterrupt:
    print(‘‘)

技术图片

 

 3. Convert the packet to scapy packet and show on the screen.

#!/usr/bin/env python

from netfilterqueue import NetfilterQueue
from scapy.layers.inet import IP


def process_packet(packet):
    scapy_packet = IP(packet.get_payload())
    print(scapy_packet.show())
    packet.accept()


queue = NetfilterQueue()
queue.bind(0, process_packet)
try:
    queue.run()
except KeyboardInterrupt:
    print(‘‘)

技术图片

 

 4. Stop the capture of the packet by the command.

iptables --flush

 

Python Ethical Hacking - Intercepting and Modifying Packets

标签:payload   fun   env   OWIN   style   cep   import   pytho   tput   

原文地址:https://www.cnblogs.com/keepmoving1113/p/11442423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!