我在Linux上的C++项目中使用libtins来创建pcap。我从代码中获得的pcap似乎无法被Wireshark、tcp转储或我用来尝试使用libtins读取代码的示例所读取。
我需要我的程序产生由wireshark可读的输出,也可以通过我的项目中的代码读取它。
编辑:
相关守则:
bool packetHandler(const PDU &pdu) {
const IP &ip = pdu.rfind_pdu<IP>();
return true;
cout << ip.src_addr() << " -> " << ip.dst_addr() << endl;
return true;
}
int main(int argc, char** argv)
{
if(getuid() != 0)
{
printf("You must run this program as root!\n");
exit(1);
}
try
{
std::string pcap_path = "/tmp/test.pcap";
std::string device = "eth0";
printf("Filepath: %s\n", pcap_path.c_str());
PacketWriter writer(pcap_path, DataLinkType<EthernetII>());
std::vector<EthernetII> vec(1000, EthernetII(hwaddr(device)));
writer.write(vec.begin(), vec.end());
writer.write(vec[0]);
} catch(Tins::unknown_link_type e)
{
printf("ERROR:\t%s\n", e.what());
} catch(std::runtime_error e)
{
printf("ERROR:\t%s\n", e.what());
}
}我也尝试过使用这段代码来读取pcap,但是它没有输出任何内容:
#include <tins/tins.h>
using namespace Tins;
using namespace std;
bool packetHandler(PDU &pdu)
{
// Find the IP layer
const IP &ip = pdu.rfind_pdu<IP>();
cout << ip.src_addr() << " -> " << ip.dst_addr() << endl;
return true;
}
int main() {
FileSniffer sniffer("/tmp/test.pcap");
sniffer.sniff_loop(packetHandler);
}编辑。再来一次
正如您从wireshark所看到的,我得到了每个字段和数据的不正确值,这些值都是0。https://i.imgur.com/wfCnaaA.png (sry我不能嵌入图像,因为我在这里没有10个声誉点)。我需要能够在wireshark上的正确字段中看到IP地址、数据等,但我没有得到正确的数据。
发布于 2022-11-02 23:33:26
我才刚开始学习libtin。在本教程中,我了解了如何捕获数据包:https://libtins.github.io/tutorial/sniffing/
#include <tins/tins.h>
#include <iostream>
#include <typeinfo>
using namespace Tins;
using namespace std;
/*
Compile
g++ loop_sniffing_simple.cpp -o loop_sniffing_simple.o -O3 -std=c++11 -lpthread -ltins
Run
sudo ./loop_sniffing_simple.o
From other machine (192.168.72.57) send something
*/
void test() {
SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_filter("ip host 192.168.72.57");
Sniffer sniffer("wlp2s0", config);
// sniffer.sniff_loop(doo);
PacketWriter writer = PacketWriter("sniffer.pcap", DataLinkType<IP>());
int n_packet = 0;
const int totalPackets = 4;
for (auto &packet : sniffer) {
auto pdu = packet.release_pdu();
IP ip = pdu->rfind_pdu<IP>();
cout << "Destination IP: " << ip.dst_addr()
<< " source IP: " << ip.src_addr() << endl;
// cout << typeid(ip).name() << endl;
// writer.write(pdu->rfind_pdu<IP>());
writer.write(ip);
++n_packet;
if (n_packet == totalPackets) {
break;
}
}
}
int main() {
test();
return 0;
}最后,您将得到一个正确的pcap。
另一个例子,我想这样做更好:
#include <vector>
#include <tins/tins.h>
#include <iostream>
using namespace Tins;
using namespace std;
/*
Compile
g++ packet_objects.cpp -o packet_objects.o -O3 -std=c++11 -lpthread -ltins
*/
int main() {
vector<Packet> vt;
PacketWriter writer = PacketWriter("sniffer_obj.pcap", DataLinkType<IP>());
Sniffer sniffer("wlp2s0");
while (vt.size() != 10) {
// next_packet returns a PtrPacket, which can be implicitly converted to Packet.
vt.push_back(sniffer.next_packet());
}
// Done, now let's cehck the packets
for (const auto& packet : vt) {
// Is there an IP PDU somewhere?
if(packet.pdu()->find_pdu<IP>()) {
// Just print timestamp's seconds and IP source address
cout << "At: " << packet.timestamp().seconds()
<< " - " << packet.pdu()->rfind_pdu<IP>().src_addr()
<< std::endl;
IP ip = packet.pdu()->rfind_pdu<IP>();
writer.write(ip);
}
}
return 0;
}https://stackoverflow.com/questions/51586054
复制相似问题