Skip to content

Commit

Permalink
Merge pull request #63 from irabva/portscan
Browse files Browse the repository at this point in the history
fixed ipv6 addresses scan error
  • Loading branch information
its-a-feature authored Nov 13, 2024
2 parents 474a9ab + bfdc446 commit 35a2ffa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 47 deletions.
36 changes: 21 additions & 15 deletions Payload_Type/poseidon/poseidon/agent_code/portscan/portscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package portscan
import (
// Standard
"encoding/json"
"errors"
"strconv"
"strings"
"time"
Expand All @@ -17,14 +18,14 @@ type PortScanParams struct {
Ports []string `json:"ports"`
}

func doScan(hostList []string, portListStrs []string, job *structs.Job) []CIDR {
func doScan(hostList []string, portListStrs []string, job *structs.Job) ([]CIDR, error) {
// Variable declarations
timeout := time.Duration(500) * time.Millisecond
var portList []PortRange

// populate the portList
for i := 0; i < len(portListStrs); i++ {
if strings.Contains(portListStrs[i], "-") && len(portListStrs) == 1 {
if strings.Contains(portListStrs[i], "-") && len(portListStrs[i]) == 1 {
// They want all the ports
allPorts := PortRange{1, 65535}
var newList []PortRange
Expand Down Expand Up @@ -58,6 +59,11 @@ func doScan(hostList []string, portListStrs []string, job *structs.Job) []CIDR {
}
}

if len(portList) == 0 {
err := errors.New("no ports to scan")
return nil, err
}

// var cidrs []*CIDR

var results []CIDR
Expand All @@ -73,8 +79,7 @@ func doScan(hostList []string, portListStrs []string, job *structs.Job) []CIDR {
// cidrs = append(cidrs, newCidr)
}
}

return results
return results, nil
}

func Run(task structs.Task) {
Expand All @@ -87,29 +92,30 @@ func Run(task structs.Task) {
task.Job.SendResponses <- msg
return
}
if len(params.Hosts) == 0 {
msg.UserOutput = "No hosts given to scan"
msg.Completed = true
msg.Status = "error"
if len(params.Hosts) == 0 || len(params.Hosts) == 1 && params.Hosts[0] == "" {
msg.SetError("No hosts given to scan")
task.Job.SendResponses <- msg
return
}
if len(params.Ports) == 0 {
msg.UserOutput = "No ports given to scan"
msg.Completed = true
msg.Status = "error"
msg.SetError("No ports given to scan")
task.Job.SendResponses <- msg
return
}

//log.Println("Beginning portscan...")
results := doScan(params.Hosts, params.Ports, task.Job)
results, err := doScan(params.Hosts, params.Ports, task.Job)
if err != nil {
msg.SetError(err.Error())
task.Job.SendResponses <- msg
return
}

// log.Println("Finished!")
data, err := json.MarshalIndent(results, "", " ")
// // fmt.Println("Data:", string(data))
if err != nil {
msg.UserOutput = err.Error()
msg.Completed = true
msg.Status = "error"
msg.SetError(err.Error())
task.Job.SendResponses <- msg
return
}
Expand Down
46 changes: 16 additions & 30 deletions Payload_Type/poseidon/poseidon/agent_code/portscan/scanutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type PortRange struct {
}

type host struct {
IPv4 string `json:"ipv4"`
IP string `json:"ip"`
Hostname string `json:"hostname"`
PrettyName string `json:"pretty_name"`
OpenPorts []int `json:"open_ports"`
Expand All @@ -36,46 +36,27 @@ type CIDR struct {
Hosts []*host `json:"hosts"`
}

// Helper function to validate IPv4
func ValidIPv4(host string) bool {
parts := strings.Split(host, ".")

if len(parts) < 4 {
return false
}

for _, x := range parts {
if i, err := strconv.Atoi(x); err == nil {
if i < 0 || i > 255 {
return false
}
} else {
return false
}

}
return true
}

// Validates that a new host can be created based on hostName
func NewHost(hostName string) (*host, error) {
mtx := sync.Mutex{}
if ValidIPv4(hostName) {
// chek if hostname is IP address
if net.ParseIP(hostName) != nil {
return &host{
IPv4: hostName,
IP: hostName,
PrettyName: hostName,
mutex: mtx,
lock: semaphore.NewWeighted(100), // yeah i hardcoded don't @me
}, nil
} else {
// Try and lookup the hostname
ips, err := net.LookupIP(hostName)
ips, err := net.LookupHost(hostName)
if err != nil {
return nil, err
}
hostStr := fmt.Sprintf("%s (%s)", ips[0].String(), hostName)
hostStr := fmt.Sprintf("%s (%s)", ips[0], hostName)

return &host{
IPv4: ips[0].String(),
IP: ips[0],
Hostname: hostName,
PrettyName: hostStr,
mutex: mtx,
Expand Down Expand Up @@ -115,7 +96,7 @@ func NewCIDR(cidrStr string) (*CIDR, error) {
}, nil
}

// http://play.golang.org/p/m8TNTtygK0
// http://play.golang.org/p/m8TNTtygK0
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
Expand All @@ -126,9 +107,14 @@ func inc(ip net.IP) {
}

// Scan a single port!
//export
// export
func (server *host) ScanPort(port int, timeout time.Duration) {
target := fmt.Sprintf("%s:%d", server.IPv4, port)
var target string
if strings.Contains(server.IP, ":") {
target = fmt.Sprintf("[%s]:%d", server.IP, port)
} else {
target = fmt.Sprintf("%s:%d", server.IP, port)
}
conn, err := net.DialTimeout("tcp", target, timeout)

if conn != nil {
Expand Down
4 changes: 2 additions & 2 deletions Payload_Type/poseidon/poseidon/browserscripts/portscan_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function(task, response){
let tables = [];
let headers = [
{"plaintext": "hostname", "type": "string", "fillWidth": true, "disableSort": true},
{"plaintext": "ipv4", "type": "string", "fillWidth": true, "disableSort": true},
{"plaintext": "ip", "type": "string", "fillWidth": true, "disableSort": true},
{"plaintext": "pretty name", "type": "string", "fillWidth": true, "disableSort": true},
{"plaintext": "open ports", "type": "string", "fillWidth": true,"disableSort": true}

Expand All @@ -18,7 +18,7 @@ function(task, response){
for(let k = 0; k < data[j]["hosts"].length; k++){
rows.push({
"hostname": {"plaintext": data[j]["hosts"][k]['hostname']},
"ipv4": {"plaintext": data[j]["hosts"][k]["ipv4"]},
"ip": {"plaintext": data[j]["hosts"][k]["ip"]},
"pretty name": {"plaintext":data[j]["hosts"][k]["pretty_name"]},
"open ports": {"plaintext": JSON.stringify(data[j]["hosts"][k]["open_ports"])}
});
Expand Down

0 comments on commit 35a2ffa

Please sign in to comment.