Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 308 display ip part of cdn #324

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions v2/pkg/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Options struct {
ResumeCfg *ResumeCfg
Stream bool
Passive bool
OutputCDN bool // display cdn in use
}

// OnResultCallback (hostname, ip, ports)
Expand All @@ -82,6 +83,7 @@ func ParseOptions() *Options {
flagSet.StringVarP(&options.ExcludePorts, "ep", "exclude-ports", "", "ports to exclude from scan (comma-separated)"),
flagSet.StringVarP(&options.PortsFile, "pf", "ports-file", "", "list of ports to exclude from scan (file)"),
flagSet.BoolVarP(&options.ExcludeCDN, "ec", "exclude-cdn", false, "skip full port scans for CDN's (only checks for 80,443)"),
flagSet.BoolVarP(&options.OutputCDN, "cdn", "display-cdn", false, "display cdn in use"),
)

flagSet.CreateGroup("rate-limit", "Rate-limit",
Expand Down
18 changes: 12 additions & 6 deletions v2/pkg/runner/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Result struct {
Host string `json:"host,omitempty" csv:"host"`
IP string `json:"ip,omitempty" csv:"ip"`
Port int `json:"port" csv:"port"`
IsCDNIP bool `json:"cdn,omitempty" csv:"cdn"`
CDNName string `json:"cdn-name,omitempty" csv:"cdn-name"`
TimeStamp time.Time `json:"timestamp" csv:"timestamp"`
}

Expand Down Expand Up @@ -53,14 +55,17 @@ func (r *Result) CSVFields() ([]string, error) {
}

// WriteHostOutput writes the output list of host ports to an io.Writer
func WriteHostOutput(host string, ports map[int]struct{}, writer io.Writer) error {
func WriteHostOutput(host string, ports map[int]struct{}, cdnName string, writer io.Writer) error {
bufwriter := bufio.NewWriter(writer)
sb := &strings.Builder{}

for port := range ports {
sb.WriteString(host)
sb.WriteString(":")
sb.WriteString(strconv.Itoa(port))
if cdnName != "" {
sb.WriteString(" [" + cdnName + "]")
}
sb.WriteString("\n")

_, err := bufwriter.WriteString(sb.String())
Expand All @@ -74,15 +79,15 @@ func WriteHostOutput(host string, ports map[int]struct{}, writer io.Writer) erro
}

// WriteJSONOutput writes the output list of subdomain in JSON to an io.Writer
func WriteJSONOutput(host, ip string, ports map[int]struct{}, writer io.Writer) error {
func WriteJSONOutput(host, ip string, ports map[int]struct{}, isCdn bool, cdnName string, writer io.Writer) error {
encoder := json.NewEncoder(writer)

data := Result{TimeStamp: time.Now().UTC()}
if host != ip {
data.Host = host
}
data.IP = ip

data.IsCDNIP = isCdn
data.CDNName = cdnName
for port := range ports {
data.Port = port
err := encoder.Encode(&data)
Expand All @@ -94,7 +99,7 @@ func WriteJSONOutput(host, ip string, ports map[int]struct{}, writer io.Writer)
}

// WriteCsvOutput writes the output list of subdomain in csv format to an io.Writer
func WriteCsvOutput(host, ip string, ports map[int]struct{}, header bool, writer io.Writer) error {
func WriteCsvOutput(host, ip string, ports map[int]struct{}, isCdn bool, cdnName string, header bool, writer io.Writer) error {
encoder := csv.NewWriter(writer)
data := &Result{TimeStamp: time.Now().UTC()}
if header {
Expand All @@ -104,7 +109,8 @@ func WriteCsvOutput(host, ip string, ports map[int]struct{}, header bool, writer
data.Host = host
}
data.IP = ip

data.IsCDNIP = isCdn
data.CDNName = cdnName
for port := range ports {
data.Port = port
writeCSVRow(data, encoder)
Expand Down
4 changes: 2 additions & 2 deletions v2/pkg/runner/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestWriteHostOutput(t *testing.T) {
ports := map[int]struct{}{80: {}, 8080: {}}
var s string
buf := bytes.NewBufferString(s)
assert.Nil(t, WriteHostOutput(host, ports, buf))
assert.Nil(t, WriteHostOutput(host, ports,"", buf))
assert.Contains(t, buf.String(), "127.0.0.1:80")
assert.Contains(t, buf.String(), "127.0.0.1:8080")
}
Expand All @@ -24,6 +24,6 @@ func TestWriteJSONOutput(t *testing.T) {
ports := map[int]struct{}{80: {}, 8080: {}}
var s string
buf := bytes.NewBufferString(s)
assert.Nil(t, WriteJSONOutput(host, ip, ports, buf))
assert.Nil(t, WriteJSONOutput(host, ip, ports,false, "", buf))
assert.Equal(t, 3, len(strings.Split(buf.String(), "\n")))
}
16 changes: 11 additions & 5 deletions v2/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewRunner(options *Options) (*Runner, error) {
Rate: options.Rate,
Debug: options.Debug,
ExcludeCdn: options.ExcludeCDN,
OutputCdn: options.OutputCDN,
ExcludedIps: excludedIps,
Proxy: options.Proxy,
Stream: options.Stream,
Expand Down Expand Up @@ -497,10 +498,11 @@ func (r *Runner) handleOutput() {
if host == "ip" {
host = hostIP
}
isCDNIP, cdnName, _ := r.scanner.CdnCheck(hostIP)
gologger.Info().Msgf("Found %d ports on host %s (%s)\n", len(ports), host, hostIP)
// console output
if r.options.JSON || r.options.CSV {
data := &Result{IP: hostIP, TimeStamp: time.Now().UTC()}
data := &Result{IP: hostIP, TimeStamp: time.Now().UTC(), IsCDNIP: isCDNIP, CDNName: cdnName}
if host != hostIP {
data.Host = host
}
Expand Down Expand Up @@ -528,17 +530,21 @@ func (r *Runner) handleOutput() {
gologger.Silent().Msgf("%s", buffer.String())
} else {
for port := range ports {
gologger.Silent().Msgf("%s:%d\n", host, port)
if r.options.OutputCDN && isCDNIP {
gologger.Silent().Msgf("%s:%d [%s]\n", host, port, cdnName)
} else {
gologger.Silent().Msgf("%s:%d\n", host, port)
}
}
}
// file output
if file != nil {
if r.options.JSON {
err = WriteJSONOutput(host, hostIP, ports, file)
err = WriteJSONOutput(host, hostIP, ports, isCDNIP, cdnName, file)
} else if r.options.CSV {
err = WriteCsvOutput(host, hostIP, ports, csvFileHeaderEnabled, file)
err = WriteCsvOutput(host, hostIP, ports, isCDNIP, cdnName, csvFileHeaderEnabled, file)
} else {
err = WriteHostOutput(host, ports, file)
err = WriteHostOutput(host, ports, cdnName, file)
}
if err != nil {
gologger.Error().Msgf("Could not write results to file %s for %s: %s\n", output, host, err)
Expand Down
1 change: 1 addition & 0 deletions v2/pkg/scan/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options struct {
Rate int
Debug bool
ExcludeCdn bool
OutputCdn bool
ExcludedIps []string
Proxy string
Stream bool
Expand Down
3 changes: 1 addition & 2 deletions v2/pkg/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ func NewScanner(options *Options) (*Scanner, error) {
}

scanner.ScanResults = result.NewResult()

if options.ExcludeCdn {
if options.ExcludeCdn || options.OutputCdn {
var err error
scanner.cdn, err = cdncheck.NewWithCache()
if err != nil {
Expand Down