-
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 d3e59a4
Showing
8 changed files
with
186 additions
and
53 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
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ package app_info | |
const NAME = "ops" | ||
|
||
// VERSION app version | ||
const VERSION = "v0.1.5" | ||
const VERSION = "v0.2.0" |
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,116 @@ | ||
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 | ||
semaphore chan struct{} | ||
log logger.Logger | ||
} | ||
|
||
func NewNetScanner(targets []string) *NetScanner { | ||
return &NetScanner{ | ||
canceled: false, | ||
targets: targets, | ||
semaphore: make(chan struct{}, 1000), | ||
log: logger.New(), | ||
} | ||
} | ||
|
||
func (s *NetScanner) Scan() ([]*DiscoveryResult, error) { | ||
s.log.Info().Msg("Scanning network...") | ||
|
||
results := []*DiscoveryResult{} | ||
|
||
ipList := []string{} | ||
|
||
wg := &sync.WaitGroup{} | ||
|
||
for _, t := range s.targets { | ||
if s.canceled { | ||
break | ||
} | ||
|
||
if cidrSuffix.MatchString(t) { | ||
ips, err := mapcidr.IPAddresses(t) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ipList = append(ipList, ips...) | ||
} else { | ||
ipList = append(ipList, t) | ||
} | ||
} | ||
|
||
for _, ip := range ipList { | ||
s.semaphore <- struct{}{} // acquire | ||
wg.Add(1) | ||
go func(i string, w *sync.WaitGroup, res []*DiscoveryResult) { | ||
r := s.scanIP(i) | ||
results = append(results, r) | ||
<-s.semaphore // release | ||
w.Done() | ||
}(ip, wg, results) | ||
} | ||
|
||
wg.Wait() | ||
|
||
return results, nil | ||
} | ||
|
||
func (s *NetScanner) Stop() { | ||
s.canceled = true | ||
} | ||
|
||
func (s *NetScanner) scanIP(ip string) *DiscoveryResult { | ||
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 * 200 | ||
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}} | ||
} | ||
|
||
return &r | ||
} |
Oops, something went wrong.