Skip to content

Commit

Permalink
append some characters to the client ID
Browse files Browse the repository at this point in the history
  • Loading branch information
RicYaben committed Nov 15, 2024
1 parent 5485c73 commit 45bd302
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions modules/mqtt/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mqtt

import (
"context"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand All @@ -10,7 +11,6 @@ import (
"sync"

paho "github.com/eclipse/paho.mqtt.golang"
"github.com/google/uuid"
"github.com/zmap/zgrab2"
)

Expand Down Expand Up @@ -60,13 +60,33 @@ func (s *scan) getBrokerURL() string {
return fmt.Sprintf("%s://%s:%d", s.scheme, s.target.Host(), *port)
}

func (s *scan) getClientOptions() (*paho.ClientOptions, error) {
// we may jump into the same host. Avoid weird issues
id := uuid.New()
uid := fmt.Sprintf("%s-%s", s.scanner.config.ClientID, id[:6])
func (s *scan) randomizeClientID() string {
const (
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
maxLen = 23 // as defined by MQTT 3.1
)

if len(s.scanner.config.ClientID) > maxLen {
return s.scanner.config.ClientID[:maxLen]
}

var (
suffix []byte
cap = maxLen - len(s.scanner.config.ClientID)
)

for i := 0; i < cap; i++ {
r := make([]byte, 1)
rand.Read(r)
suffix = append(suffix, charset[r[0]%byte(len(charset))])
}
return s.scanner.config.ClientID + string(suffix)
}

func (s *scan) getClientOptions() (*paho.ClientOptions, error) {
id := s.randomizeClientID()
opts := paho.NewClientOptions().
SetClientID(uid).
SetClientID(id).
SetCleanSession(true).
SetAutoReconnect(true).
SetOrderMatters(false)
Expand Down

0 comments on commit 45bd302

Please sign in to comment.