mfontanini / libtins

High-level, multiplatform C++ network packet sniffing and crafting library.
http://libtins.github.io/
BSD 2-Clause "Simplified" License
1.91k stars 375 forks source link

Crash (Segmentation fault) in application. #381

Closed stepen closed 4 years ago

stepen commented 4 years ago

First, thanks for a very useful library. However, I do experience a crash similar to what was explained in 'crash application #329', but not on windows. I'm running Linux on RaspberryPi4B / 4GB, OS ver 'buster'.

When I run the following code (essentially the example-code for a WPA2 decryptor).

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <tins/tins.h>

using namespace Tins;
using namespace std;
using namespace Crypto;

bool wpa2_callback(const PDU &pdu) 
{
    static int decrypted_pktcnt=0;

    cout << endl << "->WPA2 DECRYPTED PDU: " << decrypted_pktcnt++   << ":  type=" << pdu.pdu_type() << ", size=" << pdu.size() << endl;
    try
    {
       const Dot11Data &data = pdu.rfind_pdu<Dot11Data>();
       cout << " --- DATA FRAME ---" << endl;
       cout << "  data.src_addr: " << data.src_addr() << endl;
       cout << "  data.dst_addr: " << data.dst_addr() << endl;

       try {
            const IP &ip = pdu.rfind_pdu<IP>(); // non-const works as well
            cout << "   --- IP FRAME ---" << endl;
            cout << "    ip.src_addr: " << ip.src_addr() << endl;
            cout << "    ip.dst_addr: " << ip.dst_addr() << endl;
       }
       catch (Tins::pdu_not_found &e)
       {
            cout << "   -->PDU is no IP frame" << endl;;
       }
    }
    catch (Tins::pdu_not_found &e)
    {
        cout << "  -->PDU is no DATA frame" << endl;;
    }
    return true;
}

int main() 
{

    // Setting the relevant channel for my AP
    std::system("iwconfig wlan1 channel 9");  

    SnifferConfiguration config;
    config.set_promisc_mode(true);
    config.set_rfmon(true);

    // Make a simple WPA2 decryptor
    auto decrypt_proxy = make_wpa2_decrypter_proxy(&wpa2_callback);

    decrypt_proxy.decrypter().add_ap_data("XXXXXXXXXXXX", "TheSSID");   

    Sniffer sniffer("wlan1", config);  
    cout << "Starting the proxy decryptor" << endl;
    sniffer.sniff_loop(decrypt_proxy);

}

I get:

Starting the proxy decryptor

->WPA2 DECRYPTED PDU: 0:  type=0, size=9
(Crash!!)

It starts fine, but immediately crashes/seg-faults when the pdu.rfind_pdu<Dot11Data>() is executed. Strangely this only occurs when linking with LIBTINS 4.3, 4.2, but it works just fine when linking with 4.1 ??

Notes:

Does this mean anything to you? Is it a bug or am I doing something wrong here? (wouldn't be the first - and probably not the last).

Thanks in advance stepen

stepen commented 4 years ago

UPDATE My Bad. I think this is not an issue after all. Looks like my compiling is wrong. The problem is caused by a pre-installed version (4.0) of libtins (obtained with 'apt-get install libtins-dev'). I have the v/4.0 include files in my path. Unfortunately, they are being used instead of the one in the 4.1/4.3 release. The compile will work, but something wicked will be built for versions 4.2, and 4.3.

My compile command should have been (if .h files are in '/home/pi/libtins_4.X/include')

   g++ -I. -I/home/pi/libtins_4.X/include -std=c++11 -Wall -O3 -g3 -c -fmessage-length=0 ip_decryptor_test1.cpp

Also, I should probably have baked the lib-path (rpath) into the ELF during linking:

   g++  -o ip_decryptor_test1 -Wl,-rpath,/home/pi/libtins_4.X /home/pi/libtins_4.X/libtins.so ip_decryptor_test1.o -lpthread -ldl -lssl -lcrypto