api.go 8.0 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. upper = "ABCDEFGHIJKLMNOPQRSTUVWXY"
  20. lower = "abcdefghijklmnopqrstuvwxyz"
  21. digits = "0123456789"
  22. )
  23. var (
  24. userID string // for flag parsing
  25. outputCTX *outputs.Output
  26. )
  27. const (
  28. uuid = "/proc/sys/kernel/random/uuid"
  29. apiConfigFile = "api.yaml"
  30. )
  31. func dumpCredentials() error {
  32. if config.output == "json" {
  33. credsYaml, err := json.Marshal(&outputCTX.API.Creds)
  34. if err != nil {
  35. log.Fatalf("Can't marshal credentials : %v", err)
  36. }
  37. fmt.Printf("%s\n", string(credsYaml))
  38. } else {
  39. credsYaml, err := yaml.Marshal(&outputCTX.API.Creds)
  40. if err != nil {
  41. log.Fatalf("Can't marshal credentials : %v", err)
  42. }
  43. fmt.Printf("%s\n", string(credsYaml))
  44. }
  45. return nil
  46. }
  47. func generatePassword(passwordLength int) string {
  48. rand.Seed(time.Now().UnixNano())
  49. charset := upper + lower + digits
  50. buf := make([]byte, passwordLength)
  51. buf[0] = digits[rand.Intn(len(digits))]
  52. buf[1] = upper[rand.Intn(len(upper))]
  53. buf[2] = lower[rand.Intn(len(lower))]
  54. for i := 3; i < passwordLength; i++ {
  55. buf[i] = charset[rand.Intn(len(charset))]
  56. }
  57. rand.Shuffle(len(buf), func(i, j int) {
  58. buf[i], buf[j] = buf[j], buf[i]
  59. })
  60. return string(buf)
  61. }
  62. func pullTOP() error {
  63. /*profile from cwhub*/
  64. var profiles []string
  65. if _, ok := cwhub.HubIdx[cwhub.SCENARIOS]; !ok || len(cwhub.HubIdx[cwhub.SCENARIOS]) == 0 {
  66. log.Errorf("no loaded scenarios, can't fill profiles")
  67. return fmt.Errorf("no profiles")
  68. }
  69. for _, item := range cwhub.HubIdx[cwhub.SCENARIOS] {
  70. if item.Tainted || !item.Installed {
  71. continue
  72. }
  73. profiles = append(profiles, item.Name)
  74. }
  75. outputCTX.API.Creds.Profile = strings.Join(profiles[:], ",")
  76. if err := outputCTX.API.Signin(); err != nil {
  77. log.Fatalf(err.Error())
  78. }
  79. ret, err := outputCTX.API.PullTop()
  80. if err != nil {
  81. log.Fatalf(err.Error())
  82. }
  83. log.Warningf("api pull returned %d entries", len(ret))
  84. for _, item := range ret {
  85. if _, ok := item["range_ip"]; !ok {
  86. continue
  87. }
  88. if _, ok := item["scenario"]; !ok {
  89. continue
  90. }
  91. if _, ok := item["action"]; !ok {
  92. continue
  93. }
  94. if _, ok := item["expiration"]; !ok {
  95. continue
  96. }
  97. if _, ok := item["country"]; !ok {
  98. item["country"] = ""
  99. }
  100. if _, ok := item["as_org"]; !ok {
  101. item["as_org"] = ""
  102. }
  103. if _, ok := item["as_num"]; !ok {
  104. item["as_num"] = ""
  105. }
  106. var signalOcc types.SignalOccurence
  107. signalOcc, err = simpleBanToSignal(item["range_ip"], item["scenario"], item["expiration"], item["action"], item["as_name"], item["as_num"], item["country"], "api")
  108. if err != nil {
  109. return fmt.Errorf("failed to convert ban to signal : %s", err)
  110. }
  111. if err := outputCTX.Insert(signalOcc); err != nil {
  112. log.Fatalf("Unable to write pull to Database : %+s", err.Error())
  113. }
  114. }
  115. outputCTX.Flush()
  116. log.Infof("Wrote %d bans from api to database.", len(ret))
  117. return nil
  118. }
  119. func NewAPICmd() *cobra.Command {
  120. var cmdAPI = &cobra.Command{
  121. Use: "api [action]",
  122. Short: "Crowdsec API interaction",
  123. Long: `
  124. Allow to register your machine into crowdsec API to send and receive signal.
  125. `,
  126. Example: `
  127. cscli api register # Register to Crowdsec API
  128. cscli api pull # Pull malevolant IPs from Crowdsec API
  129. cscli api reset # Reset your machines credentials
  130. cscli api enroll # Enroll your machine to the user account you created on Crowdsec backend
  131. cscli api credentials # Display your API credentials
  132. `,
  133. Args: cobra.MinimumNArgs(1),
  134. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  135. var err error
  136. if !config.configured {
  137. return fmt.Errorf("you must configure cli before interacting with hub")
  138. }
  139. outputConfig := outputs.OutputFactory{
  140. BackendFolder: config.BackendPluginFolder,
  141. Flush: false,
  142. }
  143. outputCTX, err = outputs.NewOutput(&outputConfig)
  144. if err != nil {
  145. return err
  146. }
  147. err = outputCTX.LoadAPIConfig(path.Join(config.InstallFolder, apiConfigFile))
  148. if err != nil {
  149. return err
  150. }
  151. return nil
  152. },
  153. }
  154. var cmdAPIRegister = &cobra.Command{
  155. Use: "register",
  156. Short: "Register on Crowdsec API",
  157. Long: `This command will register your machine to crowdsec API to allow you to receive list of malveolent IPs.
  158. The printed machine_id and password should be added to your api.yaml file.`,
  159. Example: `cscli api register`,
  160. Args: cobra.MinimumNArgs(0),
  161. Run: func(cmd *cobra.Command, args []string) {
  162. id, err := machineid.ID()
  163. if err != nil {
  164. log.Debugf("failed to get machine-id with usual files : %s", err)
  165. }
  166. if id == "" || err != nil {
  167. bID, err := ioutil.ReadFile(uuid)
  168. if err != nil {
  169. log.Fatalf("can'get a valid machine_id")
  170. }
  171. id = string(bID)
  172. id = strings.ReplaceAll(id, "-", "")[:32]
  173. }
  174. password := generatePassword(64)
  175. if err := outputCTX.API.RegisterMachine(fmt.Sprintf("%s%s", id, generatePassword(16)), 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. fmt.Printf("#You need to append these credentials in /etc/crowdsec/config/api.yaml")
  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(64)
  218. if err := outputCTX.API.ResetPassword(fmt.Sprintf("%s%s", id, generatePassword(16)), 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. }