Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polling the output from airodump-ng in Python

I am trying to create a python program that can periodically poll the output from airodump-ng, a wifi sniffing tool. I am doing this on a RPI running Raspbian and Python 3.4 I've looked up how to do this on several website but whenever I try this I get a sort of deadlock and the program stalls.

I am using this code:

import subprocess
airodump = subprocess.Popen(['sudo','airodump-ng','mon0'])
out,err = airodump.communicate(timeout=10)

So the weird thing is that when I type these commands one by one into IDLE running on the RPI, everything works but after 10 seconds I get a timeout error. When not using the timeout argument, the program simply stalls. Using the extra argument 'stdout=subprocess.PIPE' also doesn't work. But when I go to terminal and start up python using the command 'python3' and then typing in the first and second line, the whole screen is then filled with the output from airodump-ng and I cannot type anything anymore!

So how can I solve this? I just want to get the most recent output from airodump-ng and the output of airodum-ng can simply be updated in the background, in another thread. I just want the most recent output.

like image 688
Héctor van den Boorn Avatar asked Nov 25 '15 16:11

Héctor van den Boorn


People also ask

What is Airodump-ng command?

airdump-ng is used to list all the network around us and display useful information about them. It is a packet sniffer, so it is basically designed to capture all the packets around us while we are in Monitor mode.

What is Airodump-Ng in cyber security?

Airodump-ng detects wireless access points and the clients connected to them. This information is used by Aircrack-ng to hack the access points. Today, most organizations and public places have Wi-Fi, and this makes them ideal hunting grounds for ...

What is go Airmon?

Description. This script can be used to enable monitor mode on wireless interfaces. It may also be used to kill network managers, or go back from monitor mode to managed mode.

What are probes in Airodump?

Probes are the wireless networks airodump-ng is trying to connect if it is not still connected. If you see the probe field, it will display the ESSID of the network which is the name of the wireless network. Probe displays the names of those wireless networks airodump-ng is trying to connect to.


2 Answers

You can use pyrcrack, a python aircrack-ng bindings.

PyrCrack is a Python API exposing a common aircrack-ng API. As AircrackNg will run in background processes, and produce parseable output both in files and stdout, the most pythonic approach are context managers, cleaning up after.

Installation:

This library is available on Pypi, you can install it directly with pip:

pip install pyrcrack

Usage:

This library exports a basic aircrack-ng API aiming to keep always a small readable codebase.

This has led to a simple library that executes each of the aircrack-ng’s suite commands and auto-detects its usage instructions. Based on that, it dinamically builds classes inheriting that usage as docstring and a run() method that accepts keyword parameters and arguments, and checks them BEFORE trying to run them.

Some classes expose themselves as async iterators, as airodump-ng’s wich returns access points with its associated clients.

You can have a look at the examples/ folder for some usage examples, such as the basic “scan for targets”, that will list available interfaces, let you choose one, put it in monitor mode, and scan for targets updating results each 2 seconds.

import asyncio

import pyrcrack

from rich.console import Console
from rich.prompt import Prompt


async def scan_for_targets():
    """Scan for targets, return json."""
    console = Console()
    console.clear()
    console.show_cursor(False)
    airmon = pyrcrack.AirmonNg()

    interface = Prompt.ask(
        'Select an interface',
        choices=[a['interface'] for a in await airmon.interfaces])

    async with airmon(interface) as mon:
        async with pyrcrack.AirodumpNg() as pdump:
            async for result in pdump(mon.monitor_interface):
                console.clear()
                console.print(result.table)
                await asyncio.sleep(2)


asyncio.run(scan_for_targets())
like image 119
arVahedi Avatar answered Oct 03 '22 03:10

arVahedi


see the doc, works as intended, especially the Note

If the process does not terminate after timeout seconds, a TimeoutExpired exception will be raised. Catching this exception and retrying communication will not lose any output.

Note

The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

I would look at wifite code which make extensive use of airodump !

like image 35
euri10 Avatar answered Oct 03 '22 04:10

euri10