forked from zmap/zgrab2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
RicYaben
committed
Jul 15, 2024
1 parent
9509dcb
commit 571bf96
Showing
8 changed files
with
477 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package modules | ||
|
||
import "github.com/zmap/zgrab2/modules/mqtt" | ||
|
||
func init() { | ||
mqtt.RegisterModule() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Run this test with a Docker container running a standard broker to test the validity of the probe | ||
package mqtt | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/zmap/zgrab2" | ||
) | ||
|
||
type mqttTester struct { | ||
port int | ||
expectedStatus zgrab2.ScanStatus | ||
} | ||
|
||
func (t *mqttTester) getScanner() (*Scanner, error) { | ||
var module Module | ||
flags := module.NewFlags().(*Flags) | ||
flags.Port = uint(t.port) | ||
|
||
// Identifiers | ||
flags.ClientID = "testClient" // MQTT-specific | ||
flags.ClientRandom = "blabla" // on the TCP handshake | ||
|
||
// Client and user | ||
flags.SubscribeTopics = "#,$SYS/#" | ||
flags.SubscribeTimeout = 10 * time.Second | ||
flags.TopicsSeparator = "," | ||
|
||
// Attempt anonymous auth with | ||
// an empty user and password as the | ||
flags.UserAuth = true | ||
flags.Username = "" | ||
flags.Password = "" | ||
|
||
scanner := module.NewScanner() | ||
if err := scanner.Init(flags); err != nil { | ||
return nil, err | ||
} | ||
|
||
return scanner.(*Scanner), nil | ||
} | ||
|
||
func (t *mqttTester) runTest(test *testing.T, name string) { | ||
scanner, err := t.getScanner() | ||
if err != nil { | ||
test.Fatalf("[%s] Unexpected error: %v", name, err) | ||
} | ||
|
||
target := zgrab2.ScanTarget{ | ||
IP: net.ParseIP("127.0.0.1"), | ||
} | ||
|
||
status, ret, err := scanner.Scan(target) | ||
if status != t.expectedStatus { | ||
test.Errorf("[%s] Wrong status: expected %s, got %s", name, t.expectedStatus, status) | ||
} | ||
|
||
if err != nil { | ||
test.Errorf("[%s] Unexpected error: %v", name, err) | ||
} | ||
|
||
if ret == nil { | ||
test.Errorf("[%s] Got empty response", name) | ||
} | ||
} | ||
|
||
var tests = map[string]*mqttTester{ | ||
"success": { | ||
port: 1883, | ||
expectedStatus: zgrab2.SCAN_SUCCESS, | ||
}, | ||
} | ||
|
||
func TestMQTT(t *testing.T) { | ||
for tname, cfg := range tests { | ||
cfg.runTest(t, tname) | ||
} | ||
} |
Oops, something went wrong.