api.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. item["scenario"] = fmt.Sprintf("api: %s", item["scenario"])
  93. if _, ok := item["action"]; !ok {
  94. continue
  95. }
  96. if _, ok := item["expiration"]; !ok {
  97. continue
  98. }
  99. if _, ok := item["country"]; !ok {
  100. item["country"] = ""
  101. }
  102. if _, ok := item["as_org"]; !ok {
  103. item["as_org"] = ""
  104. }
  105. if _, ok := item["as_num"]; !ok {
  106. item["as_num"] = ""
  107. }
  108. var signalOcc types.SignalOccurence
  109. signalOcc, err = simpleBanToSignal(item["range_ip"], item["scenario"], item["expiration"], item["action"], item["as_name"], item["as_num"], item["country"], "api")
  110. if err != nil {
  111. return fmt.Errorf("failed to convert ban to signal : %s", err)
  112. }
  113. if err := outputCTX.Insert(signalOcc); err != nil {
  114. log.Fatalf("Unable to write pull to sqliteDB : %+s", err.Error())
  115. }
  116. }
  117. outputCTX.Flush()
  118. log.Infof("Wrote %d bans from api to database.", len(ret))
  119. return nil
  120. }
  121. func NewAPICmd() *cobra.Command {
  122. var cmdAPI = &cobra.Command{
  123. Use: "api [action]",
  124. Short: "Crowdsec API interaction",
  125. Long: `
  126. Allow to register your machine into crowdsec API to send and receive signal.
  127. `,
  128. Example: `
  129. cscli api register # Register to Crowdsec API
  130. cscli api pull # Pull malevolant IPs from Crowdsec API
  131. cscli api reset # Reset your machines credentials
  132. cscli api enroll # Enroll your machine to the user account you created on Crowdsec backend
  133. cscli api credentials # Display your API credentials
  134. `,
  135. Args: cobra.MinimumNArgs(1),
  136. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  137. var err error
  138. if !config.configured {
  139. return fmt.Errorf("you must configure cli before interacting with hub")
  140. }
  141. outputConfig := outputs.OutputFactory{
  142. BackendFolder: config.BackendPluginFolder,
  143. }
  144. outputCTX, err = outputs.NewOutput(&outputConfig, false)
  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.Fatalf("failed to get machine id: %s", err)
  166. }
  167. if id == "" {
  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. }
  174. password := generatePassword()
  175. if err := outputCTX.API.RegisterMachine(id, password); err != nil {
  176. log.Fatalf(err.Error())
  177. }
  178. fmt.Printf("machine_id: %s\n", outputCTX.API.Creds.User)
  179. fmt.Printf("password: %s\n", outputCTX.API.Creds.Password)
  180. },
  181. }
  182. var cmdAPIEnroll = &cobra.Command{
  183. Use: "enroll",
  184. Short: "Associate your machine to an existing crowdsec user",
  185. Long: `Enrolling your machine into your user account will allow for more accurate lists and threat detection. See website to create user account.`,
  186. Example: `cscli api enroll -u 1234567890ffff`,
  187. Args: cobra.MinimumNArgs(0),
  188. Run: func(cmd *cobra.Command, args []string) {
  189. if err := outputCTX.API.Signin(); err != nil {
  190. log.Fatalf("unable to signin : %s", err)
  191. }
  192. if err := outputCTX.API.Enroll(userID); err != nil {
  193. log.Fatalf(err.Error())
  194. }
  195. },
  196. }
  197. var cmdAPIResetPassword = &cobra.Command{
  198. Use: "reset",
  199. Short: "Reset password on CrowdSec API",
  200. Long: `Attempts to reset your credentials to the API.`,
  201. Example: `cscli api reset`,
  202. Args: cobra.MinimumNArgs(0),
  203. Run: func(cmd *cobra.Command, args []string) {
  204. id, err := machineid.ID()
  205. if err != nil {
  206. log.Fatalf("failed to get machine id: %s", err)
  207. }
  208. if id == "" {
  209. bID, err := ioutil.ReadFile(uuid)
  210. if err != nil {
  211. log.Fatalf("can'get a valid machine_id")
  212. }
  213. id = string(bID)
  214. }
  215. password := generatePassword()
  216. if err := outputCTX.API.ResetPassword(id, password); err != nil {
  217. log.Fatalf(err.Error())
  218. }
  219. fmt.Printf("machine_id: %s\n", outputCTX.API.Creds.User)
  220. fmt.Printf("password: %s\n", outputCTX.API.Creds.Password)
  221. },
  222. }
  223. var cmdAPIPull = &cobra.Command{
  224. Use: "pull",
  225. Short: "Pull crowdsec API TopX",
  226. Long: `Pulls a list of malveolent IPs relevant to your situation and add them into the local ban database.`,
  227. Example: `cscli api pull`,
  228. Args: cobra.MinimumNArgs(0),
  229. Run: func(cmd *cobra.Command, args []string) {
  230. if err := cwhub.GetHubIdx(); err != nil {
  231. log.Fatalf(err.Error())
  232. }
  233. err := pullTOP()
  234. if err != nil {
  235. log.Fatalf(err.Error())
  236. }
  237. },
  238. }
  239. var cmdAPICreds = &cobra.Command{
  240. Use: "credentials",
  241. Short: "Display api credentials",
  242. Long: ``,
  243. Example: `cscli api credentials`,
  244. Args: cobra.MinimumNArgs(0),
  245. Run: func(cmd *cobra.Command, args []string) {
  246. if err := dumpCredentials(); err != nil {
  247. log.Fatalf(err.Error())
  248. }
  249. },
  250. }
  251. cmdAPI.AddCommand(cmdAPICreds)
  252. cmdAPIEnroll.Flags().StringVarP(&userID, "user", "u", "", "User ID (required)")
  253. if err := cmdAPIEnroll.MarkFlagRequired("user"); err != nil {
  254. log.Errorf("'user' flag : %s", err)
  255. }
  256. cmdAPI.AddCommand(cmdAPIEnroll)
  257. cmdAPI.AddCommand(cmdAPIResetPassword)
  258. cmdAPI.AddCommand(cmdAPIRegister)
  259. cmdAPI.AddCommand(cmdAPIPull)
  260. return cmdAPI
  261. }