exprlib.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/c-robinson/iplib"
  14. "github.com/crowdsecurity/crowdsec/pkg/cache"
  15. "github.com/crowdsecurity/crowdsec/pkg/database"
  16. "github.com/davecgh/go-spew/spew"
  17. log "github.com/sirupsen/logrus"
  18. )
  19. var dataFile map[string][]string
  20. var dataFileRegex map[string][]*regexp.Regexp
  21. var dbClient *database.Client
  22. func Get(arr []string, index int) string {
  23. if index >= len(arr) {
  24. return ""
  25. }
  26. return arr[index]
  27. }
  28. func Atof(x string) float64 {
  29. log.Debugf("debug atof %s", x)
  30. ret, err := strconv.ParseFloat(x, 64)
  31. if err != nil {
  32. log.Warningf("Atof : can't convert float '%s' : %v", x, err)
  33. }
  34. return ret
  35. }
  36. func Upper(s string) string {
  37. return strings.ToUpper(s)
  38. }
  39. func Lower(s string) string {
  40. return strings.ToLower(s)
  41. }
  42. func GetExprEnv(ctx map[string]interface{}) map[string]interface{} {
  43. var ExprLib = map[string]interface{}{
  44. "Atof": Atof,
  45. "JsonExtract": JsonExtract,
  46. "JsonExtractUnescape": JsonExtractUnescape,
  47. "JsonExtractLib": JsonExtractLib,
  48. "JsonExtractSlice": JsonExtractSlice,
  49. "JsonExtractObject": JsonExtractObject,
  50. "ToJsonString": ToJson,
  51. "File": File,
  52. "RegexpInFile": RegexpInFile,
  53. "Upper": Upper,
  54. "Lower": Lower,
  55. "IpInRange": IpInRange,
  56. "TimeNow": TimeNow,
  57. "ParseUri": ParseUri,
  58. "PathUnescape": PathUnescape,
  59. "QueryUnescape": QueryUnescape,
  60. "PathEscape": PathEscape,
  61. "QueryEscape": QueryEscape,
  62. "XMLGetAttributeValue": XMLGetAttributeValue,
  63. "XMLGetNodeValue": XMLGetNodeValue,
  64. "IpToRange": IpToRange,
  65. "IsIPV6": IsIPV6,
  66. "LookupHost": LookupHost,
  67. "GetDecisionsCount": GetDecisionsCount,
  68. "GetDecisionsSinceCount": GetDecisionsSinceCount,
  69. "Sprintf": fmt.Sprintf,
  70. "CrowdsecCTI": CrowdsecCTI,
  71. "ParseUnix": ParseUnix,
  72. "GetFromStash": cache.GetKey,
  73. "SetInStash": cache.SetKey,
  74. //go 1.20 "CutPrefix": strings.CutPrefix,
  75. //go 1.20 "CutSuffix": strings.CutSuffix,
  76. //"Cut": strings.Cut, -> returns more than 2 values, not supported by expr
  77. "Fields": strings.Fields,
  78. "Index": strings.Index,
  79. "IndexAny": strings.IndexAny,
  80. "Join": strings.Join,
  81. "Split": strings.Split,
  82. "SplitAfter": strings.SplitAfter,
  83. "SplitAfterN": strings.SplitAfterN,
  84. "SplitN": strings.SplitN,
  85. "Replace": strings.Replace,
  86. "ReplaceAll": strings.ReplaceAll,
  87. "Trim": strings.Trim,
  88. "TrimLeft": strings.TrimLeft,
  89. "TrimRight": strings.TrimRight,
  90. "TrimSpace": strings.TrimSpace,
  91. "TrimPrefix": strings.TrimPrefix,
  92. "TrimSuffix": strings.TrimSuffix,
  93. "Get": Get,
  94. }
  95. for k, v := range ctx {
  96. ExprLib[k] = v
  97. }
  98. return ExprLib
  99. }
  100. func Init(databaseClient *database.Client) error {
  101. dataFile = make(map[string][]string)
  102. dataFileRegex = make(map[string][]*regexp.Regexp)
  103. dbClient = databaseClient
  104. return nil
  105. }
  106. func FileInit(fileFolder string, filename string, fileType string) error {
  107. log.Debugf("init (folder:%s) (file:%s) (type:%s)", fileFolder, filename, fileType)
  108. filepath := path.Join(fileFolder, filename)
  109. file, err := os.Open(filepath)
  110. if err != nil {
  111. return err
  112. }
  113. defer file.Close()
  114. if fileType == "" {
  115. log.Debugf("ignored file %s%s because no type specified", fileFolder, filename)
  116. return nil
  117. }
  118. if _, ok := dataFile[filename]; !ok {
  119. dataFile[filename] = []string{}
  120. }
  121. scanner := bufio.NewScanner(file)
  122. for scanner.Scan() {
  123. if strings.HasPrefix(scanner.Text(), "#") { // allow comments
  124. continue
  125. }
  126. if len(scanner.Text()) == 0 { //skip empty lines
  127. continue
  128. }
  129. switch fileType {
  130. case "regex", "regexp":
  131. dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
  132. case "string":
  133. dataFile[filename] = append(dataFile[filename], scanner.Text())
  134. default:
  135. return fmt.Errorf("unknown data type '%s' for : '%s'", fileType, filename)
  136. }
  137. }
  138. if err := scanner.Err(); err != nil {
  139. return err
  140. }
  141. return nil
  142. }
  143. func QueryEscape(s string) string {
  144. return url.QueryEscape(s)
  145. }
  146. func PathEscape(s string) string {
  147. return url.PathEscape(s)
  148. }
  149. func PathUnescape(s string) string {
  150. ret, err := url.PathUnescape(s)
  151. if err != nil {
  152. log.Debugf("unable to PathUnescape '%s': %+v", s, err)
  153. return s
  154. }
  155. return ret
  156. }
  157. func QueryUnescape(s string) string {
  158. ret, err := url.QueryUnescape(s)
  159. if err != nil {
  160. log.Debugf("unable to QueryUnescape '%s': %+v", s, err)
  161. return s
  162. }
  163. return ret
  164. }
  165. func File(filename string) []string {
  166. if _, ok := dataFile[filename]; ok {
  167. return dataFile[filename]
  168. }
  169. log.Errorf("file '%s' (type:string) not found in expr library", filename)
  170. log.Errorf("expr library : %s", spew.Sdump(dataFile))
  171. return []string{}
  172. }
  173. func RegexpInFile(data string, filename string) bool {
  174. if _, ok := dataFileRegex[filename]; ok {
  175. for _, re := range dataFileRegex[filename] {
  176. if re.Match([]byte(data)) {
  177. return true
  178. }
  179. }
  180. } else {
  181. log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
  182. log.Errorf("expr library : %s", spew.Sdump(dataFileRegex))
  183. }
  184. return false
  185. }
  186. func IpInRange(ip string, ipRange string) bool {
  187. var err error
  188. var ipParsed net.IP
  189. var ipRangeParsed *net.IPNet
  190. ipParsed = net.ParseIP(ip)
  191. if ipParsed == nil {
  192. log.Debugf("'%s' is not a valid IP", ip)
  193. return false
  194. }
  195. if _, ipRangeParsed, err = net.ParseCIDR(ipRange); err != nil {
  196. log.Debugf("'%s' is not a valid IP Range", ipRange)
  197. return false
  198. }
  199. if ipRangeParsed.Contains(ipParsed) {
  200. return true
  201. }
  202. return false
  203. }
  204. func IsIPV6(ip string) bool {
  205. ipParsed := net.ParseIP(ip)
  206. if ipParsed == nil {
  207. log.Debugf("'%s' is not a valid IP", ip)
  208. return false
  209. }
  210. // If it's a valid IP and can't be converted to IPv4 then it is an IPv6
  211. return ipParsed.To4() == nil
  212. }
  213. func IpToRange(ip string, cidr string) string {
  214. cidr = strings.TrimPrefix(cidr, "/")
  215. mask, err := strconv.Atoi(cidr)
  216. if err != nil {
  217. log.Errorf("bad cidr '%s': %s", cidr, err)
  218. return ""
  219. }
  220. ipAddr := net.ParseIP(ip)
  221. if ipAddr == nil {
  222. log.Errorf("can't parse IP address '%s'", ip)
  223. return ""
  224. }
  225. ipRange := iplib.NewNet(ipAddr, mask)
  226. if ipRange.IP() == nil {
  227. log.Errorf("can't get cidr '%s' of '%s'", cidr, ip)
  228. return ""
  229. }
  230. return ipRange.String()
  231. }
  232. func TimeNow() string {
  233. return time.Now().UTC().Format(time.RFC3339)
  234. }
  235. func ParseUri(uri string) map[string][]string {
  236. ret := make(map[string][]string)
  237. u, err := url.Parse(uri)
  238. if err != nil {
  239. log.Errorf("Could not parse URI: %s", err)
  240. return ret
  241. }
  242. parsed, err := url.ParseQuery(u.RawQuery)
  243. if err != nil {
  244. log.Errorf("Could not parse query uri : %s", err)
  245. return ret
  246. }
  247. for k, v := range parsed {
  248. ret[k] = v
  249. }
  250. return ret
  251. }
  252. func KeyExists(key string, dict map[string]interface{}) bool {
  253. _, ok := dict[key]
  254. return ok
  255. }
  256. func GetDecisionsCount(value string) int {
  257. if dbClient == nil {
  258. log.Error("No database config to call GetDecisionsCount()")
  259. return 0
  260. }
  261. count, err := dbClient.CountDecisionsByValue(value)
  262. if err != nil {
  263. log.Errorf("Failed to get decisions count from value '%s'", value)
  264. return 0
  265. }
  266. return count
  267. }
  268. func GetDecisionsSinceCount(value string, since string) int {
  269. if dbClient == nil {
  270. log.Error("No database config to call GetDecisionsCount()")
  271. return 0
  272. }
  273. sinceDuration, err := time.ParseDuration(since)
  274. if err != nil {
  275. log.Errorf("Failed to parse since parameter '%s' : %s", since, err)
  276. return 0
  277. }
  278. sinceTime := time.Now().UTC().Add(-sinceDuration)
  279. count, err := dbClient.CountDecisionsSinceByValue(value, sinceTime)
  280. if err != nil {
  281. log.Errorf("Failed to get decisions count from value '%s'", value)
  282. return 0
  283. }
  284. return count
  285. }
  286. func LookupHost(value string) []string {
  287. addresses, err := net.LookupHost(value)
  288. if err != nil {
  289. log.Errorf("Failed to lookup host '%s' : %s", value, err)
  290. return []string{}
  291. }
  292. return addresses
  293. }
  294. func ParseUnixTime(value string) (time.Time, error) {
  295. //Splitting string here as some unix timestamp may have milliseconds and break ParseInt
  296. i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64)
  297. if err != nil || i <= 0 {
  298. return time.Time{}, fmt.Errorf("unable to parse %s as unix timestamp", value)
  299. }
  300. return time.Unix(i, 0), nil
  301. }
  302. func ParseUnix(value string) string {
  303. t, err := ParseUnixTime(value)
  304. if err != nil {
  305. log.Error(err)
  306. return ""
  307. }
  308. return t.Format(time.RFC3339)
  309. }