parse.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package rfc5424
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/syslog/internal/parser/utils"
  6. )
  7. type RFC5424Option func(*RFC5424)
  8. type RFC5424 struct {
  9. PRI int
  10. Timestamp time.Time
  11. Hostname string
  12. Tag string
  13. Message string
  14. PID string
  15. MsgID string
  16. //
  17. len int
  18. position int
  19. buf []byte
  20. useCurrentYear bool //If no year is specified in the timestamp, use the current year
  21. strictHostname bool //If the hostname contains invalid characters or is not an IP, return an error
  22. }
  23. const PRI_MAX_LEN = 3
  24. const NIL_VALUE = '-'
  25. var VALID_TIMESTAMPS = []string{
  26. time.RFC3339,
  27. }
  28. const VALID_TIMESTAMP = time.RFC3339Nano
  29. func WithCurrentYear() RFC5424Option {
  30. return func(r *RFC5424) {
  31. r.useCurrentYear = true
  32. }
  33. }
  34. func WithStrictHostname() RFC5424Option {
  35. return func(r *RFC5424) {
  36. r.strictHostname = true
  37. }
  38. }
  39. func (r *RFC5424) parsePRI() error {
  40. pri := 0
  41. if r.buf[r.position] != '<' {
  42. return fmt.Errorf("PRI must start with '<'")
  43. }
  44. r.position++
  45. for r.position < r.len {
  46. c := r.buf[r.position]
  47. if c == '>' {
  48. r.position++
  49. break
  50. }
  51. if c < '0' || c > '9' {
  52. return fmt.Errorf("PRI must be a number")
  53. }
  54. pri = pri*10 + int(c-'0')
  55. r.position++
  56. }
  57. if pri > 999 {
  58. return fmt.Errorf("PRI must be up to 3 characters long")
  59. }
  60. if r.position == r.len && r.buf[r.position-1] != '>' {
  61. return fmt.Errorf("PRI must end with '>'")
  62. }
  63. r.PRI = pri
  64. return nil
  65. }
  66. func (r *RFC5424) parseVersion() error {
  67. if r.buf[r.position] != '1' {
  68. return fmt.Errorf("version must be 1")
  69. }
  70. r.position += 2
  71. if r.position >= r.len {
  72. return fmt.Errorf("version must be followed by a space")
  73. }
  74. return nil
  75. }
  76. func (r *RFC5424) parseTimestamp() error {
  77. timestamp := []byte{}
  78. if r.buf[r.position] == NIL_VALUE {
  79. r.Timestamp = time.Now().UTC().Round(0)
  80. r.position += 2
  81. return nil
  82. }
  83. for r.position < r.len {
  84. c := r.buf[r.position]
  85. if c == ' ' {
  86. break
  87. }
  88. timestamp = append(timestamp, c)
  89. r.position++
  90. }
  91. if len(timestamp) == 0 {
  92. return fmt.Errorf("timestamp is empty")
  93. }
  94. if r.position == r.len {
  95. return fmt.Errorf("EOL after timestamp")
  96. }
  97. date, err := time.Parse(VALID_TIMESTAMP, string(timestamp))
  98. if err != nil {
  99. return fmt.Errorf("timestamp is not valid")
  100. }
  101. r.Timestamp = date
  102. r.position++
  103. if r.position >= r.len {
  104. return fmt.Errorf("EOL after timestamp")
  105. }
  106. return nil
  107. }
  108. func (r *RFC5424) parseHostname() error {
  109. if r.buf[r.position] == NIL_VALUE {
  110. r.Hostname = ""
  111. r.position += 2
  112. return nil
  113. }
  114. hostname := []byte{}
  115. for r.position < r.len {
  116. c := r.buf[r.position]
  117. if c == ' ' {
  118. r.position++
  119. break
  120. }
  121. hostname = append(hostname, c)
  122. r.position++
  123. }
  124. if r.strictHostname {
  125. if !utils.IsValidHostnameOrIP(string(hostname)) {
  126. return fmt.Errorf("hostname is not valid")
  127. }
  128. }
  129. if len(hostname) == 0 {
  130. return fmt.Errorf("hostname is empty")
  131. }
  132. r.Hostname = string(hostname)
  133. return nil
  134. }
  135. func (r *RFC5424) parseAppName() error {
  136. if r.buf[r.position] == NIL_VALUE {
  137. r.Tag = ""
  138. r.position += 2
  139. return nil
  140. }
  141. appname := []byte{}
  142. for r.position < r.len {
  143. c := r.buf[r.position]
  144. if c == ' ' {
  145. r.position++
  146. break
  147. }
  148. appname = append(appname, c)
  149. r.position++
  150. }
  151. if len(appname) == 0 {
  152. return fmt.Errorf("appname is empty")
  153. }
  154. if len(appname) > 48 {
  155. return fmt.Errorf("appname is too long")
  156. }
  157. r.Tag = string(appname)
  158. return nil
  159. }
  160. func (r *RFC5424) parseProcID() error {
  161. if r.buf[r.position] == NIL_VALUE {
  162. r.PID = ""
  163. r.position += 2
  164. return nil
  165. }
  166. procid := []byte{}
  167. for r.position < r.len {
  168. c := r.buf[r.position]
  169. if c == ' ' {
  170. r.position++
  171. break
  172. }
  173. procid = append(procid, c)
  174. r.position++
  175. }
  176. if len(procid) == 0 {
  177. return fmt.Errorf("procid is empty")
  178. }
  179. if len(procid) > 128 {
  180. return fmt.Errorf("procid is too long")
  181. }
  182. r.PID = string(procid)
  183. return nil
  184. }
  185. func (r *RFC5424) parseMsgID() error {
  186. if r.buf[r.position] == NIL_VALUE {
  187. r.MsgID = ""
  188. r.position += 2
  189. return nil
  190. }
  191. msgid := []byte{}
  192. for r.position < r.len {
  193. c := r.buf[r.position]
  194. if c == ' ' {
  195. r.position++
  196. break
  197. }
  198. msgid = append(msgid, c)
  199. r.position++
  200. }
  201. if len(msgid) == 0 {
  202. return fmt.Errorf("msgid is empty")
  203. }
  204. if len(msgid) > 32 {
  205. return fmt.Errorf("msgid is too long")
  206. }
  207. r.MsgID = string(msgid)
  208. return nil
  209. }
  210. func (r *RFC5424) parseStructuredData() error {
  211. done := false
  212. if r.buf[r.position] == NIL_VALUE {
  213. r.position += 2
  214. return nil
  215. }
  216. if r.buf[r.position] != '[' {
  217. return fmt.Errorf("structured data must start with '[' or be '-'")
  218. }
  219. prev := byte(0)
  220. for r.position < r.len {
  221. done = false
  222. c := r.buf[r.position]
  223. if c == ']' && prev != '\\' {
  224. done = true
  225. r.position++
  226. if r.position < r.len && r.buf[r.position] == ' ' {
  227. break
  228. }
  229. }
  230. prev = c
  231. r.position++
  232. }
  233. r.position++
  234. if !done {
  235. return fmt.Errorf("structured data must end with ']'")
  236. }
  237. return nil
  238. }
  239. func (r *RFC5424) parseMessage() error {
  240. if r.position == r.len {
  241. return fmt.Errorf("message is empty")
  242. }
  243. message := []byte{}
  244. for r.position < r.len {
  245. c := r.buf[r.position]
  246. message = append(message, c)
  247. r.position++
  248. }
  249. r.Message = string(message)
  250. return nil
  251. }
  252. func (r *RFC5424) Parse(message []byte) error {
  253. r.len = len(message)
  254. if r.len == 0 {
  255. return fmt.Errorf("syslog line is empty")
  256. }
  257. r.buf = message
  258. err := r.parsePRI()
  259. if err != nil {
  260. return err
  261. }
  262. if r.position >= r.len {
  263. return fmt.Errorf("EOL after PRI")
  264. }
  265. err = r.parseVersion()
  266. if err != nil {
  267. return err
  268. }
  269. if r.position >= r.len {
  270. return fmt.Errorf("EOL after Version")
  271. }
  272. err = r.parseTimestamp()
  273. if err != nil {
  274. return err
  275. }
  276. if r.position >= r.len {
  277. return fmt.Errorf("EOL after Timestamp")
  278. }
  279. err = r.parseHostname()
  280. if err != nil {
  281. return err
  282. }
  283. if r.position >= r.len {
  284. return fmt.Errorf("EOL after hostname")
  285. }
  286. err = r.parseAppName()
  287. if err != nil {
  288. return err
  289. }
  290. if r.position >= r.len {
  291. return fmt.Errorf("EOL after appname")
  292. }
  293. err = r.parseProcID()
  294. if err != nil {
  295. return err
  296. }
  297. if r.position >= r.len {
  298. return fmt.Errorf("EOL after ProcID")
  299. }
  300. err = r.parseMsgID()
  301. if err != nil {
  302. return err
  303. }
  304. if r.position >= r.len {
  305. return fmt.Errorf("EOL after MSGID")
  306. }
  307. err = r.parseStructuredData()
  308. if err != nil {
  309. return err
  310. }
  311. if r.position >= r.len {
  312. return fmt.Errorf("EOL after SD")
  313. }
  314. err = r.parseMessage()
  315. if err != nil {
  316. return err
  317. }
  318. return nil
  319. }
  320. func NewRFC5424Parser(opts ...RFC5424Option) *RFC5424 {
  321. r := &RFC5424{}
  322. for _, opt := range opts {
  323. opt(r)
  324. }
  325. return r
  326. }