exprlib.go 11 KB

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