使用 libtins 重新创建 IP 数据包更改其 header
Using libtins to recreate IP packet change its header
我有一个 uint8_t*
缓冲区,可以通过 buf.data()
获得。它是一个IP数据包的缓冲区。
我想更改源地址并重新计算校验和,因此我按照您在下面看到的操作在 libtins 中重新创建了数据包:
//prints original packet
for (size_t i = 0; i < buf.size(); i++)
{
printf("%x ", buf.data()[i]);
}
printf("\n");
Tins::IP pa = Tins::IP(buf.data(), buf.size());
pa.src_addr(newIpv4address);
uint8_t *packetData = pa.serialize().data();
//prints libtins packet created from original packet
for (size_t i = 0; i < buf.size(); i++)
{
printf("%x ", packetData[i]);
}
printf("\n");
问题是从 libtins 打印的数据包出现了不同的 header:
45 0 0 34 0 0 40 0 40 6 6b 53 c0 a8 45 1 ac d9 1c ee 0 20 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 a5 4d 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
60 b 0 34 62 76 0 0 d0 8 0 28 62 76 0 0 ac d9 1c ee 0 20 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 eb 47 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
然后我尝试用新数据包更改 buf
,即使它看起来不同:
uint8_t* b = buf.data();
uint8_t* o = pa.serialize().data();
for (int i = 0; i < buf.size(); i++)
{
b[i] = o[i];
}
auto p = Tins::IP(buf.data(), buf.size());
但是在 auto p = Tins::IP(buf.data(), buf.size());
行我得到:
terminate called after throwing an instance of 'Tins::malformed_packet'
what(): Malformed packet
这很奇怪,因为它实际上是一个从 libtins 生成的数据包。
我做错了什么?根据 libtins documentation 我可以从缓冲区和大小创建一个 libtins 数据包。
更新:
#include <iostream>
#include <memory>
#include <tins/ip.h>
int main()
{
const int packet_size = 52;
uint8_t ip_packet[] = {
69, 0, 0, 52, 0, 0, 64, 0, 64, 6, 107,
83, 192, 168, 69, 1, 172, 217, 28, 238,
0, 112, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 128,
2, 253, 232, 164, 253, 0, 0, 2, 4, 5, 180,
3, 3, 0, 4, 2, 0, 0, 0};
std::cout << "packet BEFORE " << std::endl;
for (size_t i = 0; i < packet_size; i++)
{
printf("%x ", ip_packet[i]);
}
uint8_t b[52];
for (int i = 0; i < packet_size; i++)
{
b[i] = ip_packet[i];
}
printf("\n");
Tins::IP pa = Tins::IP(b, packet_size);
pa.src_addr("172.78.123.76");
uint8_t *packetData = pa.serialize().data();
std::cout << "TINS packet: " << std::endl;
for (size_t i = 0; i < packet_size; i++)
{
printf("%x ", packetData[i]);
}
printf("\n");
return 0;
}
输出:
packet BEFORE
45 0 0 34 0 0 40 0 40 6 6b 53 c0 a8 45 1 ac d9 1c ee 0 70 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 a4 fd 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
TINS packet:
0 0 0 0 0 0 0 0 10 90 68 1 0 0 0 0 ac d9 1c ee 0 70 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 83 c 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
使用您提供的示例 IP 数据包,我完成了以下操作:
uint8_t ip_packet[] = {
69, 0, 0, 52, 0, 0, 64, 0,
64, 6, 107, 83, 192, 168, 69, 1,
172, 217, 28, 238, 0, 112, 0, 80,
0, 0, 0, 0, 0, 0, 0, 0,
128, 2, 253, 232, 164, 253, 0, 0,
2, 4, 5, 180, 3, 3, 0, 4,
2, 0, 0, 0
};
Tins::IP ip(ip_packet, sizeof(ip_packet));
for (std::size_t i = 0; i < sizeof(ip_packet); i++)
std::cout << +ip_packet[i] << " ";
std::cout << std::endl;
ip.src_addr("192.155.32.10");
auto data = ip.serialize();
for (std::size_t i = 0; i < data.size(); i++)
std::cout << +data[i] << " ";
std::cout << std::endl;
我得到以下输出:
69 0 0 52 0 0 64 0 64 6 107 83 192 168 69 1 172 217 28 238 0 112 0 80 0 0 0 0 0 0 0 0 128 2 253 232 164 253 0 0 2 4 5 180 3 3 0 4 2 0 0 0
69 0 0 52 0 0 64 0 64 6 144 87 192 155 32 10 172 217 28 238 0 112 0 80 0 0 0 0 0 0 0 0 128 2 253 232 202 1 0 0 2 4 5 180 3 3 0 4 2 0 0 0
所以它按预期工作。
我有一个 uint8_t*
缓冲区,可以通过 buf.data()
获得。它是一个IP数据包的缓冲区。
我想更改源地址并重新计算校验和,因此我按照您在下面看到的操作在 libtins 中重新创建了数据包:
//prints original packet
for (size_t i = 0; i < buf.size(); i++)
{
printf("%x ", buf.data()[i]);
}
printf("\n");
Tins::IP pa = Tins::IP(buf.data(), buf.size());
pa.src_addr(newIpv4address);
uint8_t *packetData = pa.serialize().data();
//prints libtins packet created from original packet
for (size_t i = 0; i < buf.size(); i++)
{
printf("%x ", packetData[i]);
}
printf("\n");
问题是从 libtins 打印的数据包出现了不同的 header:
45 0 0 34 0 0 40 0 40 6 6b 53 c0 a8 45 1 ac d9 1c ee 0 20 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 a5 4d 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
60 b 0 34 62 76 0 0 d0 8 0 28 62 76 0 0 ac d9 1c ee 0 20 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 eb 47 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
然后我尝试用新数据包更改 buf
,即使它看起来不同:
uint8_t* b = buf.data();
uint8_t* o = pa.serialize().data();
for (int i = 0; i < buf.size(); i++)
{
b[i] = o[i];
}
auto p = Tins::IP(buf.data(), buf.size());
但是在 auto p = Tins::IP(buf.data(), buf.size());
行我得到:
terminate called after throwing an instance of 'Tins::malformed_packet'
what(): Malformed packet
这很奇怪,因为它实际上是一个从 libtins 生成的数据包。
我做错了什么?根据 libtins documentation 我可以从缓冲区和大小创建一个 libtins 数据包。
更新:
#include <iostream>
#include <memory>
#include <tins/ip.h>
int main()
{
const int packet_size = 52;
uint8_t ip_packet[] = {
69, 0, 0, 52, 0, 0, 64, 0, 64, 6, 107,
83, 192, 168, 69, 1, 172, 217, 28, 238,
0, 112, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 128,
2, 253, 232, 164, 253, 0, 0, 2, 4, 5, 180,
3, 3, 0, 4, 2, 0, 0, 0};
std::cout << "packet BEFORE " << std::endl;
for (size_t i = 0; i < packet_size; i++)
{
printf("%x ", ip_packet[i]);
}
uint8_t b[52];
for (int i = 0; i < packet_size; i++)
{
b[i] = ip_packet[i];
}
printf("\n");
Tins::IP pa = Tins::IP(b, packet_size);
pa.src_addr("172.78.123.76");
uint8_t *packetData = pa.serialize().data();
std::cout << "TINS packet: " << std::endl;
for (size_t i = 0; i < packet_size; i++)
{
printf("%x ", packetData[i]);
}
printf("\n");
return 0;
}
输出:
packet BEFORE
45 0 0 34 0 0 40 0 40 6 6b 53 c0 a8 45 1 ac d9 1c ee 0 70 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 a4 fd 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
TINS packet:
0 0 0 0 0 0 0 0 10 90 68 1 0 0 0 0 ac d9 1c ee 0 70 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 83 c 0 0 2 4 5 b4 3 3 0 4 2 0 0 0
使用您提供的示例 IP 数据包,我完成了以下操作:
uint8_t ip_packet[] = {
69, 0, 0, 52, 0, 0, 64, 0,
64, 6, 107, 83, 192, 168, 69, 1,
172, 217, 28, 238, 0, 112, 0, 80,
0, 0, 0, 0, 0, 0, 0, 0,
128, 2, 253, 232, 164, 253, 0, 0,
2, 4, 5, 180, 3, 3, 0, 4,
2, 0, 0, 0
};
Tins::IP ip(ip_packet, sizeof(ip_packet));
for (std::size_t i = 0; i < sizeof(ip_packet); i++)
std::cout << +ip_packet[i] << " ";
std::cout << std::endl;
ip.src_addr("192.155.32.10");
auto data = ip.serialize();
for (std::size_t i = 0; i < data.size(); i++)
std::cout << +data[i] << " ";
std::cout << std::endl;
我得到以下输出:
69 0 0 52 0 0 64 0 64 6 107 83 192 168 69 1 172 217 28 238 0 112 0 80 0 0 0 0 0 0 0 0 128 2 253 232 164 253 0 0 2 4 5 180 3 3 0 4 2 0 0 0
69 0 0 52 0 0 64 0 64 6 144 87 192 155 32 10 172 217 28 238 0 112 0 80 0 0 0 0 0 0 0 0 128 2 253 232 202 1 0 0 2 4 5 180 3 3 0 4 2 0 0 0
所以它按预期工作。