1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-30 05:24:26 +01:00

Add active tests for IPv4

This commit is contained in:
Matias Fontanini
2016-03-20 16:27:43 -07:00
parent a70ce10bed
commit 5dc7b20a43
15 changed files with 865 additions and 2 deletions

View File

@@ -0,0 +1,17 @@
FILE(GLOB SOURCES "*.cpp")
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}/../include
)
# Find pthread library
FIND_PACKAGE(Threads REQUIRED)
# Link against GoogleTest, libtins and pthread.
LINK_LIBRARIES(
tins
${CMAKE_THREAD_LIBS_INIT}
${PCAP_LIBRARY}
)
ADD_EXECUTABLE(active_tester EXCLUDE_FROM_ALL ${SOURCES})

View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <iostream>
#include "tins/exceptions.h"
#include "tins/pdu.h"
#include "active_test.h"
using std::cout;
using std::endl;
using Tins::PDU;
using Tins::pdu_not_found;
using Tins::option_not_found;
ActiveTest::ActiveTest(const PacketSenderPtr& packet_sender,
const ConfigurationPtr& configuration)
: packet_sender_(packet_sender), configuration_(configuration) {
}
void ActiveTest::execute() {
execute_test();
}
bool ActiveTest::matches_packet(const PDU& pdu) const {
try {
return test_matches_packet(pdu);
}
catch (pdu_not_found&) {
return false;
}
catch (option_not_found&) {
return false;
}
}
const ActiveTest::PacketSenderPtr& ActiveTest::packet_sender() const {
return packet_sender_;
}
const ActiveTest::ConfigurationPtr& ActiveTest::configuration() const {
return configuration_;
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <set>
#include <algorithm>
#include <iostream>
#include <chrono>
#include <stdexcept>
#include "active_test_runner.h"
using std::make_shared;
using std::set;
using std::string;
using std::exception;
using std::cout;
using std::cerr;
using std::endl;
using std::this_thread::sleep_for;
using std::chrono::seconds;
using Tins::PacketSender;
ActiveTestRunner::ActiveTestRunner(const Configuration& configuration)
: configuration_(make_shared<Configuration>(configuration)),
packet_sender_(make_shared<PacketSender>()),
capturer_(*configuration_) {
packet_sender_->default_interface(configuration.interface());
}
bool ActiveTestRunner::validate_tests() {
set<string> names;
for (const auto& test : tests_) {
if (names.insert(test->name()).second == false) {
return false;
}
}
return true;
}
void ActiveTestRunner::run() {
try {
do_run();
}
catch (exception& ex) {
cerr << "[-] Caught exception while running: " << ex.what() << endl;
}
}
void ActiveTestRunner::do_run() {
string prefix = "[runner] ";
cout << prefix << "Starting capture on interface " << configuration_->interface() << endl;
capturer_.start_capture();
cout << prefix << "Executing " << tests_.size() << " tests" << endl;
for (auto& test : tests_) {
cout << prefix << "Sending packet for " << test->name() << " test" << endl;
test->execute();
}
cout << prefix << "Done executing tests. Sleeping for a second" << endl;
sleep_for(seconds(1));
cout << prefix << "Stopping capture" << endl;
capturer_.stop_capture();
cout << prefix << "Capture stopped" << endl;
auto packets = capturer_.captured_packets();
cout << prefix << "Captured " << packets.size() << " packets" << endl;
for (const auto& test : tests_) {
prefix = "[" + test->name() + "] ";
size_t i = 0;
while (i < packets.size() && !test->matches_packet(*packets[i])) {
++i;
}
if (i == packets.size()) {
cout << prefix << "ERROR: Packet was not captured" << endl;
}
else {
try {
test->validate_packet(*packets[i]);
cout << prefix << "OK" << endl;
}
catch (TestFailed& ex) {
cout << prefix << "ERROR: " << ex.what() << endl;
}
packets.erase(packets.begin() + i);
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "configuration.h"
using std::string;
using Tins::NetworkInterface;
void Configuration::interface(const NetworkInterface& interface) {
interface_ = interface;
}
void Configuration::source_port(uint16_t value) {
source_port_ = value;
}
void Configuration::destination_port(uint16_t value) {
destination_port_ = value;
}
const NetworkInterface& Configuration::interface() const {
return interface_;
}
uint16_t Configuration::source_port() const {
return source_port_;
}
uint16_t Configuration::destination_port() const {
return destination_port_;
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sstream>
#include "ipv4_tests.h"
#include "tins/rawpdu.h"
#include "tins/ethernetII.h"
#include "tins/ip.h"
#include "tins/udp.h"
#include "tins/network_interface.h"
using std::string;
using std::ostringstream;
using Tins::IP;
using Tins::UDP;
using Tins::PDU;
using Tins::RawPDU;
using Tins::EthernetII;
using Tins::PacketSender;
using Tins::NetworkInterface;
// Source address test
string IPv4SourceAddressTest::name() const {
return "ipv4_source_address";
}
void IPv4SourceAddressTest::execute_test() {
PacketSender& sender = *packet_sender();
Configuration& config = *configuration();
auto packet = IP("8.8.8.8");
packet /= UDP(config.destination_port(), config.source_port());
packet /= RawPDU(name());
sender.send(packet);
}
void IPv4SourceAddressTest::validate_packet(const PDU& pdu) {
const IP& ip = pdu.rfind_pdu<IP>();
const NetworkInterface& iface = configuration()->interface();
// The source address should be the same as the default interface's
if (iface.ipv4_address() != ip.src_addr()) {
ostringstream oss;
oss << "Source address should be " << iface.ipv4_address()
<< " but is " << ip.src_addr();
throw TestFailed(oss.str());
}
}
bool IPv4SourceAddressTest::test_matches_packet(const PDU& pdu) const {
const string& test_name = name();
RawPDU::payload_type expected_payload(test_name.begin(), test_name.end());
return pdu.rfind_pdu<RawPDU>().payload() == expected_payload;
}
// Fragmentation test
string IPv4FragmentationTest::name() const {
return "ipv4_fragmentation";
}
void IPv4FragmentationTest::execute_test() {
PacketSender& sender = *packet_sender();
Configuration& config = *configuration();
auto packet = IP("8.8.8.8");
packet /= UDP(config.destination_port(), config.source_port());
packet /= RawPDU(name());
IP& ip = packet.rfind_pdu<IP>();
ip.fragment_offset(100);
ip.flags(IP::MORE_FRAGMENTS);
sender.send(packet);
}
void IPv4FragmentationTest::validate_packet(const PDU& pdu) {
ostringstream oss;
const IP& ip = pdu.rfind_pdu<IP>();
if (ip.fragment_offset() != 100) {
oss << "Expected fragment offset 100 but got " << ip.fragment_offset();
throw TestFailed(oss.str());
}
if (ip.flags() != IP::MORE_FRAGMENTS) {
oss << "Expected MORE_FRAGMENT flags but got " << (unsigned)ip.flags();
throw TestFailed(oss.str());
}
}
bool IPv4FragmentationTest::test_matches_packet(const PDU& pdu) const {
string test_name = name();
const RawPDU& raw = pdu.rfind_pdu<RawPDU>();
string payload(raw.payload().begin(), raw.payload().end());
return payload.find(test_name) != string::npos;
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <iostream>
#include <stdexcept>
#include "configuration.h"
#include "active_test_runner.h"
#include "ipv4_tests.h"
#include "tins/network_interface.h"
using std::cerr;
using std::endl;
using std::exception;
using std::runtime_error;
using Tins::NetworkInterface;
int main() {
try {
Configuration config;
config.source_port(1234);
config.destination_port(4321);
config.interface(NetworkInterface::default_interface());
ActiveTestRunner runner(config);
runner.add_test<IPv4SourceAddressTest>();
runner.add_test<IPv4FragmentationTest>();
if (!runner.validate_tests()) {
throw runtime_error("Test validation failed");
}
runner.run();
}
catch (exception& ex) {
cerr << "Error: " << ex.what() << endl;
return 1;
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (c) 2016, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sstream>
#include <functional>
#include <mutex>
#include <condition_variable>
#include "packet_capturer.h"
using std::string;
using std::thread;
using std::move;
using std::bind;
using std::condition_variable;
using std::unique_lock;
using std::lock_guard;
using std::mutex;
using std::ostringstream;
using Tins::PDU;
using Tins::Sniffer;
using Tins::SnifferConfiguration;
PacketCapturer::PacketCapturer(const Configuration& configuration) {
SnifferConfiguration sniffer_config;
sniffer_config.set_filter(make_filter(configuration));
sniffer_config.set_immediate_mode(true);
sniffer_.reset(new Sniffer(configuration.interface().name(), sniffer_config));
}
void PacketCapturer::start_capture() {
using std::placeholders::_1;
mutex mtx;
condition_variable cond;
bool started = false;
running_ = true;
sniffer_thread_ = thread([&]() {
{
lock_guard<mutex> _(mtx);
started = true;
cond.notify_one();
}
sniffer_->sniff_loop(bind(&PacketCapturer::callback, this, _1));
});
unique_lock<mutex> locker(mtx);
while (!started) {
cond.wait(locker);
}
}
void PacketCapturer::stop_capture() {
running_ = false;
sniffer_->stop_sniff();
sniffer_thread_.join();
}
PacketCapturer::PacketStorage PacketCapturer::captured_packets() {
return move(storage_);
}
bool PacketCapturer::callback(const PDU& pdu) {
storage_.emplace_back(pdu.clone());
return running_;
}
string PacketCapturer::make_filter(const Configuration& configuration) const {
ostringstream oss;
oss << "((tcp or udp) and src port " << configuration.source_port()
<< " and dst port " << configuration.destination_port() << ") or icmp"
// Fragmentted IP packets
<< " or (ip[6:2] & 0x1fff) > 0";
return oss.str();
}