Skip to content

Commit

Permalink
resolved #398 #415 regarding proper JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Aug 30, 2020
1 parent b7f6b42 commit b84da2d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 90 deletions.
150 changes: 113 additions & 37 deletions cmd/amass/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/OWASP/Amass/v3/config"
"github.com/OWASP/Amass/v3/format"
"github.com/OWASP/Amass/v3/graph"
"github.com/OWASP/Amass/v3/requests"
"github.com/OWASP/Amass/v3/stringset"
"github.com/fatih/color"
)
Expand Down Expand Up @@ -42,6 +44,7 @@ type dbArgs struct {
ConfigFile string
Directory string
Domains string
JSONOutput string
TermOut string
}
}
Expand Down Expand Up @@ -74,6 +77,7 @@ func runDBCommand(clArgs []string) {
dbCommand.StringVar(&args.Filepaths.ConfigFile, "config", "", "Path to the INI configuration file. Additional details below")
dbCommand.StringVar(&args.Filepaths.Directory, "dir", "", "Path to the directory containing the graph database")
dbCommand.StringVar(&args.Filepaths.Domains, "df", "", "Path to a file providing root domain names")
dbCommand.StringVar(&args.Filepaths.JSONOutput, "json", "", "Path to the JSON output file")
dbCommand.StringVar(&args.Filepaths.TermOut, "o", "", "Path to the text file containing terminal stdout/stderr")

if len(clArgs) < 1 {
Expand Down Expand Up @@ -129,21 +133,6 @@ func runDBCommand(clArgs []string) {
}
defer db.Close()

if args.Options.ListEnumerations {
listEvents(&args, db)
return
}

if args.Options.ShowAll {
args.Options.DiscoveredNames = true
args.Options.ASNTableSummary = true
}

if !args.Options.DiscoveredNames && !args.Options.ASNTableSummary {
commandUsage(dbUsageMsg, dbCommand, dbBuf)
return
}

// Create the in-memory graph database for events that have information in scope
memDB, err := memGraphForScope(args.Domains.Slice(), db)
if err != nil {
Expand All @@ -158,6 +147,21 @@ func runDBCommand(clArgs []string) {
os.Exit(1)
}

if args.Options.ListEnumerations {
listEvents(uuids, memDB)
return
}

if args.Options.ShowAll {
args.Options.DiscoveredNames = true
args.Options.ASNTableSummary = true
}

if !args.Options.DiscoveredNames && !args.Options.ASNTableSummary {
commandUsage(dbUsageMsg, dbCommand, dbBuf)
return
}

// Put the events in chronological order
uuids, _, _ = orderedEvents(uuids, memDB)
if len(uuids) == 0 {
Expand All @@ -166,8 +170,10 @@ func runDBCommand(clArgs []string) {
}

// Select the enumeration that the user specified
if args.Enum > 0 && len(uuids) > args.Enum {
uuids = []string{uuids[args.Enum]}
if args.Enum > 0 && len(uuids) >= args.Enum {
idx := len(uuids) - args.Enum

uuids = []string{uuids[idx]}
}

if args.Options.ASNTableSummary {
Expand All @@ -181,38 +187,32 @@ func runDBCommand(clArgs []string) {
showEventData(&args, uuids, memDB)
}

func listEvents(args *dbArgs, db *graph.Graph) {
domains := args.Domains.Slice()
events := eventUUIDs(domains, db)

if len(events) == 0 {
r.Fprintln(color.Error, "No enumerations found within the provided scope")
return
}

events, earliest, latest := orderedEvents(events, db)
func listEvents(uuids []string, db *graph.Graph) {
events, earliest, latest := orderedEvents(uuids, db)
// Check if the user has requested the list of enumerations
for i := range events {
if i != 0 {
for pos, idx := 0, len(events)-1; idx >= 0; idx-- {
if pos != 0 {
g.Println()
}

g.Printf("%d) %s -> %s: ", i+1, earliest[i].Format(timeFormat), latest[i].Format(timeFormat))
g.Printf("%d) %s -> %s: ", pos+1, earliest[idx].Format(timeFormat), latest[idx].Format(timeFormat))
// Print out the scope for this enumeration
for x, domain := range db.EventDomains(events[i]) {
for x, domain := range db.EventDomains(events[idx]) {
if x != 0 {
g.Print(", ")
}
g.Print(domain)
}
g.Println()
pos++
}
}

func showEventData(args *dbArgs, uuids []string, db *graph.Graph) {
var total int
var err error
var outfile *os.File
var discovered []*requests.Output
domains := args.Domains.Slice()

if args.Filepaths.TermOut != "" {
Expand All @@ -237,31 +237,43 @@ func showEventData(args *dbArgs, uuids []string, db *graph.Graph) {
}

out.Addresses = format.DesiredAddrTypes(out.Addresses, args.Options.IPv4, args.Options.IPv6)
if len(out.Addresses) == 0 {
if l := len(out.Addresses); (args.Options.IPs || args.Options.IPv4 || args.Options.IPv6) && l == 0 {
continue
} else if l > 0 {
total++
format.UpdateSummaryData(out, tags, asns)
}

total++
format.UpdateSummaryData(out, tags, asns)
source, name, ips := format.OutputLineParts(out, args.Options.Sources,
args.Options.IPs || args.Options.IPv4 || args.Options.IPv6, args.Options.DemoMode)

if ips != "" {
ips = " " + ips
}

if args.Options.DiscoveredNames {
var written bool
if outfile != nil {
fmt.Fprintf(outfile, "%s%s%s\n", source, name, ips)
} else {
written = true
}
if args.Filepaths.JSONOutput != "" {
discovered = append(discovered, out)
written = true
}
if !written {
fmt.Fprintf(color.Output, "%s%s%s\n", blue(source), green(name), yellow(ips))
}
}
}

if total == 0 {
r.Println("No names were discovered")
} else if args.Options.ASNTableSummary {
return
}
if args.Filepaths.JSONOutput != "" {
writeJSON(args, uuids, discovered, db)
}
if args.Options.ASNTableSummary {
var out io.Writer
status := color.NoColor

Expand All @@ -278,3 +290,67 @@ func showEventData(args *dbArgs, uuids []string, db *graph.Graph) {
color.NoColor = status
}
}

type jsonEvent struct {
UUID string `json:"uuid"`
Start string `json:"start"`
Finish string `json:"finish"`
}

type jsonDomain struct {
Domain string `json:"domain"`
Total int `json:"total"`
Names []*requests.Output
}

type jsonOutput struct {
Events []*jsonEvent `json:"events"`
Domains []*jsonDomain `json:"domains"`
}

func writeJSON(args *dbArgs, uuids []string, assets []*requests.Output, db *graph.Graph) {
var output jsonOutput

// Add the event data to the JSON
events, earliest, latest := orderedEvents(uuids, db)
for i, uuid := range events {
output.Events = append(output.Events, &jsonEvent{
UUID: uuid,
Start: earliest[i].Format(timeFormat),
Finish: latest[i].Format(timeFormat),
})
}
// Add the asset specific data
for _, asset := range assets {
var found bool
var d *jsonDomain

for _, domain := range output.Domains {
if domain.Domain == asset.Domain {
found = true
d = domain
break
}
}
if !found {
d = &jsonDomain{Domain: asset.Domain}

output.Domains = append(output.Domains, d)
}

d.Total++
d.Names = append(d.Names, asset)
}

jsonptr, err := os.OpenFile(args.Filepaths.JSONOutput, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
r.Fprintf(color.Error, "Failed to open the JSON output file: %v\n", err)
return
}
// Remove previously stored data and encode the JSON
jsonptr.Truncate(0)
jsonptr.Seek(0, 0)
json.NewEncoder(jsonptr).Encode(output)
jsonptr.Sync()
jsonptr.Close()
}
36 changes: 0 additions & 36 deletions resolvers/pool_test.go

This file was deleted.

17 changes: 0 additions & 17 deletions resources/scripts/scrape/dnstable.ads

This file was deleted.

0 comments on commit b84da2d

Please sign in to comment.