|
1 | | -// Copyright (c) 2025 TM Hospitality Strategies |
| 1 | +// Copyright (c) 2026 TM Hospitality Strategies |
2 | 2 | // |
3 | | -// Tool for generating custom wordlists for subdomain enumeration |
4 | | -// Educational purposes only |
| 3 | +// Tool for generating custom wordlists for subdomain enumeration. |
| 4 | +// For authorized use only. |
5 | 5 |
|
6 | 6 | package main |
7 | 7 |
|
8 | 8 | import ( |
9 | 9 | "bufio" |
10 | 10 | "flag" |
11 | 11 | "fmt" |
| 12 | + "io" |
12 | 13 | "os" |
13 | 14 | "strings" |
14 | 15 | ) |
15 | 16 |
|
| 17 | +// commonPrefixes are frequently seen subdomain labels, emitted first when |
| 18 | +// -common is set. |
| 19 | +var commonPrefixes = []string{ |
| 20 | + "www", "mail", "remote", "blog", "webmail", "server", "ns1", "ns2", |
| 21 | + "smtp", "secure", "vpn", "m", "shop", "ftp", "mail2", "test", |
| 22 | + "portal", "admin", "host", "api", "dev", "web", "cloud", "email", |
| 23 | + "apps", "support", "app", "staging", "proxy", "beta", "gateway", |
| 24 | + "cdn", "auth", "intranet", "mobile", "sso", "help", "docs", |
| 25 | +} |
| 26 | + |
| 27 | +// secondLevelSuffixes are labels that sit under a ccTLD as a registry |
| 28 | +// namespace (acme.co.uk, acme.com.au), so they are stripped along with it. |
| 29 | +var secondLevelSuffixes = map[string]bool{ |
| 30 | + "co": true, "com": true, "net": true, "org": true, "gov": true, |
| 31 | + "edu": true, "ac": true, "or": true, "ne": true, "go": true, |
| 32 | +} |
| 33 | + |
| 34 | +type options struct { |
| 35 | + common bool |
| 36 | + domain string |
| 37 | + combine string |
| 38 | +} |
| 39 | + |
| 40 | +// domainTerms extracts candidate words from a domain: the public suffix is |
| 41 | +// dropped (the last label, plus a second-level label such as "co" in |
| 42 | +// acme.co.uk) and the rest is split on dots, hyphens and underscores. Terms |
| 43 | +// shorter than three characters are skipped. |
| 44 | +func domainTerms(domain string) []string { |
| 45 | + labels := strings.Split(strings.ToLower(strings.Trim(domain, ".")), ".") |
| 46 | + if len(labels) > 1 { |
| 47 | + labels = labels[:len(labels)-1] |
| 48 | + } |
| 49 | + if len(labels) > 1 && secondLevelSuffixes[labels[len(labels)-1]] { |
| 50 | + labels = labels[:len(labels)-1] |
| 51 | + } |
| 52 | + var terms []string |
| 53 | + for _, part := range strings.FieldsFunc(strings.Join(labels, "."), func(r rune) bool { |
| 54 | + return r == '.' || r == '-' || r == '_' |
| 55 | + }) { |
| 56 | + if len(part) > 2 { |
| 57 | + terms = append(terms, part) |
| 58 | + } |
| 59 | + } |
| 60 | + return terms |
| 61 | +} |
| 62 | + |
| 63 | +// generate returns the wordlist in a stable order: common prefixes, then |
| 64 | +// domain terms, then each -combine prefix followed by its prefix-word |
| 65 | +// combinations. Duplicates keep their first position, so the same options |
| 66 | +// always produce byte-identical output. |
| 67 | +func generate(o options) []string { |
| 68 | + var out []string |
| 69 | + seen := make(map[string]bool) |
| 70 | + add := func(w string) { |
| 71 | + w = strings.ToLower(strings.TrimSpace(w)) |
| 72 | + if w == "" || seen[w] { |
| 73 | + return |
| 74 | + } |
| 75 | + seen[w] = true |
| 76 | + out = append(out, w) |
| 77 | + } |
| 78 | + |
| 79 | + if o.common { |
| 80 | + for _, p := range commonPrefixes { |
| 81 | + add(p) |
| 82 | + } |
| 83 | + } |
| 84 | + if o.domain != "" { |
| 85 | + for _, t := range domainTerms(o.domain) { |
| 86 | + add(t) |
| 87 | + } |
| 88 | + } |
| 89 | + if o.combine != "" { |
| 90 | + base := append([]string(nil), out...) |
| 91 | + for _, prefix := range strings.Split(o.combine, ",") { |
| 92 | + prefix = strings.ToLower(strings.TrimSpace(prefix)) |
| 93 | + if prefix == "" { |
| 94 | + continue |
| 95 | + } |
| 96 | + add(prefix) |
| 97 | + for _, word := range base { |
| 98 | + if word != prefix { // no "shop-shop" |
| 99 | + add(prefix + "-" + word) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return out |
| 105 | +} |
| 106 | + |
| 107 | +// writeWordlist writes one word per line and reports the first write error. |
| 108 | +func writeWordlist(w io.Writer, words []string) error { |
| 109 | + bw := bufio.NewWriter(w) |
| 110 | + for _, word := range words { |
| 111 | + if _, err := fmt.Fprintln(bw, word); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + } |
| 115 | + return bw.Flush() |
| 116 | +} |
| 117 | + |
16 | 118 | func main() { |
17 | | - // Command-line flags |
18 | 119 | outputFile := flag.String("o", "wordlist.txt", "Path to output wordlist file") |
19 | 120 | combineWith := flag.String("combine", "", "Combine each word with these prefixes (comma-separated)") |
20 | 121 | addCommon := flag.Bool("common", true, "Add common subdomain prefixes") |
21 | 122 | domainInfo := flag.String("domain", "", "Domain to extract potential subdomains from (e.g., company-name.com -> company, name)") |
| 123 | + verbose := flag.Bool("v", false, "Print every generated entry") |
22 | 124 | flag.Parse() |
23 | 125 |
|
24 | | - // Validate output file |
25 | 126 | if *outputFile == "" { |
26 | | - fmt.Println("Error: Output file cannot be empty") |
| 127 | + fmt.Fprintln(os.Stderr, "Error: output file cannot be empty") |
27 | 128 | os.Exit(1) |
28 | 129 | } |
29 | 130 |
|
30 | | - // Create output file |
| 131 | + words := generate(options{common: *addCommon, domain: *domainInfo, combine: *combineWith}) |
| 132 | + |
31 | 133 | file, err := os.Create(*outputFile) |
32 | 134 | if err != nil { |
33 | | - fmt.Printf("Error creating output file: %v\n", err) |
| 135 | + fmt.Fprintf(os.Stderr, "Error creating output file: %v\n", err) |
34 | 136 | os.Exit(1) |
35 | 137 | } |
36 | | - |
37 | | - writer := bufio.NewWriter(file) |
38 | | - defer func() { |
39 | | - if flushErr := writer.Flush(); flushErr != nil { |
40 | | - fmt.Printf("Error flushing writer: %v\n", flushErr) |
41 | | - } |
42 | | - if closeErr := file.Close(); closeErr != nil { |
43 | | - fmt.Printf("Error closing file: %v\n", closeErr) |
44 | | - } |
45 | | - }() |
46 | | - |
47 | | - // Track added words to avoid duplicates |
48 | | - addedWords := make(map[string]bool) |
49 | | - |
50 | | - // Add common subdomain prefixes if requested |
51 | | - if *addCommon { |
52 | | - fmt.Println("Adding common subdomain prefixes...") |
53 | | - commonPrefixes := []string{ |
54 | | - "www", "mail", "remote", "blog", "webmail", "server", "ns1", "ns2", |
55 | | - "smtp", "secure", "vpn", "m", "shop", "ftp", "mail2", "test", |
56 | | - "portal", "admin", "host", "api", "dev", "web", "cloud", "email", |
57 | | - "apps", "support", "app", "staging", "proxy", "beta", "gateway", |
58 | | - "cdn", "auth", "intranet", "mobile", "sso", "help", "docs", |
59 | | - } |
60 | | - |
61 | | - for _, prefix := range commonPrefixes { |
62 | | - if !addedWords[prefix] { |
63 | | - fmt.Fprintln(writer, prefix) |
64 | | - addedWords[prefix] = true |
65 | | - fmt.Printf("Added: %s\n", prefix) |
66 | | - } |
67 | | - } |
| 138 | + writeErr := writeWordlist(file, words) |
| 139 | + closeErr := file.Close() |
| 140 | + if writeErr != nil || closeErr != nil { |
| 141 | + fmt.Fprintf(os.Stderr, "Error writing %s: %v\n", *outputFile, firstErr(writeErr, closeErr)) |
| 142 | + os.Exit(1) |
68 | 143 | } |
69 | 144 |
|
70 | | - // Process domain-specific terms if provided |
71 | | - if *domainInfo != "" { |
72 | | - fmt.Printf("Extracting terms from domain: %s\n", *domainInfo) |
73 | | - |
74 | | - // Remove TLD and split by separators |
75 | | - domain := *domainInfo |
76 | | - |
77 | | - // Remove common TLDs |
78 | | - domain = strings.TrimSuffix(domain, ".com") |
79 | | - domain = strings.TrimSuffix(domain, ".org") |
80 | | - domain = strings.TrimSuffix(domain, ".net") |
81 | | - domain = strings.TrimSuffix(domain, ".edu") |
82 | | - domain = strings.TrimSuffix(domain, ".gov") |
83 | | - domain = strings.TrimSuffix(domain, ".io") |
84 | | - |
85 | | - // Split by common separators |
86 | | - parts := strings.FieldsFunc(domain, func(r rune) bool { |
87 | | - return r == '.' || r == '-' || r == '_' |
88 | | - }) |
89 | | - |
90 | | - for _, part := range parts { |
91 | | - if len(part) > 2 && !addedWords[part] { // Only add if length > 2 |
92 | | - fmt.Fprintln(writer, part) |
93 | | - addedWords[part] = true |
94 | | - fmt.Printf("Added: %s\n", part) |
95 | | - } |
| 145 | + if *verbose { |
| 146 | + for _, w := range words { |
| 147 | + fmt.Println(w) |
96 | 148 | } |
97 | 149 | } |
| 150 | + fmt.Fprintf(os.Stderr, "Wordlist generated at %s with %d unique entries\n", *outputFile, len(words)) |
| 151 | + fmt.Fprintln(os.Stderr, "NOTE: Only use this tool to generate wordlists for domains you have explicit permission to test.") |
| 152 | +} |
98 | 153 |
|
99 | | - // Process combination prefixes if provided |
100 | | - if *combineWith != "" { |
101 | | - prefixes := strings.Split(*combineWith, ",") |
102 | | - fmt.Printf("Combining with prefixes: %v\n", prefixes) |
103 | | - |
104 | | - // Create a temporary copy of addedWords to iterate over |
105 | | - wordsCopy := make([]string, 0, len(addedWords)) |
106 | | - for word := range addedWords { |
107 | | - wordsCopy = append(wordsCopy, word) |
108 | | - } |
109 | | - |
110 | | - // Create combinations |
111 | | - for _, prefix := range prefixes { |
112 | | - prefix = strings.TrimSpace(prefix) |
113 | | - if prefix == "" { |
114 | | - continue |
115 | | - } |
116 | | - |
117 | | - // Add the prefix itself |
118 | | - if !addedWords[prefix] { |
119 | | - fmt.Fprintln(writer, prefix) |
120 | | - addedWords[prefix] = true |
121 | | - fmt.Printf("Added: %s\n", prefix) |
122 | | - } |
123 | | - |
124 | | - // Create combinations with existing words |
125 | | - for _, word := range wordsCopy { |
126 | | - combination := prefix + "-" + word |
127 | | - if !addedWords[combination] { |
128 | | - fmt.Fprintln(writer, combination) |
129 | | - addedWords[combination] = true |
130 | | - fmt.Printf("Added: %s\n", combination) |
131 | | - } |
132 | | - } |
| 154 | +func firstErr(errs ...error) error { |
| 155 | + for _, err := range errs { |
| 156 | + if err != nil { |
| 157 | + return err |
133 | 158 | } |
134 | 159 | } |
135 | | - |
136 | | - // Count total words |
137 | | - fmt.Printf("\nWordlist generated at %s with %d unique entries\n", *outputFile, len(addedWords)) |
138 | | - fmt.Println("\nNOTE: Only use this tool to generate wordlists for domains you have explicit permission to test.") |
| 160 | + return nil |
139 | 161 | } |
0 commit comments