|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/root4loot/ctlog" |
| 11 | + "github.com/root4loot/goutils/domainutil" |
| 12 | + "github.com/root4loot/goutils/fileutil" |
| 13 | + "github.com/root4loot/goutils/log" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + AppName = "ctlog" |
| 18 | + Version = "0.1.0" |
| 19 | +) |
| 20 | + |
| 21 | +func init() { |
| 22 | + log.Init(AppName) |
| 23 | +} |
| 24 | + |
| 25 | +const usage = ` |
| 26 | +Usage: ctlog [options] <domain | orgname> |
| 27 | + -f, --file <file> Specify input file containing targets, one per line. |
| 28 | + -t, --timeout <seconds> Set the timeout for each request (default: 90). |
| 29 | + -c, --concurrency <number> Set the number of concurrent requests (default: 3). |
| 30 | + --version Display the version information. |
| 31 | + --help Display this help message. |
| 32 | +
|
| 33 | +Search Query Identity: |
| 34 | + - Domain Name |
| 35 | + - Organization Name |
| 36 | +
|
| 37 | +Examples: |
| 38 | + ctlog example.com |
| 39 | + ctlog "Hackerone Inc" |
| 40 | + ctlog --file domains.txt |
| 41 | +` |
| 42 | + |
| 43 | +func main() { |
| 44 | + inputList, opts, err := parseCLI() |
| 45 | + runner := ctlog.NewRunnerWithOptions(opts) |
| 46 | + |
| 47 | + if err != nil { |
| 48 | + if err.Error() == "version" { |
| 49 | + fmt.Println("version:", Version) |
| 50 | + os.Exit(0) |
| 51 | + } |
| 52 | + |
| 53 | + log.Error(err) |
| 54 | + os.Exit(0) |
| 55 | + } |
| 56 | + |
| 57 | + if hasStdin() { |
| 58 | + log.Debug("Reading from stdin") |
| 59 | + |
| 60 | + scanner := bufio.NewScanner(os.Stdin) |
| 61 | + for scanner.Scan() { |
| 62 | + inputList = append(inputList, strings.TrimSpace(scanner.Text())) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if inputList != nil { |
| 67 | + processResults(runner, inputList...) |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +func processResults(runner *ctlog.Runner, target ...string) { |
| 73 | + go runner.RunMultipleAsync(target) |
| 74 | + |
| 75 | + printed := make(map[string]bool) |
| 76 | + for res := range runner.Results { |
| 77 | + res.CommonName = strings.ToLower(res.CommonName) |
| 78 | + res.CommonName = strings.TrimPrefix(res.CommonName, "*.") |
| 79 | + res.IssuerName = strings.ToLower(res.IssuerName) |
| 80 | + res.IssuerName = strings.TrimPrefix(res.IssuerName, "*.") |
| 81 | + |
| 82 | + if !printed[res.CommonName] { |
| 83 | + if domainutil.IsDomainName(res.CommonName) { |
| 84 | + log.Result(res.CommonName) |
| 85 | + printed[res.CommonName] = true |
| 86 | + } |
| 87 | + |
| 88 | + if domainutil.IsDomainName(res.IssuerName) { |
| 89 | + log.Result(res.IssuerName) |
| 90 | + printed[res.IssuerName] = true |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func parseCLI() ([]string, *ctlog.Options, error) { |
| 97 | + var version, help bool |
| 98 | + var inputFilePath string |
| 99 | + var inputList []string |
| 100 | + |
| 101 | + opts := *ctlog.DefaultOptions() |
| 102 | + |
| 103 | + flag.StringVar(&inputFilePath, "f", "", "") |
| 104 | + flag.StringVar(&inputFilePath, "file", "", "") |
| 105 | + flag.IntVar(&opts.Timeout, "timeout", opts.Timeout, "") |
| 106 | + flag.IntVar(&opts.Timeout, "t", opts.Timeout, "") |
| 107 | + flag.IntVar(&opts.Concurrency, "concurrency", opts.Concurrency, "") |
| 108 | + flag.IntVar(&opts.Concurrency, "c", opts.Concurrency, "") |
| 109 | + flag.BoolVar(&version, "version", false, "") |
| 110 | + flag.BoolVar(&help, "help", false, "") |
| 111 | + flag.Usage = func() { |
| 112 | + fmt.Fprint(os.Stdout, usage) |
| 113 | + } |
| 114 | + flag.Parse() |
| 115 | + |
| 116 | + args := flag.Args() |
| 117 | + if len(args) > 0 { |
| 118 | + inputList = args |
| 119 | + } |
| 120 | + |
| 121 | + if inputFilePath != "" { |
| 122 | + lines, err := fileutil.ReadFile(inputFilePath) |
| 123 | + if err != nil { |
| 124 | + return nil, nil, err |
| 125 | + } |
| 126 | + |
| 127 | + inputList = append(inputList, lines...) |
| 128 | + } |
| 129 | + |
| 130 | + if help || version || (len(flag.Args()) == 0 && len(inputList) == 0 && !hasStdin()) { |
| 131 | + if help { |
| 132 | + fmt.Fprint(os.Stdout, usage) |
| 133 | + return nil, nil, nil |
| 134 | + } else if version { |
| 135 | + log.Info("Version:", Version) |
| 136 | + } else { |
| 137 | + return nil, nil, fmt.Errorf("No input provided. See -h for usage") |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if log.IsOutputPiped() { |
| 142 | + log.Notify(log.PipedOutputNotification) |
| 143 | + } |
| 144 | + |
| 145 | + if hasStdin() { |
| 146 | + inputList = append(inputList, processStdin()...) |
| 147 | + } |
| 148 | + |
| 149 | + if inputFilePath != "" { |
| 150 | + lines, err := fileutil.ReadFile(inputFilePath) |
| 151 | + if err != nil { |
| 152 | + return nil, nil, err |
| 153 | + } |
| 154 | + |
| 155 | + inputList = append(inputList, lines...) |
| 156 | + } |
| 157 | + |
| 158 | + return inputList, &opts, nil |
| 159 | +} |
| 160 | + |
| 161 | +func hasStdin() bool { |
| 162 | + stat, err := os.Stdin.Stat() |
| 163 | + if err != nil { |
| 164 | + return false |
| 165 | + } |
| 166 | + |
| 167 | + mode := stat.Mode() |
| 168 | + isPipedFromChrDev := (mode & os.ModeCharDevice) == 0 |
| 169 | + isPipedFromFIFO := (mode & os.ModeNamedPipe) != 0 |
| 170 | + |
| 171 | + return isPipedFromChrDev || isPipedFromFIFO |
| 172 | +} |
| 173 | + |
| 174 | +func processStdin() []string { |
| 175 | + var targets []string |
| 176 | + if hasStdin() { |
| 177 | + scanner := bufio.NewScanner(os.Stdin) |
| 178 | + for scanner.Scan() { |
| 179 | + line := strings.TrimSpace(scanner.Text()) |
| 180 | + if len(line) > 0 { |
| 181 | + targets = append(targets, strings.Fields(line)...) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + return targets |
| 186 | +} |
0 commit comments