Browse Source

Merge branch 'us04-daemon-can-sniff-packets' into 'master'

US04: Daemon can sniff packets

See merge request tobias.wach/ccats!1
Pflanzer, Jonas 4 years ago
parent
commit
34650a6c49
4 changed files with 108 additions and 2 deletions
  1. 4 2
      daemon/CMakeLists.txt
  2. 66 0
      daemon/include/Sniffer.h
  3. 28 0
      daemon/src/Sniffer.cpp
  4. 10 0
      daemon/src/main.cpp

+ 4 - 2
daemon/CMakeLists.txt

@@ -1,13 +1,15 @@
 cmake_minimum_required(VERSION 2.8)
 
+set(CMAKE_CXX_STANDARD 11)
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 
 project(ccats)
 
-add_executable(ccats src/main.cpp)
+add_executable(ccats src/main.cpp src/Sniffer.cpp)
 
 find_package(Threads)
 find_package(Boost 1.67 REQUIRED COMPONENTS system)
+find_package(libtins 4.2 REQUIRED)
 
 include_directories(${Boost_INCLUDE_DIR})
-target_link_libraries(ccats PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
+target_link_libraries(ccats PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${TINS_LIBRARY} ${PCAP_LIBRARY})

+ 66 - 0
daemon/include/Sniffer.h

@@ -0,0 +1,66 @@
+#ifndef SNIFFER_H
+#define SNIFFER_H
+
+#include <tins/tins.h>
+
+/**
+ * @class Sniffer
+ *
+ * Sniffs the network.
+ *
+ * Sniffer class which will sniff on a network interface. It is supposed to forward the packets to an analyzer or
+ * modifyer so we can hide data in the traffic.
+ */
+class Sniffer {
+public:
+    /**
+     * Creates a Sniffer.
+     *
+     * Creates a Sniffer and sets the network interface for sniffing.
+     *
+     * @param interface name of the interface for sniffing
+     */
+    Sniffer(std::string interfaceName);
+
+    /**
+     * Destroys the Sniffer.
+     *
+     * Destructor of the Sniffer.
+     */
+    ~Sniffer();
+
+    /**
+     * Start sniffing on the interface.
+     *
+     * Starts a sniffing loop which calls handle. The loop will only be stopped if handle returns false.
+     */
+    void startSniffing();
+
+    /**
+     * Sets a filter for the sniffer.
+     *
+     * Sets the filter for a sniffer with a pcap filter string. E.g. "ip dst 8.8.8.8".
+     *
+     * @param filterString pcap filter string
+     */
+    void setFilter(std::string filterString);
+
+private:
+    /**
+     * Handler for sniffed packets.
+     *
+     * Handles incoming connections and provides data for the package analyzer and modifyer.
+     *
+     * @param pdu sniffed packet
+     *
+     * @return false = stop loop | true = continue loop
+     */
+    bool handle(Tins::PDU& pdu);
+
+    /**
+     * Tins sniffer object.
+     */
+    Tins::Sniffer sniffer;
+};
+
+#endif

+ 28 - 0
daemon/src/Sniffer.cpp

@@ -0,0 +1,28 @@
+#include "../include/Sniffer.h"
+#include <iostream>
+
+Sniffer::Sniffer(std::string interfaceName) : sniffer(interfaceName) {
+    Tins::SnifferConfiguration config;
+    config.set_promisc_mode(true);
+
+    sniffer = Tins::Sniffer(interfaceName, config);
+}
+
+Sniffer::~Sniffer() {
+}
+
+void Sniffer::startSniffing() {
+    sniffer.sniff_loop(make_sniffer_handler(this, &Sniffer::handle));
+}
+
+void Sniffer::setFilter(std::string filterString) {
+    sniffer.set_filter(filterString);
+}
+
+bool Sniffer::handle(Tins::PDU& pdu) {
+    // TODO implement handler for sniffed traffic
+
+    std::cout << "packet sniffed" << std::endl;
+
+    return false; // will stop sniffing after the first packet because this handler returns false
+}

+ 10 - 0
daemon/src/main.cpp

@@ -3,6 +3,8 @@
 #include <boost/bind.hpp>
 #include <boost/enable_shared_from_this.hpp>
 
+#include "../include/Sniffer.h"
+
 using namespace boost::asio;
 using ip::tcp;
 using std::cout;
@@ -72,6 +74,14 @@ public:
 
 
 int main(int argc, char *argv[]) {
+    if(argc < 2) {
+        std::cout << "Usage: " << argv[0] << " <interface>" << std::endl << std::endl;
+        return 0;
+    }
+
+    Sniffer sniffer(argv[1]);
+    sniffer.startSniffing();
+
   try {
     boost::asio::io_service io_service;  
     Server server(io_service);