|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v3" |
| 8 | +) |
| 9 | + |
| 10 | +const ConfigDefaultFilename = "naabu.conf" |
| 11 | + |
| 12 | +// ConfigFile contains the fields stored in the configuration file |
| 13 | +type ConfigFile struct { |
| 14 | + // Retries is the number of retries for the port |
| 15 | + Retries int `yaml:"retries,omitempty"` |
| 16 | + // Rate is the rate of port scan requests |
| 17 | + Rate int `yaml:"rate,omitempty"` |
| 18 | + // Thread controls the number of parallel host to enumerate |
| 19 | + Threads int `yaml:"threads,omitempty"` |
| 20 | + // Timeout is the seconds to wait for ports to respond |
| 21 | + Timeout int `yaml:"timeout,omitempty"` |
| 22 | + // Hosts are the host to find ports for |
| 23 | + Host []string `yaml:"host,omitempty"` |
| 24 | + // Ports is the ports to use for enumeration |
| 25 | + Ports []string `yaml:"ports,omitempty"` |
| 26 | + // ExcludePorts is the list of ports to exclude from enumeration |
| 27 | + ExcludePorts []string `yaml:"exclude-ports,omitempty"` |
| 28 | + // Verify is used to check if the ports found were valid using CONNECT method |
| 29 | + Verify bool `yaml:"verify,omitempty"` |
| 30 | + // NoProbe skips probes to discover alive hosts |
| 31 | + NoProbe bool `yaml:"no-probe,omitempty"` |
| 32 | + // Ping uses ping probes to discover fastest active host and discover dead hosts |
| 33 | + Ping bool `yaml:"ping,omitempty"` |
| 34 | + // Port Probes (SYN-PORT, ACK-PORT) |
| 35 | + PortProbes []string `yaml:"port-probes,omitempty"` |
| 36 | + // Ips or cidr to be excluded from the scan |
| 37 | + ExcludeIps []string `yaml:"exclude-ips,omitempty"` |
| 38 | + // Top ports list |
| 39 | + TopPorts string `yaml:"top-ports,omitempty"` |
| 40 | + // Attempts to run as root |
| 41 | + Privileged bool `yaml:"privileged,omitempty"` |
| 42 | + // Drop root privileges |
| 43 | + Unprivileged bool `yaml:"unprivileged,omitempty"` |
| 44 | + // Excludes ip of knows CDN ranges |
| 45 | + ExcludeCDN bool `yaml:"exclude-cdn,omitempty"` |
| 46 | + // IcmpEchoProbe before scanning |
| 47 | + IcmpEchoProbe bool `yaml:"icmp-echo-probe,omitempty"` |
| 48 | + // IcmpTimestampProbe before scanning |
| 49 | + IcmpTimestampProbe bool `yaml:"icmp-timestamp-probe,omitempty"` |
| 50 | + // SourceIp to use in TCP packets |
| 51 | + SourceIp string `yaml:"source-ip,omitempty"` |
| 52 | + // Interface to use for TCP packets |
| 53 | + Interface string `yaml:"interface,omitempty"` |
| 54 | + // WarmUpTime between scan phases |
| 55 | + WarmUpTime int `yaml:"warm-up-time,omitempty"` |
| 56 | + // NMapCommand to invoke after scanning |
| 57 | + NMapCommand string `yaml:"nmap,omitempty"` |
| 58 | +} |
| 59 | + |
| 60 | +// GetConfigDirectory gets the subfinder config directory for a user |
| 61 | +func GetConfigDirectory() (string, error) { |
| 62 | + var config string |
| 63 | + |
| 64 | + directory, err := os.UserHomeDir() |
| 65 | + if err != nil { |
| 66 | + return config, err |
| 67 | + } |
| 68 | + config = directory + "/.config/naabu" |
| 69 | + |
| 70 | + // Create All directory for naabu even if they exist |
| 71 | + err = os.MkdirAll(config, os.ModePerm) |
| 72 | + if err != nil { |
| 73 | + return config, err |
| 74 | + } |
| 75 | + |
| 76 | + return config, nil |
| 77 | +} |
| 78 | + |
| 79 | +// CheckConfigExists checks if the config file exists in the given path |
| 80 | +func CheckConfigExists(configPath string) bool { |
| 81 | + if _, err := os.Stat(configPath); err == nil { |
| 82 | + return true |
| 83 | + } else if os.IsNotExist(err) { |
| 84 | + return false |
| 85 | + } |
| 86 | + return false |
| 87 | +} |
| 88 | + |
| 89 | +// MarshalWrite writes the marshaled yaml config to disk |
| 90 | +func (c *ConfigFile) MarshalWrite(file string) error { |
| 91 | + f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0755) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + // Indent the spaces too |
| 97 | + enc := yaml.NewEncoder(f) |
| 98 | + err = enc.Encode(&c) |
| 99 | + f.Close() |
| 100 | + return err |
| 101 | +} |
| 102 | + |
| 103 | +// UnmarshalRead reads the unmarshalled config yaml file from disk |
| 104 | +func UnmarshalRead(file string) (ConfigFile, error) { |
| 105 | + config := ConfigFile{} |
| 106 | + |
| 107 | + f, err := os.Open(file) |
| 108 | + if err != nil { |
| 109 | + return config, err |
| 110 | + } |
| 111 | + err = yaml.NewDecoder(f).Decode(&config) |
| 112 | + f.Close() |
| 113 | + return config, err |
| 114 | +} |
| 115 | + |
| 116 | +func getDefaultConfigFile() (string, error) { |
| 117 | + directory, err := GetConfigDirectory() |
| 118 | + if err != nil { |
| 119 | + return "", err |
| 120 | + } |
| 121 | + return path.Join(directory, ConfigDefaultFilename), nil |
| 122 | +} |
0 commit comments