Skip to content

Add length checks to natneg handleReport and qr2 messages #82

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion natneg/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"github.com/logrusorgru/aurora/v3"
)

func (session *NATNEGSession) handleReport(conn net.PacketConn, addr net.Addr, buffer []byte, _ string, version byte) {
func (session *NATNEGSession) handleReport(conn net.PacketConn, addr net.Addr, buffer []byte, _moduleName string, version byte) {
if len(buffer) < 2 {
logging.Error(_moduleName, "Invalid packet size")
return
}

response := createPacketHeader(version, NNReportReply, session.Cookie)
response = append(response, buffer[:9]...)
response[14] = 0
Expand Down
5 changes: 3 additions & 2 deletions qr2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func SendClientMessage(senderIP string, destSearchID uint64, message []byte) {

// Decode and validate the message
isNatnegPacket := false
if bytes.Equal(message[:2], []byte{0xfd, 0xfc}) {
if len(message) >= 2 && bytes.Equal(message[:2], []byte{0xfd, 0xfc}) {
// Sending natneg cookie
isNatnegPacket = true
if len(message) != 0xA {
Expand All @@ -69,7 +69,7 @@ func SendClientMessage(senderIP string, destSearchID uint64, message []byte) {

natnegID := binary.LittleEndian.Uint32(message[0x6:0xA])
moduleName = "QR2/MSG:s" + strconv.FormatUint(uint64(natnegID), 10)
} else if bytes.Equal(message[:4], []byte{0xbb, 0x49, 0xcc, 0x4d}) || bytes.Equal(message[:4], []byte("SBCM")) {
} else if len(message) >= 4 && (bytes.Equal(message[:4], []byte{0xbb, 0x49, 0xcc, 0x4d}) || bytes.Equal(message[:4], []byte("SBCM"))) {
// DWC match command
if len(message) < 0x14 || len(message) > 0x94 {
logging.Error(moduleName, "Received invalid length match command packet")
Expand Down Expand Up @@ -219,6 +219,7 @@ func SendClientMessage(senderIP string, destSearchID uint64, message []byte) {
}
} else {
logging.Error(moduleName, "Invalid message:", aurora.Cyan(printHex(message)))
return
}

destSessionID, packetCount, destAddr := processClientMessage(moduleName, sender, receiver, message, isNatnegPacket, matchData)
Expand Down