From 124cba56669db899043400e64f8ca89ede0ebdbc Mon Sep 17 00:00:00 2001 From: Juan Pablo Contreras Franco Date: Sun, 8 Oct 2023 17:10:53 +0200 Subject: [PATCH] Adds the scaffold for the HTTP 1.x reassembler example application. Adds the scaffold for the HTTP 1.x reassembler example application. --- Examples/CMakeLists.txt | 1 + Examples/HttpReassembler/CMakeLists.txt | 17 +++++ Examples/HttpReassembler/README.md | 14 ++++ Examples/HttpReassembler/main.cpp | 86 +++++++++++++++++++++++++ Tests/Packet++Test/Tests/HttpTests.cpp | 31 --------- 5 files changed, 118 insertions(+), 31 deletions(-) create mode 100644 Examples/HttpReassembler/CMakeLists.txt create mode 100644 Examples/HttpReassembler/README.md create mode 100644 Examples/HttpReassembler/main.cpp diff --git a/Examples/CMakeLists.txt b/Examples/CMakeLists.txt index d101bdd6e1..17946a2dd7 100644 --- a/Examples/CMakeLists.txt +++ b/Examples/CMakeLists.txt @@ -25,6 +25,7 @@ add_subdirectory(ArpSpoofing) add_subdirectory(DNSResolver) add_subdirectory(DnsSpoofing) add_subdirectory(HttpAnalyzer) +add_subdirectory(HttpReassembler) add_subdirectory(IcmpFileTransfer) add_subdirectory(IPDefragUtil) add_subdirectory(IPFragUtil) diff --git a/Examples/HttpReassembler/CMakeLists.txt b/Examples/HttpReassembler/CMakeLists.txt new file mode 100644 index 0000000000..3e8802a044 --- /dev/null +++ b/Examples/HttpReassembler/CMakeLists.txt @@ -0,0 +1,17 @@ +add_executable(HttpReassembler main.cpp) + +target_link_libraries(HttpReassembler PUBLIC PcapPlusPlus::Pcap++) + +if(MSVC) + # This executable requires getopt.h not available on VStudio + target_link_libraries(HttpReassembler PRIVATE Getopt-for-Visual-Studio) +endif() + +set_target_properties(HttpReassembler PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PCAPPP_BINARY_EXAMPLES_DIR}") + +if(PCAPPP_INSTALL) + install( + TARGETS HttpReassembler + EXPORT PcapPlusPlusTargets + RUNTIME DESTINATION ${PCAPPP_INSTALL_BINDIR}) +endif() diff --git a/Examples/HttpReassembler/README.md b/Examples/HttpReassembler/README.md new file mode 100644 index 0000000000..a31681cce9 --- /dev/null +++ b/Examples/HttpReassembler/README.md @@ -0,0 +1,14 @@ +HTTP Traffic Analyzer +===================== + +This application reassembless HTTP 1.x packets and generate a file from the payload. It read packets from a pcap/pcap-ng file. + +Using the utility (Work In Progress) +----------------- +When extracting HTTP traffic payload a pcap/pcap-ng file: + + Basic usage: + HttpAnalyzer [-h] -f input_file + Options: + -f : The input pcap file to analyze. Required argument for this mode + -h : Displays this help message and exits \ No newline at end of file diff --git a/Examples/HttpReassembler/main.cpp b/Examples/HttpReassembler/main.cpp new file mode 100644 index 0000000000..14161b15e1 --- /dev/null +++ b/Examples/HttpReassembler/main.cpp @@ -0,0 +1,86 @@ +/** + * HttpReassembler application + * ======================== + * This application reassembles HTTP payloads from captured packets as a text file. + */ + +#include +#include +#include "PcapPlusPlusVersion.h" +#include "SystemUtils.h" + +#define EXIT_WITH_ERROR(reason) do { \ + printUsage(); \ + std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \ + exit(1); \ + } while(0) + +static struct option HttpReassemblerOptions[] = +{ + {"help", no_argument, nullptr, 'h'}, + {"version", no_argument, nullptr, 'v'} +}; + +/** + * Print application usage + */ +void printUsage() +{ + std::cout << std::endl + << "Usage:" << std::endl + << "----------------------" << std::endl + << pcpp::AppName::get() << " [-vh]" << std::endl + << std::endl + << "Options:" << std::endl + << std::endl + << " -v : Displays the current version and exists" << std::endl + << " -h : Displays this help message and exits" << std::endl + << std::endl; +} + +/** + * Print application version + */ +void printAppVersion() +{ + std::cout + << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl + << "Built: " << pcpp::getBuildDateTime() << std::endl + << "Built from: " << pcpp::getGitInfo() << std::endl; + exit(0); +} + +/** + * Utility's main method + */ +int main(int argc, char* argv[]) +{ + pcpp::AppName::init(argc, argv); + + int optionIndex = 0; + int opt = 0; + + if (argc == 1) { // No options provided + printUsage(); + exit(0); + } + + while((opt = getopt_long(argc, argv, "hv", HttpReassemblerOptions, &optionIndex)) != -1) + { + switch (opt) + { + case 0: + break; + case 'h': + printUsage(); + exit(0); + break; + case 'v': + printAppVersion(); + break; + default: + printUsage(); + exit(-1); + } + } +} diff --git a/Tests/Packet++Test/Tests/HttpTests.cpp b/Tests/Packet++Test/Tests/HttpTests.cpp index dd648d4c18..d0694abaf4 100644 --- a/Tests/Packet++Test/Tests/HttpTests.cpp +++ b/Tests/Packet++Test/Tests/HttpTests.cpp @@ -536,36 +536,5 @@ PTF_TEST_CASE(HttpReassemblyTest) pcpp::HttpResponseLayer* responseLayer3 = frag3.getLayerOfType(); PTF_ASSERT_NOT_NULL(responseLayer3); -/* PTF_ASSERT_NOT_NULL(ipLayer); - PTF_ASSERT_TRUE(ipLayer->isFragment()); - PTF_ASSERT_TRUE(ipLayer->isFirstFragment()); - PTF_ASSERT_FALSE(ipLayer->isLastFragment()); - PTF_ASSERT_EQUAL(ipLayer->getFragmentOffset(), 0); - PTF_ASSERT_NOT_EQUAL((ipLayer->getFragmentFlags() & PCPP_IP_MORE_FRAGMENTS), 0); - PTF_ASSERT_NOT_NULL(ipLayer->getNextLayer()); - PTF_ASSERT_EQUAL(ipLayer->getNextLayer()->getProtocol(), pcpp::GenericPayload, enum); - - - ipLayer = frag2.getLayerOfType(); - PTF_ASSERT_NOT_NULL(ipLayer); - PTF_ASSERT_TRUE(ipLayer->isFragment()); - PTF_ASSERT_FALSE(ipLayer->isFirstFragment()); - PTF_ASSERT_FALSE(ipLayer->isLastFragment()); - PTF_ASSERT_EQUAL(ipLayer->getFragmentOffset(), 1480); - PTF_ASSERT_NOT_EQUAL((ipLayer->getFragmentFlags() & PCPP_IP_MORE_FRAGMENTS), 0); - PTF_ASSERT_NOT_NULL(ipLayer->getNextLayer()); - PTF_ASSERT_EQUAL(ipLayer->getNextLayer()->getProtocol(), pcpp::GenericPayload, enum); - - ipLayer = frag3.getLayerOfType(); - PTF_ASSERT_NOT_NULL(ipLayer); - PTF_ASSERT_TRUE(ipLayer->isFragment()); - PTF_ASSERT_FALSE(ipLayer->isFirstFragment()); - PTF_ASSERT_TRUE(ipLayer->isLastFragment()); - PTF_ASSERT_EQUAL(ipLayer->getFragmentOffset(), 2960); - PTF_ASSERT_EQUAL(ipLayer->getFragmentFlags(), 0); - PTF_ASSERT_NOT_NULL(ipLayer->getNextLayer()) - PTF_ASSERT_EQUAL(ipLayer->getNextLayer()->getProtocol(), pcpp::GenericPayload, enum); */ - - PTF_ASSERT_TRUE(true); } // HttpReassemblyTest