Skip to content

Commit

Permalink
Merge pull request #16 from praetorian-inc/fix-ldap-fingerprint-bug
Browse files Browse the repository at this point in the history
Updated Fingerprintx Unit Tests
  • Loading branch information
UNC1739 committed Apr 12, 2023
2 parents 7434175 + 2646995 commit 312cf79
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/plugins/services/echo/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2022 Praetorian Security, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package echo

import (
"bytes"
"crypto/rand"
"net"
"time"

"github.com/praetorian-inc/fingerprintx/pkg/plugins"
"github.com/praetorian-inc/fingerprintx/pkg/plugins/pluginutils"
)

type EchoPlugin struct{}

const ECHO = "echo"

func isEcho(conn net.Conn, timeout time.Duration) (bool, error) {
// Generate a random 64 byte payload
payload := make([]byte, 64)
if _, err := rand.Read(payload); err != nil {
return false, err
}

response, err := pluginutils.SendRecv(conn, payload, timeout)
if err != nil {
return false, err
}

// Check if the response matches the payload
isEchoService := bytes.Equal(payload, response)

return isEchoService, nil
}

func init() {
plugins.RegisterPlugin(&EchoPlugin{})
}

func (p *EchoPlugin) PortPriority(port uint16) bool {
return port == 7
}

func (p *EchoPlugin) Run(conn net.Conn, timeout time.Duration, target plugins.Target) (*plugins.Service, error) {
if isEcho, err := isEcho(conn, timeout); !isEcho || err != nil {
return nil, nil
}
payload := plugins.ServiceEcho{}

return plugins.CreateServiceFrom(target, payload, false, "", plugins.TCP), nil
}

func (p *EchoPlugin) Name() string {
return ECHO
}

func (p *EchoPlugin) Type() plugins.Protocol {
return plugins.TCP
}

func (p *EchoPlugin) Priority() int {
return 1
}
55 changes: 55 additions & 0 deletions pkg/plugins/services/echo/echo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2022 Praetorian Security, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package echo

import (
"testing"

"github.com/ory/dockertest/v3"
"github.com/praetorian-inc/fingerprintx/pkg/plugins"
"github.com/praetorian-inc/fingerprintx/pkg/test"
)

func TestEcho(t *testing.T) {
testcases := []test.Testcase{
{
Description: "echo",
Port: 7,
Protocol: plugins.TCP,
Expected: func(res *plugins.Service) bool {
return res != nil
},
RunConfig: dockertest.RunOptions{
Repository: "itsthenetwork/alpine-ncat",
Cmd: []string{"-e", "/bin/cat", "-k", "-l", "-p", "7"},
Entrypoint: []string{"/usr/bin/ncat"},
ExposedPorts: []string{"7"},
},
},
}

p := &EchoPlugin{}

for _, tc := range testcases {
tc := tc
t.Run(tc.Description, func(t *testing.T) {
t.Parallel()
err := test.RunTest(t, tc, p)
if err != nil {
t.Errorf(err.Error())
}
})
}
}
5 changes: 5 additions & 0 deletions pkg/plugins/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const TypeService string = "service"
const (
ProtoDNS = "dns"
ProtoDHCP = "dhcp"
ProtoEcho = "echo"
ProtoFTP = "ftp"
ProtoHTTP = "http"
ProtoHTTPS = "https"
Expand Down Expand Up @@ -483,6 +484,10 @@ type ServiceDHCP struct {

func (e ServiceDHCP) Type() string { return ProtoDHCP }

type ServiceEcho struct{}

func (e ServiceEcho) Type() string { return ProtoEcho }

type ServiceRsync struct{}

func (e ServiceRsync) Type() string { return ProtoRsync }
1 change: 1 addition & 0 deletions pkg/scan/plugin_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package scan
import (
_ "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/dhcp"
_ "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/dns"
_ "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/echo"
_ "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/ftp"
_ "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/http"
_ "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/imap"
Expand Down

0 comments on commit 312cf79

Please sign in to comment.