Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid send not dns packet through tcp #262

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/handlers/dns/DnsStreamHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ void DnsStreamHandler::process_udp_packet_cb(pcpp::Packet &payload, PacketDirect

void TcpSessionData::receive_dns_wire_data(const uint8_t *data, size_t len)
{
if (_invalid_data) {
return;
}

_buffer.append(reinterpret_cast<const char *>(data), len);

for (;;) {
Expand All @@ -193,6 +197,13 @@ void TcpSessionData::receive_dns_wire_data(const uint8_t *data, size_t len)
// dns packet size is in network byte order.
size = static_cast<unsigned char>(_buffer[1]) | static_cast<unsigned char>(_buffer[0]) << 8;

//if size is less than MIN_DNS_QUERY_SIZE, it is not a dns packet
if (size < MIN_DNS_QUERY_SIZE) {
_buffer.clear();
_invalid_data = true;
break;
}

if (_buffer.size() >= sizeof(size) + size) {
auto data = std::make_unique<uint8_t[]>(size);
std::memcpy(data.get(), _buffer.data() + sizeof(size), size);
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/dns/DnsStreamHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,13 @@ class TcpSessionData final
private:
std::string _buffer;
got_msg_cb _got_dns_msg;
bool _invalid_data;

public:
TcpSessionData(
got_msg_cb got_data_handler)
: _got_dns_msg{std::move(got_data_handler)}
, _invalid_data(false)
{
}

Expand Down