exprlib.go 12 KB

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