(minutes int, whitelist []string)
| 451 | } |
| 452 | |
| 453 | func FindRecentSuccessLoginNotInWhitelist(minutes int, whitelist []string) ([]string, error) { |
| 454 | lines, err := grepSSHLog([]string{"Accepted password", "Accepted publickey"}) |
| 455 | if err != nil { |
| 456 | return nil, err |
| 457 | } |
| 458 | |
| 459 | thresholdTime := time.Now().Add(-time.Duration(minutes) * time.Minute) |
| 460 | var abnormalLogins []string |
| 461 | |
| 462 | whitelistMap := make(map[string]struct{}, len(whitelist)) |
| 463 | for _, ip := range whitelist { |
| 464 | whitelistMap[ip] = struct{}{} |
| 465 | } |
| 466 | |
| 467 | ipRegex := re.GetRegex(re.AlertIPPattern) |
| 468 | |
| 469 | for _, line := range lines { |
| 470 | line = strings.TrimSpace(line) |
| 471 | if line == "" { |
| 472 | continue |
| 473 | } |
| 474 | |
| 475 | t, err := parseLogTime(line) |
| 476 | if err != nil || t.Before(thresholdTime) { |
| 477 | continue |
| 478 | } |
| 479 | |
| 480 | match := ipRegex.FindStringSubmatch(line) |
| 481 | if len(match) >= 2 { |
| 482 | ip := match[1] |
| 483 | if _, ok := whitelistMap[ip]; !ok { |
| 484 | abnormalLogins = append(abnormalLogins, fmt.Sprintf("%s-%s", ip, t.Format("2006-01-02 15:04:05"))) |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return abnormalLogins, nil |
| 490 | } |
| 491 | |
| 492 | func findGrepPath() (string, error) { |
| 493 | path, err := exec.LookPath("grep") |
nothing calls this directly
no test coverage detected