Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nullt3r committed Jan 10, 2023
1 parent c7c60a2 commit 39a5d95
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
47 changes: 36 additions & 11 deletions cmd/udpx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync"
"time"
"os"
"encoding/hex"
"encoding/json"

"github.com/nullt3r/udpx/pkg/probes"
"github.com/nullt3r/udpx/pkg/scan"
Expand All @@ -22,7 +24,7 @@ func main() {
/ / / / / / / /_/ / /
/ /_/ / /_/ / ____/ |
\____/_____/_/ /_/|_|
v1.0.5, by @nullt3r
v1.0.6, by @nullt3r
%s`, colors.SetColor().Cyan, colors.SetColor().Reset)

Expand Down Expand Up @@ -88,14 +90,14 @@ func main() {

wg.Add(toscan_count)

result := make(chan string)
comm := make(chan scan.Message)

go func() {
for _, t := range toscan {
guard <- struct{}{}
go func(t string) {
defer wg.Done()
scanner := scan.Scanner{Target: t, Probes: probes.Probes, Arg_st: opts.Arg_st, Arg_sp: opts.Arg_sp, Result: result}
scanner := scan.Scanner{Target: t, Probes: probes.Probes, Arg_st: opts.Arg_st, Arg_sp: opts.Arg_sp, Channel: comm}
scanner.Run()
<-guard
}(t)
Expand All @@ -105,27 +107,50 @@ func main() {

go func() {
wg.Wait()
close(result)
close(comm)
}()

if len(opts.Arg_o) != 0 {
log.Printf("[+] Using output file '%s'", opts.Arg_o)

f, err := os.Create(opts.Arg_o)

defer f.Close()

if err != nil {
log.Fatalf("%s[!]%s Can't create output file: %s", colors.SetColor().Red, colors.SetColor().Reset, err)
log.Fatalf("%s[!]%s Error creating output file: %s", colors.SetColor().Red, colors.SetColor().Reset, err)
}

defer f.Close()
log.Printf("[+] Results will be written to: %s", opts.Arg_o)
}

for message := range comm {
log.Printf("%s[*]%s %s:%d (%s)", colors.SetColor().Cyan, colors.SetColor().Reset, message.Address, message.Port, message.Service)

for r := range result {
f.WriteString(r + "\n")
if opts.Arg_sp {
log.Printf("[+] Received packet: %s%s%s...", colors.SetColor().Yellow, hex.EncodeToString(message.ResponseData), colors.SetColor().Reset)
}

if len(opts.Arg_o) != 0 {
json, err := json.Marshal(&message)

if err != nil {
log.Fatalf("%s[!]%s Error: %s", colors.SetColor().Red, colors.SetColor().Reset, err)
}

f, err := os.OpenFile(opts.Arg_o, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)

if err != nil {
log.Fatalf("%s[!]%s Error opening output file: %s", colors.SetColor().Red, colors.SetColor().Reset, err)
}

defer f.Close()

if _, err = f.WriteString(string(json) + "\n"); err != nil {
log.Fatalf("%s[!]%s Error writing output file: %s", colors.SetColor().Red, colors.SetColor().Reset, err)
}
}
}

<-result
<-comm

log.Print("[+] Scan completed")
}
25 changes: 12 additions & 13 deletions pkg/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package scan

import (
"bufio"
"encoding/hex"
"fmt"
"log"
"net"
"strings"
"time"
"encoding/hex"

"github.com/nullt3r/udpx/pkg/probes"
"github.com/nullt3r/udpx/pkg/colors"
Expand All @@ -18,7 +18,15 @@ type Scanner struct {
Probes []probes.Probe
Arg_st int
Arg_sp bool
Result chan string
Channel chan Message
}

type Message struct {
Address string `json:"address"`
Hostname string `json:"hostname"`
Port int `json:"port"`
Service string `json:"service"`
ResponseData []byte `json:"response_data"`
}

func (s Scanner) Run() {
Expand Down Expand Up @@ -79,12 +87,7 @@ func (s Scanner) Run() {
}

if recv_length != 0 {
log.Printf("%s[*]%s %s:%d (%s)", colors.SetColor().Cyan, colors.SetColor().Reset, ip, port, probe.Name)
if s.Arg_sp {
log.Printf("[+] Received packet: %s%s%s...", colors.SetColor().Yellow, hex.EncodeToString(recv_Data), colors.SetColor().Reset)
}

s.Result <- fmt.Sprintf(`{"address": "%s", "hostname": "%s", "protocol": "udp", "portid": "%d", "port_state": "open", "service_name": "%s", "service_product": null, "service_version": null, "extrainfo": "%s"}`, ip, domain, port, probe.Name, hex.EncodeToString(recv_Data))
s.Channel <- Message{Address: ip, Hostname: domain, Port: port, Service: probe.Name, ResponseData: recv_Data}
return
}
}
Expand Down Expand Up @@ -135,11 +138,7 @@ func (s Scanner) Run() {
}

if recv_length != 0 {
log.Printf("%s[*]%s %s:%d (%s)", colors.SetColor().Cyan, colors.SetColor().Reset, ip, port, probe.Name)
if s.Arg_sp {
log.Printf("[+] Received packet: %s%s%s...", colors.SetColor().Yellow, hex.EncodeToString(recv_Data), colors.SetColor().Reset)
}
s.Result <- fmt.Sprintf(`{"address": "%s", "hostname": null, "protocol": "udp", "portid": %d, "port_state": "open", "service_name": "%s", "service_product": null, "service_version": null, "extrainfo": "%s"}`, ip, port, probe.Name, hex.EncodeToString(recv_Data))
s.Channel <- Message{Address: ip, Port: port, Service: probe.Name, ResponseData: recv_Data}
return
}
}
Expand Down

0 comments on commit 39a5d95

Please sign in to comment.