helpers.go 15 KB

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