main文件中common.Parse(&Info) 如何处理参数

parse中有四个处理参数的函数,分别处理扫描类型、用户名、密码 、输出。

  1. func Parse(Info *HostInfo) {
  2. ParseScantype(Info)
  3. ParseUser(Info)
  4. ParsePass(Info)
  5. ParseInput(Info)
  6. }

ParseScantype(Info)

处理 -m 参数:flag.StringVar(&Info.Scantype, “m”, “all”, “Select scan type ,as: -m ssh”)

  1. 判断Info.Scantype 是否在PORTList的key中(PORTList定义协议所对应的端口),使用map进行判断,不在则调用showmode(输出PORTList并退出)
  2. 判断扫描类型是否是all(默认值all)
    1. 判断-p 参数的info.ports,是否等于默认端口
      1. 使用switch判断扫描类型

(自己日常使用者从没有用过-m 参数,默认值为all,不进入这里)

  1. func ParseScantype(Info *HostInfo) {
  2. _, ok := PORTList[Info.Scantype]
  3. if !ok {
  4. showmode()
  5. }
  6. if Info.Scantype != "all" {
  7. if Info.Ports == DefaultPorts {
  8. switch Info.Scantype {
  9. case "rdp":
  10. Info.Ports = "3389"
  11. case "wmi":
  12. Info.Ports = "135"
  13. case "web":
  14. Info.Ports = Webport
  15. case "ms17010":
  16. Info.Ports = "445"
  17. case "cve20200796":
  18. Info.Ports = "445"
  19. case "smb2":
  20. Info.Ports = "445"
  21. case "portscan":
  22. Info.Ports = DefaultPorts + "," + Webport
  23. case "main":
  24. Info.Ports = DefaultPorts
  25. default:
  26. port, _ := PORTList[Info.Scantype]
  27. Info.Ports = strconv.Itoa(port)
  28. }
  29. fmt.Println("-m ", Info.Scantype, " start scan the port:", Info.Ports)
  30. }
  31. }
  32. }

ParseUser(Info)

处理爆破时的用户名、用户名文件,-user、-userf

  1. 判断-user、-userf是否为空,等于空return
  2. 如果-user参数不为空,使用,分割多个用户名,例如 -user root,admin
  3. 如果-userf不为空,使用readfile函数打开userfile,
    1. readfile函数主要bufio处理用户名文件,最终返回下标对应的username(不是很理解,返回的string类型为啥是这样,以及bufio)

0 123
1 456
2 789
3 abc
遍历返回的值拿出username 追加到Info.Usernames 中

  1. 使用RemoveDuplicate对用户名进行去重
  2. userdict 为常见协议的用户名map,将上面的所有用户名赋值给 Userdict的key。(Userdict应该是在没有设置用户名与用户文件是的默认用户名map,在设置自己的用户名后就覆盖)

    1. var Userdict = map[string][]string{
    2. "ftp": {"ftp", "admin", "www", "web", "root", "db", "wwwroot", "data"},
    3. "mysql": {"root", "mysql"},
    4. "mssql": {"sa", "sql"},
    5. "smb": {"administrator", "admin", "guest"},
    6. "rdp": {"administrator", "admin", "guest"},
    7. "postgresql": {"postgres", "admin"},
    8. "ssh": {"root", "admin"},
    9. "mongodb": {"root", "admin"},
    10. }
    1. func ParseUser(Info *HostInfo) {
    2. if Info.Username == "" && Userfile == "" {
    3. return
    4. }
    5. if Info.Username != "" {
    6. Info.Usernames = strings.Split(Info.Username, ",")
    7. }
    8. if Userfile != "" {
    9. users, err := Readfile(Userfile)
    10. if err == nil {
    11. for _, user := range users {
    12. if user != "" {
    13. Info.Usernames = append(Info.Usernames, user)
    14. }
    15. }
    16. }
    17. }
    18. Info.Usernames = RemoveDuplicate(Info.Usernames)
    19. for name := range Userdict {
    20. Userdict[name] = Info.Usernames
    21. }
    22. }

    ParsePass(Info *HostInfo)

  3. 密码处理思路跟用户名差不多

  4. 先判断有无单独 使用-pwd 参数的密码,逗号分隔。
  5. 判断是否指定了password文件,读取文件追加到Info.Passwords中,然后覆盖默认密码Passwords
  6. 判断-uf 指定的url文件,处理后赋值给urls

    1. func ParsePass(Info *HostInfo) {
    2. if Info.Password != "" {
    3. passs := strings.Split(Info.Password, ",")
    4. for _, pass := range passs {
    5. if pass != "" {
    6. Info.Passwords = append(Info.Passwords, pass)
    7. }
    8. }
    9. Passwords = Info.Passwords
    10. }
    11. if Passfile != "" {
    12. passs, err := Readfile(Passfile)
    13. if err == nil {
    14. for _, pass := range passs {
    15. if pass != "" {
    16. Info.Passwords = append(Info.Passwords, pass)
    17. }
    18. }
    19. Passwords = Info.Passwords
    20. }
    21. }
    22. if UrlFile != "" {
    23. urls, err := Readfile(UrlFile)
    24. if err == nil {
    25. TmpUrls := make(map[string]struct{})
    26. for _, url := range urls {
    27. if _, ok := TmpUrls[url]; !ok {
    28. TmpUrls[url] = struct{}{}
    29. if url != "" {
    30. Urls = append(Urls, url)
    31. }
    32. }
    33. }
    34. }
    35. }
    36. }

    ParseInput(Info *HostInfo)

    处理-o 参数

    1. func ParseInput(Info *HostInfo) {
    2. if Info.Host == "" && HostFile == "" && URL == "" && UrlFile == "" {
    3. fmt.Println("Host is none")
    4. flag.Usage()
    5. os.Exit(0)
    6. }
    7. if TmpOutputfile != "" {
    8. if !strings.Contains(Outputfile, "/") && !strings.Contains(Outputfile, `\`) {
    9. Outputfile = getpath() + TmpOutputfile
    10. } else {
    11. Outputfile = TmpOutputfile
    12. }
    13. }
    14. if TmpSave == true {
    15. IsSave = false
    16. }
    17. if Info.Ports == DefaultPorts {
    18. Info.Ports += "," + Webport
    19. }
    20. if PortAdd != "" {
    21. if strings.HasSuffix(Info.Ports, ",") {
    22. Info.Ports += PortAdd
    23. } else {
    24. Info.Ports += "," + PortAdd
    25. }
    26. }
    27. }