helpers.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package exprhelpers
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/antonmedv/expr"
  15. "github.com/bluele/gcache"
  16. "github.com/c-robinson/iplib"
  17. "github.com/cespare/xxhash/v2"
  18. "github.com/davecgh/go-spew/spew"
  19. "github.com/prometheus/client_golang/prometheus"
  20. log "github.com/sirupsen/logrus"
  21. "github.com/umahmood/haversine"
  22. "github.com/wasilibs/go-re2"
  23. "github.com/crowdsecurity/go-cs-lib/pkg/ptr"
  24. "github.com/crowdsecurity/crowdsec/pkg/cache"
  25. "github.com/crowdsecurity/crowdsec/pkg/database"
  26. "github.com/crowdsecurity/crowdsec/pkg/fflag"
  27. "github.com/crowdsecurity/crowdsec/pkg/types"
  28. )
  29. var dataFile map[string][]string
  30. var dataFileRegex map[string][]*regexp.Regexp
  31. var dataFileRe2 map[string][]*re2.Regexp
  32. // This is used to (optionally) cache regexp results for RegexpInFile operations
  33. var dataFileRegexCache map[string]gcache.Cache = make(map[string]gcache.Cache)
  34. /*prometheus*/
  35. var RegexpCacheMetrics = prometheus.NewGaugeVec(
  36. prometheus.GaugeOpts{
  37. Name: "cs_regexp_cache_size",
  38. Help: "Entries per regexp cache.",
  39. },
  40. []string{"name"},
  41. )
  42. var dbClient *database.Client
  43. var exprFunctionOptions []expr.Option
  44. var keyValuePattern = regexp.MustCompile(`(?P<key>[^=\s]+)=(?:"(?P<quoted_value>[^"\\]*(?:\\.[^"\\]*)*)"|(?P<value>[^=\s]+)|\s*)`)
  45. func GetExprOptions(ctx map[string]interface{}) []expr.Option {
  46. ret := []expr.Option{}
  47. ret = append(ret, exprFunctionOptions...)
  48. ret = append(ret, expr.Env(ctx))
  49. return ret
  50. }
  51. func Init(databaseClient *database.Client) error {
  52. dataFile = make(map[string][]string)
  53. dataFileRegex = make(map[string][]*regexp.Regexp)
  54. dataFileRe2 = make(map[string][]*re2.Regexp)
  55. dbClient = databaseClient
  56. exprFunctionOptions = []expr.Option{}
  57. for _, function := range exprFuncs {
  58. exprFunctionOptions = append(exprFunctionOptions,
  59. expr.Function(function.name,
  60. function.function,
  61. function.signature...,
  62. ))
  63. }
  64. return nil
  65. }
  66. func RegexpCacheInit(filename string, CacheCfg types.DataSource) error {
  67. //cache is explicitly disabled
  68. if CacheCfg.Cache != nil && !*CacheCfg.Cache {
  69. return nil
  70. }
  71. //cache is implicitly disabled if no cache config is provided
  72. if CacheCfg.Strategy == nil && CacheCfg.TTL == nil && CacheCfg.Size == nil {
  73. return nil
  74. }
  75. //cache is enabled
  76. if CacheCfg.Size == nil {
  77. CacheCfg.Size = ptr.Of(50)
  78. }
  79. gc := gcache.New(*CacheCfg.Size)
  80. if CacheCfg.Strategy == nil {
  81. CacheCfg.Strategy = ptr.Of("LRU")
  82. }
  83. switch *CacheCfg.Strategy {
  84. case "LRU":
  85. gc = gc.LRU()
  86. case "LFU":
  87. gc = gc.LFU()
  88. case "ARC":
  89. gc = gc.ARC()
  90. default:
  91. return fmt.Errorf("unknown cache strategy '%s'", *CacheCfg.Strategy)
  92. }
  93. if CacheCfg.TTL != nil {
  94. gc.Expiration(*CacheCfg.TTL)
  95. }
  96. cache := gc.Build()
  97. dataFileRegexCache[filename] = cache
  98. return nil
  99. }
  100. // UpdateCacheMetrics is called directly by the prom handler
  101. func UpdateRegexpCacheMetrics() {
  102. RegexpCacheMetrics.Reset()
  103. for name := range dataFileRegexCache {
  104. RegexpCacheMetrics.With(prometheus.Labels{"name": name}).Set(float64(dataFileRegexCache[name].Len(true)))
  105. }
  106. }
  107. func FileInit(fileFolder string, filename string, fileType string) error {
  108. log.Debugf("init (folder:%s) (file:%s) (type:%s)", fileFolder, filename, fileType)
  109. filepath := filepath.Join(fileFolder, filename)
  110. file, err := os.Open(filepath)
  111. if err != nil {
  112. return err
  113. }
  114. defer file.Close()
  115. if fileType == "" {
  116. log.Debugf("ignored file %s%s because no type specified", fileFolder, filename)
  117. return nil
  118. }
  119. if _, ok := dataFile[filename]; !ok {
  120. dataFile[filename] = []string{}
  121. }
  122. scanner := bufio.NewScanner(file)
  123. for scanner.Scan() {
  124. if strings.HasPrefix(scanner.Text(), "#") { // allow comments
  125. continue
  126. }
  127. if len(scanner.Text()) == 0 { //skip empty lines
  128. continue
  129. }
  130. switch fileType {
  131. case "regex", "regexp":
  132. if fflag.Re2RegexpInfileSupport.IsEnabled() {
  133. dataFileRe2[filename] = append(dataFileRe2[filename], re2.MustCompile(scanner.Text()))
  134. } else {
  135. dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
  136. }
  137. case "string":
  138. dataFile[filename] = append(dataFile[filename], scanner.Text())
  139. default:
  140. return fmt.Errorf("unknown data type '%s' for : '%s'", fileType, filename)
  141. }
  142. }
  143. if err := scanner.Err(); err != nil {
  144. return err
  145. }
  146. return nil
  147. }
  148. //Expr helpers
  149. // func Get(arr []string, index int) string {
  150. func Get(params ...any) (any, error) {
  151. arr := params[0].([]string)
  152. index := params[1].(int)
  153. if index >= len(arr) {
  154. return "", nil
  155. }
  156. return arr[index], nil
  157. }
  158. // func Atof(x string) float64 {
  159. func Atof(params ...any) (any, error) {
  160. x := params[0].(string)
  161. log.Debugf("debug atof %s", x)
  162. ret, err := strconv.ParseFloat(x, 64)
  163. if err != nil {
  164. log.Warningf("Atof : can't convert float '%s' : %v", x, err)
  165. }
  166. return ret, nil
  167. }
  168. // func Upper(s string) string {
  169. func Upper(params ...any) (any, error) {
  170. s := params[0].(string)
  171. return strings.ToUpper(s), nil
  172. }
  173. // func Lower(s string) string {
  174. func Lower(params ...any) (any, error) {
  175. s := params[0].(string)
  176. return strings.ToLower(s), nil
  177. }
  178. // func Distance(lat1 string, long1 string, lat2 string, long2 string) (float64, error) {
  179. func Distance(params ...any) (any, error) {
  180. lat1 := params[0].(string)
  181. long1 := params[1].(string)
  182. lat2 := params[2].(string)
  183. long2 := params[3].(string)
  184. lat1f, err := strconv.ParseFloat(lat1, 64)
  185. if err != nil {
  186. log.Warningf("lat1 is not a float : %v", err)
  187. return 0.0, fmt.Errorf("lat1 is not a float : %v", err)
  188. }
  189. long1f, err := strconv.ParseFloat(long1, 64)
  190. if err != nil {
  191. log.Warningf("long1 is not a float : %v", err)
  192. return 0.0, fmt.Errorf("long1 is not a float : %v", err)
  193. }
  194. lat2f, err := strconv.ParseFloat(lat2, 64)
  195. if err != nil {
  196. log.Warningf("lat2 is not a float : %v", err)
  197. return 0.0, fmt.Errorf("lat2 is not a float : %v", err)
  198. }
  199. long2f, err := strconv.ParseFloat(long2, 64)
  200. if err != nil {
  201. log.Warningf("long2 is not a float : %v", err)
  202. return 0.0, fmt.Errorf("long2 is not a float : %v", err)
  203. }
  204. //either set of coordinates is 0,0, return 0 to avoid FPs
  205. if (lat1f == 0.0 && long1f == 0.0) || (lat2f == 0.0 && long2f == 0.0) {
  206. log.Warningf("one of the coordinates is 0,0, returning 0")
  207. return 0.0, nil
  208. }
  209. first := haversine.Coord{Lat: lat1f, Lon: long1f}
  210. second := haversine.Coord{Lat: lat2f, Lon: long2f}
  211. _, km := haversine.Distance(first, second)
  212. return km, nil
  213. }
  214. // func QueryEscape(s string) string {
  215. func QueryEscape(params ...any) (any, error) {
  216. s := params[0].(string)
  217. return url.QueryEscape(s), nil
  218. }
  219. // func PathEscape(s string) string {
  220. func PathEscape(params ...any) (any, error) {
  221. s := params[0].(string)
  222. return url.PathEscape(s), nil
  223. }
  224. // func PathUnescape(s string) string {
  225. func PathUnescape(params ...any) (any, error) {
  226. s := params[0].(string)
  227. ret, err := url.PathUnescape(s)
  228. if err != nil {
  229. log.Debugf("unable to PathUnescape '%s': %+v", s, err)
  230. return s, nil
  231. }
  232. return ret, nil
  233. }
  234. // func QueryUnescape(s string) string {
  235. func QueryUnescape(params ...any) (any, error) {
  236. s := params[0].(string)
  237. ret, err := url.QueryUnescape(s)
  238. if err != nil {
  239. log.Debugf("unable to QueryUnescape '%s': %+v", s, err)
  240. return s, nil
  241. }
  242. return ret, nil
  243. }
  244. // func File(filename string) []string {
  245. func File(params ...any) (any, error) {
  246. filename := params[0].(string)
  247. if _, ok := dataFile[filename]; ok {
  248. return dataFile[filename], nil
  249. }
  250. log.Errorf("file '%s' (type:string) not found in expr library", filename)
  251. log.Errorf("expr library : %s", spew.Sdump(dataFile))
  252. return []string{}, nil
  253. }
  254. // func RegexpInFile(data string, filename string) bool {
  255. func RegexpInFile(params ...any) (any, error) {
  256. data := params[0].(string)
  257. filename := params[1].(string)
  258. var hash uint64
  259. hasCache := false
  260. matched := false
  261. if _, ok := dataFileRegexCache[filename]; ok {
  262. hasCache = true
  263. hash = xxhash.Sum64String(data)
  264. if val, err := dataFileRegexCache[filename].Get(hash); err == nil {
  265. return val.(bool), nil
  266. }
  267. }
  268. switch fflag.Re2RegexpInfileSupport.IsEnabled() {
  269. case true:
  270. if _, ok := dataFileRe2[filename]; ok {
  271. for _, re := range dataFileRe2[filename] {
  272. if re.MatchString(data) {
  273. matched = true
  274. break
  275. }
  276. }
  277. } else {
  278. log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
  279. log.Errorf("expr library : %s", spew.Sdump(dataFileRe2))
  280. }
  281. case false:
  282. if _, ok := dataFileRegex[filename]; ok {
  283. for _, re := range dataFileRegex[filename] {
  284. if re.MatchString(data) {
  285. matched = true
  286. break
  287. }
  288. }
  289. } else {
  290. log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
  291. log.Errorf("expr library : %s", spew.Sdump(dataFileRegex))
  292. }
  293. }
  294. if hasCache {
  295. dataFileRegexCache[filename].Set(hash, matched)
  296. }
  297. return matched, nil
  298. }
  299. // func IpInRange(ip string, ipRange string) bool {
  300. func IpInRange(params ...any) (any, error) {
  301. var err error
  302. var ipParsed net.IP
  303. var ipRangeParsed *net.IPNet
  304. ip := params[0].(string)
  305. ipRange := params[1].(string)
  306. ipParsed = net.ParseIP(ip)
  307. if ipParsed == nil {
  308. log.Debugf("'%s' is not a valid IP", ip)
  309. return false, nil
  310. }
  311. if _, ipRangeParsed, err = net.ParseCIDR(ipRange); err != nil {
  312. log.Debugf("'%s' is not a valid IP Range", ipRange)
  313. return false, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  314. }
  315. if ipRangeParsed.Contains(ipParsed) {
  316. return true, nil
  317. }
  318. return false, nil
  319. }
  320. // func IsIPV6(ip string) bool {
  321. func IsIPV6(params ...any) (any, error) {
  322. ip := params[0].(string)
  323. ipParsed := net.ParseIP(ip)
  324. if ipParsed == nil {
  325. log.Debugf("'%s' is not a valid IP", ip)
  326. return false, nil
  327. }
  328. // If it's a valid IP and can't be converted to IPv4 then it is an IPv6
  329. return ipParsed.To4() == nil, nil
  330. }
  331. // func IsIPV4(ip string) bool {
  332. func IsIPV4(params ...any) (any, error) {
  333. ip := params[0].(string)
  334. ipParsed := net.ParseIP(ip)
  335. if ipParsed == nil {
  336. log.Debugf("'%s' is not a valid IP", ip)
  337. return false, nil
  338. }
  339. return ipParsed.To4() != nil, nil
  340. }
  341. // func IsIP(ip string) bool {
  342. func IsIP(params ...any) (any, error) {
  343. ip := params[0].(string)
  344. ipParsed := net.ParseIP(ip)
  345. if ipParsed == nil {
  346. log.Debugf("'%s' is not a valid IP", ip)
  347. return false, nil
  348. }
  349. return true, nil
  350. }
  351. // func IpToRange(ip string, cidr string) string {
  352. func IpToRange(params ...any) (any, error) {
  353. ip := params[0].(string)
  354. cidr := params[1].(string)
  355. cidr = strings.TrimPrefix(cidr, "/")
  356. mask, err := strconv.Atoi(cidr)
  357. if err != nil {
  358. log.Errorf("bad cidr '%s': %s", cidr, err)
  359. return "", nil
  360. }
  361. ipAddr := net.ParseIP(ip)
  362. if ipAddr == nil {
  363. log.Errorf("can't parse IP address '%s'", ip)
  364. return "", nil
  365. }
  366. ipRange := iplib.NewNet(ipAddr, mask)
  367. if ipRange.IP() == nil {
  368. log.Errorf("can't get cidr '%s' of '%s'", cidr, ip)
  369. return "", nil
  370. }
  371. return ipRange.String(), nil
  372. }
  373. // func TimeNow() string {
  374. func TimeNow(params ...any) (any, error) {
  375. return time.Now().UTC().Format(time.RFC3339), nil
  376. }
  377. // func ParseUri(uri string) map[string][]string {
  378. func ParseUri(params ...any) (any, error) {
  379. uri := params[0].(string)
  380. ret := make(map[string][]string)
  381. u, err := url.Parse(uri)
  382. if err != nil {
  383. log.Errorf("Could not parse URI: %s", err)
  384. return ret, nil
  385. }
  386. parsed, err := url.ParseQuery(u.RawQuery)
  387. if err != nil {
  388. log.Errorf("Could not parse query uri : %s", err)
  389. return ret, nil
  390. }
  391. for k, v := range parsed {
  392. ret[k] = v
  393. }
  394. return ret, nil
  395. }
  396. // func KeyExists(key string, dict map[string]interface{}) bool {
  397. func KeyExists(params ...any) (any, error) {
  398. key := params[0].(string)
  399. dict := params[1].(map[string]interface{})
  400. _, ok := dict[key]
  401. return ok, nil
  402. }
  403. // func GetDecisionsCount(value string) int {
  404. func GetDecisionsCount(params ...any) (any, error) {
  405. value := params[0].(string)
  406. if dbClient == nil {
  407. log.Error("No database config to call GetDecisionsCount()")
  408. return 0, nil
  409. }
  410. count, err := dbClient.CountDecisionsByValue(value)
  411. if err != nil {
  412. log.Errorf("Failed to get decisions count from value '%s'", value)
  413. return 0, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  414. }
  415. return count, nil
  416. }
  417. // func GetDecisionsSinceCount(value string, since string) int {
  418. func GetDecisionsSinceCount(params ...any) (any, error) {
  419. value := params[0].(string)
  420. since := params[1].(string)
  421. if dbClient == nil {
  422. log.Error("No database config to call GetDecisionsCount()")
  423. return 0, nil
  424. }
  425. sinceDuration, err := time.ParseDuration(since)
  426. if err != nil {
  427. log.Errorf("Failed to parse since parameter '%s' : %s", since, err)
  428. return 0, nil
  429. }
  430. sinceTime := time.Now().UTC().Add(-sinceDuration)
  431. count, err := dbClient.CountDecisionsSinceByValue(value, sinceTime)
  432. if err != nil {
  433. log.Errorf("Failed to get decisions count from value '%s'", value)
  434. return 0, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  435. }
  436. return count, nil
  437. }
  438. // func LookupHost(value string) []string {
  439. func LookupHost(params ...any) (any, error) {
  440. value := params[0].(string)
  441. addresses, err := net.LookupHost(value)
  442. if err != nil {
  443. log.Errorf("Failed to lookup host '%s' : %s", value, err)
  444. return []string{}, nil
  445. }
  446. return addresses, nil
  447. }
  448. // func ParseUnixTime(value string) (time.Time, error) {
  449. func ParseUnixTime(params ...any) (any, error) {
  450. value := params[0].(string)
  451. //Splitting string here as some unix timestamp may have milliseconds and break ParseInt
  452. i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64)
  453. if err != nil || i <= 0 {
  454. return time.Time{}, fmt.Errorf("unable to parse %s as unix timestamp", value)
  455. }
  456. return time.Unix(i, 0), nil
  457. }
  458. // func ParseUnix(value string) string {
  459. func ParseUnix(params ...any) (any, error) {
  460. value := params[0].(string)
  461. t, err := ParseUnixTime(value)
  462. if err != nil {
  463. log.Error(err)
  464. return "", nil
  465. }
  466. return t.(time.Time).Format(time.RFC3339), nil
  467. }
  468. // func ToString(value interface{}) string {
  469. func ToString(params ...any) (any, error) {
  470. value := params[0]
  471. s, ok := value.(string)
  472. if !ok {
  473. return "", nil
  474. }
  475. return s, nil
  476. }
  477. // func GetFromStash(cacheName string, key string) (string, error) {
  478. func GetFromStash(params ...any) (any, error) {
  479. cacheName := params[0].(string)
  480. key := params[1].(string)
  481. return cache.GetKey(cacheName, key)
  482. }
  483. // func SetInStash(cacheName string, key string, value string, expiration *time.Duration) any {
  484. func SetInStash(params ...any) (any, error) {
  485. cacheName := params[0].(string)
  486. key := params[1].(string)
  487. value := params[2].(string)
  488. expiration := params[3].(*time.Duration)
  489. return cache.SetKey(cacheName, key, value, expiration), nil
  490. }
  491. func Sprintf(params ...any) (any, error) {
  492. format := params[0].(string)
  493. return fmt.Sprintf(format, params[1:]...), nil
  494. }
  495. // func Match(pattern, name string) bool {
  496. func Match(params ...any) (any, error) {
  497. var matched bool
  498. pattern := params[0].(string)
  499. name := params[1].(string)
  500. if pattern == "" {
  501. return name == "", nil
  502. }
  503. if name == "" {
  504. if pattern == "*" || pattern == "" {
  505. return true, nil
  506. }
  507. return false, nil
  508. }
  509. if pattern[0] == '*' {
  510. for i := 0; i <= len(name); i++ {
  511. matched, _ := Match(pattern[1:], name[i:])
  512. if matched.(bool) {
  513. return matched, nil
  514. }
  515. }
  516. return matched, nil
  517. }
  518. if pattern[0] == '?' || pattern[0] == name[0] {
  519. return Match(pattern[1:], name[1:])
  520. }
  521. return matched, nil
  522. }
  523. func B64Decode(params ...any) (any, error) {
  524. encoded := params[0].(string)
  525. decoded, err := base64.StdEncoding.DecodeString(encoded)
  526. if err != nil {
  527. return "", err
  528. }
  529. return string(decoded), nil
  530. }
  531. func ParseKV(params ...any) (any, error) {
  532. blob := params[0].(string)
  533. target := params[1].(map[string]interface{})
  534. prefix := params[2].(string)
  535. matches := keyValuePattern.FindAllStringSubmatch(blob, -1)
  536. if matches == nil {
  537. log.Errorf("could not find any key/value pair in line")
  538. return nil, fmt.Errorf("invalid input format")
  539. }
  540. if _, ok := target[prefix]; !ok {
  541. target[prefix] = make(map[string]string)
  542. } else {
  543. _, ok := target[prefix].(map[string]string)
  544. if !ok {
  545. log.Errorf("ParseKV: target is not a map[string]string")
  546. return nil, fmt.Errorf("target is not a map[string]string")
  547. }
  548. }
  549. for _, match := range matches {
  550. key := ""
  551. value := ""
  552. for i, name := range keyValuePattern.SubexpNames() {
  553. if name == "key" {
  554. key = match[i]
  555. } else if name == "quoted_value" && match[i] != "" {
  556. value = match[i]
  557. } else if name == "value" && match[i] != "" {
  558. value = match[i]
  559. }
  560. }
  561. target[prefix].(map[string]string)[key] = value
  562. }
  563. log.Tracef("unmarshaled KV: %+v", target[prefix])
  564. return nil, nil
  565. }
  566. func Hostname(params ...any) (any, error) {
  567. hostname, err := os.Hostname()
  568. if err != nil {
  569. return "", err
  570. }
  571. return hostname, nil
  572. }