-
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.
Implements network scanner in pure go (no reliance on nmap)
- Loading branch information
1 parent
100e8e4
commit 71f3f30
Showing
4 changed files
with
158 additions
and
11 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,120 @@ | ||
package discovery | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"net" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/projectdiscovery/mapcidr" | ||
"github.com/robgonnella/ops/internal/logger" | ||
"github.com/robgonnella/ops/internal/server" | ||
) | ||
|
||
var cidrSuffix = regexp.MustCompile(`\/\d{2}$`) | ||
|
||
type NetScanner struct { | ||
canceled bool | ||
targets []string | ||
wg sync.WaitGroup | ||
log logger.Logger | ||
} | ||
|
||
func NewNetScanner(targets []string) *NetScanner { | ||
return &NetScanner{ | ||
canceled: false, | ||
targets: targets, | ||
wg: sync.WaitGroup{}, | ||
log: logger.New(), | ||
} | ||
} | ||
|
||
func (s *NetScanner) Scan() ([]*DiscoveryResult, error) { | ||
s.log.Info().Msg("Scanning network...") | ||
|
||
results := []*DiscoveryResult{} | ||
// allow 1000 concurrent connections | ||
semaphore := make(chan struct{}, 1000) | ||
|
||
for _, t := range s.targets { | ||
if s.canceled { | ||
break | ||
} | ||
|
||
if cidrSuffix.MatchString(t) { | ||
ips, err := mapcidr.IPAddresses(t) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, ip := range ips { | ||
semaphore <- struct{}{} // acquire | ||
s.wg.Add(1) | ||
go func(i string, res []*DiscoveryResult) { | ||
r := s.scanIP(i, semaphore) | ||
results = append(results, r) | ||
}(ip, results) | ||
} | ||
} else { | ||
semaphore <- struct{}{} // acquire | ||
s.wg.Add(1) | ||
go func(i string, res []*DiscoveryResult) { | ||
r := s.scanIP(i, semaphore) | ||
results = append(results, r) | ||
}(t, results) | ||
} | ||
} | ||
|
||
s.wg.Wait() | ||
|
||
return results, nil | ||
} | ||
|
||
func (s *NetScanner) Stop() { | ||
s.canceled = true | ||
} | ||
|
||
func (s *NetScanner) scanIP(ip string, semaphore chan struct{}) *DiscoveryResult { | ||
s.wg.Done() | ||
|
||
hashedIP := sha1.Sum([]byte(ip)) | ||
id := hex.EncodeToString(hashedIP[:]) | ||
|
||
r := DiscoveryResult{ | ||
ID: id, | ||
Hostname: "", | ||
IP: ip, | ||
OS: "", | ||
Status: server.StatusOffline, | ||
} | ||
|
||
s.log.Info().Str("ip", ip).Msg("Scanning target") | ||
|
||
timeOut := time.Millisecond * 3000 | ||
conn, err := net.DialTimeout("tcp", ip+":22", timeOut) | ||
|
||
if err != nil { | ||
r.Ports = []Port{{ID: 22, Status: PortClosed}} | ||
|
||
s.log.Error().Err(err).Msg("network scanning error") | ||
|
||
if _, ok := err.(*net.OpError); ok { | ||
if strings.HasSuffix(err.Error(), "connect: connection refused") { | ||
r.Status = server.StatusOnline | ||
} | ||
} | ||
} else { | ||
defer conn.Close() | ||
r.Status = server.StatusOnline | ||
r.Ports = []Port{{ID: 22, Status: PortOpen}} | ||
} | ||
|
||
// release | ||
<-semaphore | ||
|
||
return &r | ||
} |
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