exprlib.go 7.3 KB

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