Skip to content

Commit 587d418

Browse files
committed
chg: [sshd] fix #1 panic on white space / empty user
1 parent 6746f5e commit 587d418

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

logparser/parser_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ var expected = map[int]map[string]string{
4040
"username": "a.min",
4141
"src": "185.56.8.191",
4242
},
43+
5: map[string]string{
44+
"date": "Jan 22 11:29:16",
45+
"host": "sigmund",
46+
"username": " ",
47+
"src": "185.56.8.191",
48+
},
49+
6: map[string]string{
50+
"date": "Jan 22 11:29:16",
51+
"host": "sigmund",
52+
"username": "",
53+
"src": "185.56.8.191",
54+
},
4355
}
4456

4557
func TestSshdParser(t *testing.T) {
@@ -53,7 +65,7 @@ func TestSshdParser(t *testing.T) {
5365
scanner := bufio.NewScanner(f)
5466
c := 0
5567
for scanner.Scan() {
56-
re := regexp.MustCompile(`^(?P<date>[[:alpha:]]{3}\s\d{2}\s\d{2}:\d{2}:\d{2}) (?P<host>[^ ]+) sshd\[[[:alnum:]]+\]: Invalid user (?P<username>[^ ]+) from (?P<src>.*$)`)
68+
re := regexp.MustCompile(`^(?P<date>[[:alpha:]]{3}\s\d{2}\s\d{2}:\d{2}:\d{2}) (?P<host>[^ ]+) sshd\[[[:alnum:]]+\]: Invalid user (?P<username>.*) from (?P<src>.*$)`)
5769
n1 := re.SubexpNames()
5870
r2 := re.FindAllStringSubmatch(scanner.Text(), -1)[0]
5971

logparser/sshd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (s *SshdParser) Set(rconn1 *redis.Conn, rconn2 *redis.Conn) {
3333
// Parse parses a line of sshd log
3434
func (s *SshdParser) Parse(logline string) error {
3535
r := *s.r1
36-
re := regexp.MustCompile(`^(?P<date>[[:alpha:]]{3}\s\d{2}\s\d{2}:\d{2}:\d{2}) (?P<host>[^ ]+) sshd\[[[:alnum:]]+\]: Invalid user (?P<username>[^ ]+) from (?P<src>.*$)`)
36+
re := regexp.MustCompile(`^(?P<date>[[:alpha:]]{3}\s\d{2}\s\d{2}:\d{2}:\d{2}) (?P<host>[^ ]+) sshd\[[[:alnum:]]+\]: Invalid user (?P<username>.*) from (?P<src>.*$)`)
3737
n1 := re.SubexpNames()
3838
r2 := re.FindAllStringSubmatch(logline, -1)[0]
3939

logparser/test.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ Jan 22 11:37:19 si.mund sshd[26143]: Invalid user gestion from 159.89.153.54
33
Jan 22 11:34:46 sigmund sshd[26125]: Invalid user atpco from 177.152.124.21
44
Jan 22 11:33:07 sigmund sshd[26109]: Invalid user ki from 49.233.183.158
55
Jan 22 11:29:16 sigmund sshd[26091]: Invalid user a.min from 185.56.8.191
6+
Jan 22 11:29:16 sigmund sshd[26091]: Invalid user from 185.56.8.191
7+
Jan 22 11:29:16 sigmund sshd[26091]: Invalid user from 185.56.8.191

0 commit comments

Comments
 (0)