-
Notifications
You must be signed in to change notification settings - Fork 122
/
root.go
97 lines (77 loc) · 2.67 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cmd
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/sensepost/godoh/lib"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var (
// Version is the current version
Version string
// CompileTimeDomain is the domain set with `make dnsDomain=foo.com`
CompileTimeDomain string
// options are CLI options
options = lib.NewOptions()
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "godoh",
Short: "A DNS (over-HTTPS) C2",
Long: `A DNS (over-HTTPS) C2
Version: ` + Version + `
By @leonjza from @sensepost`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
rand.Seed(time.Now().UTC().UnixNano())
// configure the TLS validation setup
options.SetTLSValidation()
// Setup the logger to use
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "02 Jan 2006 15:04:05"})
if options.Debug {
log.Logger = log.Logger.Level(zerolog.DebugLevel)
log.Logger = log.With().Caller().Logger()
log.Debug().Msg("debug logging enabed")
} else {
log.Logger = log.Logger.Level(zerolog.InfoLevel)
}
if options.DisableLogging {
log.Logger = log.Logger.Level(zerolog.Disabled)
}
options.Logger = &log.Logger
// if we have a compile time domain, use that if one is not set via CLI
if (options.Domain == "") && (CompileTimeDomain != "") {
log.Debug().Str("domain", CompileTimeDomain).Msg("using compile time domain")
options.Domain = CompileTimeDomain
} else {
log.Debug().Str("domain", options.Domain).Msg("using flag domain")
}
},
Run: func(cmd *cobra.Command, args []string) {
// by default, start in agent mode
if len(args) == 0 {
agentCmd.Run(cmd, args)
os.Exit(0)
}
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
// logging
rootCmd.PersistentFlags().BoolVar(&options.Debug, "debug", false, "enable debug logging")
rootCmd.PersistentFlags().BoolVar(&options.DisableLogging, "disable-logging", false, "disable all logging")
// if the DNS domain was configured at compile time, remove the flag
if options.Domain == "" {
rootCmd.PersistentFlags().StringVarP(&options.Domain, "domain", "d", "", "DNS Domain to use. (ie: example.com)")
}
rootCmd.PersistentFlags().StringVarP(&options.ProviderName, "provider", "p", "google", "Preferred DNS provider to use. [possible: googlefront, google, cloudflare, quad9, raw]")
rootCmd.PersistentFlags().BoolVarP(&options.ValidateTLS, "validate-certificate", "K", false, "Validate DoH provider SSL certificates")
}