2016-03-08 16:57:50 +00:00
|
|
|
package matching
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2016-06-19 13:33:56 +00:00
|
|
|
"strings"
|
2018-08-29 02:30:34 +00:00
|
|
|
"regexp"
|
2017-11-02 13:14:31 +00:00
|
|
|
|
|
|
|
"github.com/nbutton23/zxcvbn-go/entropy"
|
|
|
|
"github.com/nbutton23/zxcvbn-go/match"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-08-29 02:30:34 +00:00
|
|
|
dateSepMatcherName = "DATESEP"
|
|
|
|
dateWithOutSepMatcherName = "DATEWITHOUT"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dateRxYearSuffix = regexp.MustCompile( `((\d{1,2})(\s|-|\/|\\|_|\.)(\d{1,2})(\s|-|\/|\\|_|\.)(19\d{2}|200\d|201\d|\d{2}))`)
|
|
|
|
dateRxYearPrefix = regexp.MustCompile(`((19\d{2}|200\d|201\d|\d{2})(\s|-|/|\\|_|\.)(\d{1,2})(\s|-|/|\\|_|\.)(\d{1,2}))`)
|
|
|
|
dateWithOutSepMatch = regexp.MustCompile( `\d{4,8}`)
|
2016-03-08 16:57:50 +00:00
|
|
|
)
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
//FilterDateSepMatcher can be pass to zxcvbn-go.PasswordStrength to skip that matcher
|
2017-11-02 13:14:31 +00:00
|
|
|
func FilterDateSepMatcher(m match.Matcher) bool {
|
2018-08-29 02:30:34 +00:00
|
|
|
return m.ID == dateSepMatcherName
|
2017-11-02 13:14:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
//FilterDateWithoutSepMatcher can be pass to zxcvbn-go.PasswordStrength to skip that matcher
|
2017-11-02 13:14:31 +00:00
|
|
|
func FilterDateWithoutSepMatcher(m match.Matcher) bool {
|
2018-08-29 02:30:34 +00:00
|
|
|
return m.ID == dateWithOutSepMatcherName
|
2017-11-02 13:14:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 16:57:50 +00:00
|
|
|
func checkDate(day, month, year int64) (bool, int64, int64, int64) {
|
|
|
|
if (12 <= month && month <= 31) && day <= 12 {
|
|
|
|
day, month = month, day
|
|
|
|
}
|
|
|
|
|
|
|
|
if day > 31 || month > 12 {
|
|
|
|
return false, 0, 0, 0
|
|
|
|
}
|
|
|
|
|
2016-05-30 00:11:57 +00:00
|
|
|
if !((1900 <= year && year <= 2019) || (0 <= year && year <= 99)) {
|
2016-03-08 16:57:50 +00:00
|
|
|
return false, 0, 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, day, month, year
|
|
|
|
}
|
2017-11-02 13:14:31 +00:00
|
|
|
|
2016-03-08 16:57:50 +00:00
|
|
|
func dateSepMatcher(password string) []match.Match {
|
|
|
|
dateMatches := dateSepMatchHelper(password)
|
|
|
|
|
|
|
|
var matches []match.Match
|
|
|
|
for _, dateMatch := range dateMatches {
|
|
|
|
match := match.Match{
|
2016-06-19 13:33:56 +00:00
|
|
|
I: dateMatch.I,
|
|
|
|
J: dateMatch.J,
|
|
|
|
Entropy: entropy.DateEntropy(dateMatch),
|
|
|
|
DictionaryName: "date_match",
|
|
|
|
Token: dateMatch.Token,
|
2016-03-08 16:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
matches = append(matches, match)
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
func dateSepMatchHelper(password string) []match.DateMatch {
|
|
|
|
|
|
|
|
var matches []match.DateMatch
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
for _, v := range dateRxYearSuffix.FindAllString(password, len(password)) {
|
|
|
|
splitV := dateRxYearSuffix.FindAllStringSubmatch(v, len(v))
|
2016-03-08 16:57:50 +00:00
|
|
|
i := strings.Index(password, v)
|
|
|
|
j := i + len(v)
|
|
|
|
day, _ := strconv.ParseInt(splitV[0][4], 10, 16)
|
|
|
|
month, _ := strconv.ParseInt(splitV[0][2], 10, 16)
|
|
|
|
year, _ := strconv.ParseInt(splitV[0][6], 10, 16)
|
2016-06-19 13:33:56 +00:00
|
|
|
match := match.DateMatch{Day: day, Month: month, Year: year, Separator: splitV[0][5], I: i, J: j, Token: password[i:j]}
|
2016-03-08 16:57:50 +00:00
|
|
|
matches = append(matches, match)
|
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
for _, v := range dateRxYearPrefix.FindAllString(password, len(password)) {
|
|
|
|
splitV := dateRxYearPrefix.FindAllStringSubmatch(v, len(v))
|
2016-03-08 16:57:50 +00:00
|
|
|
i := strings.Index(password, v)
|
|
|
|
j := i + len(v)
|
|
|
|
day, _ := strconv.ParseInt(splitV[0][4], 10, 16)
|
|
|
|
month, _ := strconv.ParseInt(splitV[0][6], 10, 16)
|
|
|
|
year, _ := strconv.ParseInt(splitV[0][2], 10, 16)
|
2016-06-19 13:33:56 +00:00
|
|
|
match := match.DateMatch{Day: day, Month: month, Year: year, Separator: splitV[0][5], I: i, J: j, Token: password[i:j]}
|
2016-03-08 16:57:50 +00:00
|
|
|
matches = append(matches, match)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []match.DateMatch
|
|
|
|
for _, match := range matches {
|
|
|
|
if valid, day, month, year := checkDate(match.Day, match.Month, match.Year); valid {
|
|
|
|
match.Pattern = "date"
|
|
|
|
match.Day = day
|
|
|
|
match.Month = month
|
|
|
|
match.Year = year
|
|
|
|
out = append(out, match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
type dateMatchCandidate struct {
|
2016-03-08 16:57:50 +00:00
|
|
|
DayMonth string
|
|
|
|
Year string
|
|
|
|
I, J int
|
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
type dateMatchCandidateTwo struct {
|
2016-06-19 13:33:56 +00:00
|
|
|
Day string
|
2016-05-30 00:11:57 +00:00
|
|
|
Month string
|
2016-06-19 13:33:56 +00:00
|
|
|
Year string
|
|
|
|
I, J int
|
2016-05-30 00:11:57 +00:00
|
|
|
}
|
2016-06-19 13:33:56 +00:00
|
|
|
|
|
|
|
func dateWithoutSepMatch(password string) []match.Match {
|
2016-05-30 00:11:57 +00:00
|
|
|
dateMatches := dateWithoutSepMatchHelper(password)
|
|
|
|
|
|
|
|
var matches []match.Match
|
|
|
|
for _, dateMatch := range dateMatches {
|
|
|
|
match := match.Match{
|
2016-06-19 13:33:56 +00:00
|
|
|
I: dateMatch.I,
|
|
|
|
J: dateMatch.J,
|
|
|
|
Entropy: entropy.DateEntropy(dateMatch),
|
|
|
|
DictionaryName: "date_match",
|
|
|
|
Token: dateMatch.Token,
|
2016-05-30 00:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
matches = append(matches, match)
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|
2016-06-19 13:33:56 +00:00
|
|
|
|
2016-05-30 00:11:57 +00:00
|
|
|
//TODO Has issues with 6 digit dates
|
|
|
|
func dateWithoutSepMatchHelper(password string) (matches []match.DateMatch) {
|
2018-08-29 02:30:34 +00:00
|
|
|
for _, v := range dateWithOutSepMatch.FindAllString(password, len(password)) {
|
2016-03-08 16:57:50 +00:00
|
|
|
i := strings.Index(password, v)
|
|
|
|
j := i + len(v)
|
|
|
|
length := len(v)
|
|
|
|
lastIndex := length - 1
|
2018-08-29 02:30:34 +00:00
|
|
|
var candidatesRoundOne []dateMatchCandidate
|
2016-03-08 16:57:50 +00:00
|
|
|
|
|
|
|
if length <= 6 {
|
|
|
|
//2-digit year prefix
|
|
|
|
candidatesRoundOne = append(candidatesRoundOne, buildDateMatchCandidate(v[2:], v[0:2], i, j))
|
|
|
|
|
|
|
|
//2-digityear suffix
|
|
|
|
candidatesRoundOne = append(candidatesRoundOne, buildDateMatchCandidate(v[0:lastIndex-2], v[lastIndex-2:], i, j))
|
|
|
|
}
|
|
|
|
if length >= 6 {
|
|
|
|
//4-digit year prefix
|
|
|
|
candidatesRoundOne = append(candidatesRoundOne, buildDateMatchCandidate(v[4:], v[0:4], i, j))
|
|
|
|
|
|
|
|
//4-digit year sufix
|
2016-05-30 00:11:57 +00:00
|
|
|
candidatesRoundOne = append(candidatesRoundOne, buildDateMatchCandidate(v[0:lastIndex-3], v[lastIndex-3:], i, j))
|
2016-03-08 16:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
var candidatesRoundTwo []dateMatchCandidateTwo
|
2016-03-08 16:57:50 +00:00
|
|
|
for _, c := range candidatesRoundOne {
|
|
|
|
if len(c.DayMonth) == 2 {
|
2016-05-30 00:11:57 +00:00
|
|
|
candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:0], c.DayMonth[1:1], c.Year, c.I, c.J))
|
|
|
|
} else if len(c.DayMonth) == 3 {
|
2016-06-19 13:33:56 +00:00
|
|
|
candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:2], c.DayMonth[2:2], c.Year, c.I, c.J))
|
|
|
|
candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:0], c.DayMonth[1:3], c.Year, c.I, c.J))
|
2016-05-30 00:11:57 +00:00
|
|
|
} else if len(c.DayMonth) == 4 {
|
2016-06-19 13:33:56 +00:00
|
|
|
candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:2], c.DayMonth[2:4], c.Year, c.I, c.J))
|
2016-05-30 00:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, candidate := range candidatesRoundTwo {
|
|
|
|
intDay, err := strconv.ParseInt(candidate.Day, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
intMonth, err := strconv.ParseInt(candidate.Month, 10, 16)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
intYear, err := strconv.ParseInt(candidate.Year, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-19 13:33:56 +00:00
|
|
|
if ok, _, _, _ := checkDate(intDay, intMonth, intYear); ok {
|
|
|
|
matches = append(matches, match.DateMatch{Token: password, Pattern: "date", Day: intDay, Month: intMonth, Year: intYear, I: i, J: j})
|
2016-03-08 16:57:50 +00:00
|
|
|
}
|
2016-05-30 00:11:57 +00:00
|
|
|
|
2016-03-08 16:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
func buildDateMatchCandidate(dayMonth, year string, i, j int) dateMatchCandidate {
|
|
|
|
return dateMatchCandidate{DayMonth: dayMonth, Year: year, I: i, J: j}
|
2016-03-08 16:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
func buildDateMatchCandidateTwo(day, month string, year string, i, j int) dateMatchCandidateTwo {
|
2016-03-08 16:57:50 +00:00
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
return dateMatchCandidateTwo{Day: day, Month: month, Year: year, I: i, J: j}
|
2016-03-08 16:57:50 +00:00
|
|
|
}
|