api.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "path"
  8. "strings"
  9. "time"
  10. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  11. "github.com/crowdsecurity/crowdsec/pkg/outputs"
  12. "github.com/crowdsecurity/crowdsec/pkg/types"
  13. "github.com/denisbrodbeck/machineid"
  14. log "github.com/sirupsen/logrus"
  15. "github.com/spf13/cobra"
  16. "gopkg.in/yaml.v2"
  17. )
  18. var (
  19. passwordLength = 64
  20. upper = "ABCDEFGHIJKLMNOPQRSTUVWXY"
  21. lower = "abcdefghijklmnopqrstuvwxyz"
  22. digits = "0123456789"
  23. )
  24. var (
  25. userID string // for flag parsing
  26. outputCTX *outputs.Output
  27. )
  28. const (
  29. uuid = "/proc/sys/kernel/random/uuid"
  30. apiConfigFile = "api.yaml"
  31. )
  32. func dumpCredentials() error {
  33. if config.output == "json" {
  34. credsYaml, err := json.Marshal(&outputCTX.API.Creds)
  35. if err != nil {
  36. log.Fatalf("Can't marshal credentials : %v", err)
  37. }
  38. fmt.Printf("%s\n", string(credsYaml))
  39. } else {
  40. credsYaml, err := yaml.Marshal(&outputCTX.API.Creds)
  41. if err != nil {
  42. log.Fatalf("Can't marshal credentials : %v", err)
  43. }
  44. fmt.Printf("%s\n", string(credsYaml))
  45. }
  46. return nil
  47. }
  48. func generatePassword() string {
  49. rand.Seed(time.Now().UnixNano())
  50. charset := upper + lower + digits
  51. buf := make([]byte, passwordLength)
  52. buf[0] = digits[rand.Intn(len(digits))]
  53. buf[1] = upper[rand.Intn(len(upper))]
  54. buf[2] = lower[rand.Intn(len(lower))]
  55. for i := 3; i < passwordLength; i++ {
  56. buf[i] = charset[rand.Intn(len(charset))]
  57. }
  58. rand.Shuffle(len(buf), func(i, j int) {
  59. buf[i], buf[j] = buf[j], buf[i]
  60. })
  61. return string(buf)
  62. }
  63. func pullTOP() error {
  64. /*profile from cwhub*/
  65. var profiles []string
  66. if _, ok := cwhub.HubIdx[cwhub.SCENARIOS]; !ok || len(cwhub.HubIdx[cwhub.SCENARIOS]) == 0 {
  67. log.Errorf("no loaded scenarios, can't fill profiles")
  68. return fmt.Errorf("no profiles")
  69. }
  70. for _, item := range cwhub.HubIdx[cwhub.SCENARIOS] {
  71. if item.Tainted || !item.Installed {
  72. continue
  73. }
  74. profiles = append(profiles, item.Name)
  75. }
  76. outputCTX.API.Creds.Profile = strings.Join(profiles[:], ",")
  77. if err := outputCTX.API.Signin(); err != nil {
  78. log.Fatalf(err.Error())
  79. }
  80. ret, err := outputCTX.API.PullTop()
  81. if err != nil {
  82. log.Fatalf(err.Error())
  83. }
  84. log.Warningf("api pull returned %d entries", len(ret))
  85. for _, item := range ret {
  86. if _, ok := item["range_ip"]; !ok {
  87. continue
  88. }
  89. if _, ok := item["scenario"]; !ok {
  90. continue
  91. }
  92. if _, ok := item["action"]; !ok {
  93. continue
  94. }
  95. if _, ok := item["expiration"]; !ok {
  96. continue
  97. }
  98. if _, ok := item["country"]; !ok {
  99. item["country"] = ""
  100. }
  101. if _, ok := item["as_org"]; !ok {
  102. item["as_org"] = ""
  103. }
  104. if _, ok := item["as_num"]; !ok {
  105. item["as_num"] = ""
  106. }
  107. var signalOcc types.SignalOccurence
  108. signalOcc, err = simpleBanToSignal(item["range_ip"], item["scenario"], item["expiration"], item["action"], item["as_name"], item["as_num"], item["country"], "api")
  109. if err != nil {
  110. return fmt.Errorf("failed to convert ban to signal : %s", err)
  111. }
  112. if err := outputCTX.Insert(signalOcc); err != nil {
  113. log.Fatalf("Unable to write pull to sqliteDB : %+s", err.Error())
  114. }
  115. }
  116. outputCTX.Flush()
  117. log.Infof("Wrote %d bans from api to database.", len(ret))
  118. return nil
  119. }
  120. func NewAPICmd() *cobra.Command {
  121. var cmdAPI = &cobra.Command{
  122. Use: "api [action]",
  123. Short: "Crowdsec API interaction",
  124. Long: `
  125. Allow to register your machine into crowdsec API to send and receive signal.
  126. `,
  127. Example: `
  128. cscli api register # Register to Crowdsec API
  129. cscli api pull # Pull malevolant IPs from Crowdsec API
  130. cscli api reset # Reset your machines credentials
  131. cscli api enroll # Enroll your machine to the user account you created on Crowdsec backend
  132. cscli api credentials # Display your API credentials
  133. `,
  134. Args: cobra.MinimumNArgs(1),
  135. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  136. var err error
  137. if !config.configured {
  138. return fmt.Errorf("you must configure cli before interacting with hub")
  139. }
  140. outputConfig := outputs.OutputFactory{
  141. BackendFolder: config.BackendPluginFolder,
  142. Flush: false,
  143. }
  144. outputCTX, err = outputs.NewOutput(&outputConfig)
  145. if err != nil {
  146. return err
  147. }
  148. err = outputCTX.LoadAPIConfig(path.Join(config.InstallFolder, apiConfigFile))
  149. if err != nil {
  150. return err
  151. }
  152. return nil
  153. },
  154. }
  155. var cmdAPIRegister = &cobra.Command{
  156. Use: "register",
  157. Short: "Register on Crowdsec API",
  158. Long: `This command will register your machine to crowdsec API to allow you to receive list of malveolent IPs.
  159. The printed machine_id and password should be added to your api.yaml file.`,
  160. Example: `cscli api register`,
  161. Args: cobra.MinimumNArgs(0),
  162. Run: func(cmd *cobra.Command, args []string) {
  163. id, err := machineid.ID()
  164. if err != nil {
  165. log.Debugf("failed to get machine-id with usual files : %s", err)
  166. }
  167. if id == "" || err != nil {
  168. bID, err := ioutil.ReadFile(uuid)
  169. if err != nil {
  170. log.Fatalf("can'get a valid machine_id")
  171. }
  172. id = string(bID)
  173. id = strings.ReplaceAll(id, "-", "")[:32]
  174. }
  175. password := generatePassword()
  176. if err := outputCTX.API.RegisterMachine(id, password); err != nil {
  177. log.Fatalf(err.Error())
  178. }
  179. fmt.Printf("machine_id: %s\n", outputCTX.API.Creds.User)
  180. fmt.Printf("password: %s\n", outputCTX.API.Creds.Password)
  181. },
  182. }
  183. var cmdAPIEnroll = &cobra.Command{
  184. Use: "enroll",
  185. Short: "Associate your machine to an existing crowdsec user",
  186. Long: `Enrolling your machine into your user account will allow for more accurate lists and threat detection. See website to create user account.`,
  187. Example: `cscli api enroll -u 1234567890ffff`,
  188. Args: cobra.MinimumNArgs(0),
  189. Run: func(cmd *cobra.Command, args []string) {
  190. if err := outputCTX.API.Signin(); err != nil {
  191. log.Fatalf("unable to signin : %s", err)
  192. }
  193. if err := outputCTX.API.Enroll(userID); err != nil {
  194. log.Fatalf(err.Error())
  195. }
  196. },
  197. }
  198. var cmdAPIResetPassword = &cobra.Command{
  199. Use: "reset",
  200. Short: "Reset password on CrowdSec API",
  201. Long: `Attempts to reset your credentials to the API.`,
  202. Example: `cscli api reset`,
  203. Args: cobra.MinimumNArgs(0),
  204. Run: func(cmd *cobra.Command, args []string) {
  205. id, err := machineid.ID()
  206. if err != nil {
  207. log.Debugf("failed to get machine-id with usual files : %s", err)
  208. }
  209. if id == "" || err != nil {
  210. bID, err := ioutil.ReadFile(uuid)
  211. if err != nil {
  212. log.Fatalf("can'get a valid machine_id")
  213. }
  214. id = string(bID)
  215. id = strings.ReplaceAll(id, "-", "")[:32]
  216. }
  217. password := generatePassword()
  218. if err := outputCTX.API.ResetPassword(id, password); err != nil {
  219. log.Fatalf(err.Error())
  220. }
  221. fmt.Printf("machine_id: %s\n", outputCTX.API.Creds.User)
  222. fmt.Printf("password: %s\n", outputCTX.API.Creds.Password)
  223. },
  224. }
  225. var cmdAPIPull = &cobra.Command{
  226. Use: "pull",
  227. Short: "Pull crowdsec API TopX",
  228. Long: `Pulls a list of malveolent IPs relevant to your situation and add them into the local ban database.`,
  229. Example: `cscli api pull`,
  230. Args: cobra.MinimumNArgs(0),
  231. Run: func(cmd *cobra.Command, args []string) {
  232. if err := cwhub.GetHubIdx(); err != nil {
  233. log.Fatalf(err.Error())
  234. }
  235. err := pullTOP()
  236. if err != nil {
  237. log.Fatalf(err.Error())
  238. }
  239. },
  240. }
  241. var cmdAPICreds = &cobra.Command{
  242. Use: "credentials",
  243. Short: "Display api credentials",
  244. Long: ``,
  245. Example: `cscli api credentials`,
  246. Args: cobra.MinimumNArgs(0),
  247. Run: func(cmd *cobra.Command, args []string) {
  248. if err := dumpCredentials(); err != nil {
  249. log.Fatalf(err.Error())
  250. }
  251. },
  252. }
  253. cmdAPI.AddCommand(cmdAPICreds)
  254. cmdAPIEnroll.Flags().StringVarP(&userID, "user", "u", "", "User ID (required)")
  255. if err := cmdAPIEnroll.MarkFlagRequired("user"); err != nil {
  256. log.Errorf("'user' flag : %s", err)
  257. }
  258. cmdAPI.AddCommand(cmdAPIEnroll)
  259. cmdAPI.AddCommand(cmdAPIResetPassword)
  260. cmdAPI.AddCommand(cmdAPIRegister)
  261. cmdAPI.AddCommand(cmdAPIPull)
  262. return cmdAPI
  263. }