main.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "time"
  8. )
  9. func main() {
  10. today := 0
  11. if len(os.Args) > 2 {
  12. today = 1
  13. }
  14. type CVE_Json struct {
  15. CVEDataType string `json:"CVE_data_type"`
  16. CVEDataFormat string `json:"CVE_data_format"`
  17. CVEDataVersion string `json:"CVE_data_version"`
  18. CVEDataNumberOfCVEs string `json:"CVE_data_numberOfCVEs"`
  19. CVEDataTimestamp string `json:"CVE_data_timestamp"`
  20. CVEItems []struct {
  21. Cve struct {
  22. DataType string `json:"data_type"`
  23. DataFormat string `json:"data_format"`
  24. DataVersion string `json:"data_version"`
  25. CVEDataMeta struct {
  26. ID string `json:"ID"`
  27. ASSIGNER string `json:"ASSIGNER"`
  28. } `json:"CVE_data_meta"`
  29. Problemtype struct {
  30. ProblemtypeData []struct {
  31. Description []struct {
  32. Lang string `json:"lang"`
  33. Value string `json:"value"`
  34. } `json:"description"`
  35. } `json:"problemtype_data"`
  36. } `json:"problemtype"`
  37. References struct {
  38. ReferenceData []struct {
  39. URL string `json:"url"`
  40. Name string `json:"name"`
  41. Refsource string `json:"refsource"`
  42. Tags []string `json:"tags"`
  43. } `json:"reference_data"`
  44. } `json:"references"`
  45. Description struct {
  46. DescriptionData []struct {
  47. Lang string `json:"lang"`
  48. Value string `json:"value"`
  49. } `json:"description_data"`
  50. } `json:"description"`
  51. } `json:"cve"`
  52. Configurations struct {
  53. CVEDataVersion string `json:"CVE_data_version"`
  54. Nodes []struct {
  55. Operator string `json:"operator"`
  56. CpeMatch []struct {
  57. Vulnerable bool `json:"vulnerable"`
  58. Cpe23URI string `json:"cpe23Uri"`
  59. VersionEndExcluding string `json:"versionEndExcluding"`
  60. } `json:"cpe_match"`
  61. } `json:"nodes"`
  62. } `json:"configurations"`
  63. Impact struct {
  64. BaseMetricV3 struct {
  65. CvssV3 struct {
  66. Version string `json:"version"`
  67. VectorString string `json:"vectorString"`
  68. AttackVector string `json:"attackVector"`
  69. AttackComplexity string `json:"attackComplexity"`
  70. PrivilegesRequired string `json:"privilegesRequired"`
  71. UserInteraction string `json:"userInteraction"`
  72. Scope string `json:"scope"`
  73. ConfidentialityImpact string `json:"confidentialityImpact"`
  74. IntegrityImpact string `json:"integrityImpact"`
  75. AvailabilityImpact string `json:"availabilityImpact"`
  76. BaseScore float64 `json:"baseScore"`
  77. BaseSeverity string `json:"baseSeverity"`
  78. } `json:"cvssV3"`
  79. ExploitabilityScore float64 `json:"exploitabilityScore"`
  80. ImpactScore float64 `json:"impactScore"`
  81. } `json:"baseMetricV3"`
  82. BaseMetricV2 struct {
  83. CvssV2 struct {
  84. Version string `json:"version"`
  85. VectorString string `json:"vectorString"`
  86. AccessVector string `json:"accessVector"`
  87. AccessComplexity string `json:"accessComplexity"`
  88. Authentication string `json:"authentication"`
  89. ConfidentialityImpact string `json:"confidentialityImpact"`
  90. IntegrityImpact string `json:"integrityImpact"`
  91. AvailabilityImpact string `json:"availabilityImpact"`
  92. BaseScore float64 `json:"baseScore"`
  93. } `json:"cvssV2"`
  94. Severity string `json:"severity"`
  95. ExploitabilityScore float64 `json:"exploitabilityScore"`
  96. ImpactScore float64 `json:"impactScore"`
  97. AcInsufInfo bool `json:"acInsufInfo"`
  98. ObtainAllPrivilege bool `json:"obtainAllPrivilege"`
  99. ObtainUserPrivilege bool `json:"obtainUserPrivilege"`
  100. ObtainOtherPrivilege bool `json:"obtainOtherPrivilege"`
  101. UserInteractionRequired bool `json:"userInteractionRequired"`
  102. } `json:"baseMetricV2"`
  103. } `json:"impact"`
  104. PublishedDate string `json:"publishedDate"`
  105. LastModifiedDate string `json:"lastModifiedDate"`
  106. } `json:"CVE_Items"`
  107. }
  108. file, _ := ioutil.ReadFile(os.Args[1])
  109. data := CVE_Json{}
  110. _ = json.Unmarshal([]byte(file), &data)
  111. // fmt.Println("error:", _err, _err.Error())
  112. type CVE struct {
  113. Title string `json:"Title"`
  114. ID string `json:"id"`
  115. Descr string `json:"descr"`
  116. BaseScoreV2 string `json:"basescorev2"`
  117. BaseScoreV3 string `json:"basescorev3"`
  118. SeverityV2 string `json:"severityv2"`
  119. SeverityV3 string `json:"severityv3"`
  120. NvdURL string `json:"nvdurl"`
  121. DebianURL string `json:"Debianurl"`
  122. RHURL string `json:"rhdurl"`
  123. UbuntuURL string `json:"ubuntuurl"`
  124. ModifiedDate time.Time `json:modifieddate`
  125. PublishedDate time.Time `json:publisheddate`
  126. //Age string `json:age`
  127. }
  128. const layout = "2006-01-02T15:04Z"
  129. dt := time.Now().Add(-24 * time.Hour * 3)
  130. //loc, _ := time.LoadLocation("UTC")
  131. //now := time.Now().In(loc)
  132. for _, v := range data.CVEItems {
  133. md, err := time.Parse(layout, v.LastModifiedDate)
  134. if err != nil {
  135. fmt.Println("Error", err.Error())
  136. }
  137. pd, _err := time.Parse(layout, v.PublishedDate)
  138. // fmt.Println(time.Date(pd.Year(), pd.Month(), pd.Day(), 0, 0, 0, 0, pd.Location()))
  139. // if time.Date(pd.Year(), pd.Month(), pd.Day(), 0, 0, 0, 0, pd.Location()) != time.Date(time.Now().AddDate(0, 0, -1).Year(), time.Now().AddDate(0, 0, -1).Month(), time.Now().AddDate(0, 0, -1).Day(), 0, 0, 0, 0, time.Now().AddDate(0, 0, -1).Location()) || time.Date(md.Year(), md.Month(), md.Day(), 0, 0, 0, 0, md.Location()) != time.Date(time.Now().AddDate(0, 0, -1).Year(), time.Now().AddDate(0, 0, -1).Month(), time.Now().AddDate(0, 0, -1).Day(), 0, 0, 0, 0, time.Now().AddDate(0, 0, -1).Location()) {
  140. if pd.Year() < 2020 {
  141. if today == 1 {
  142. continue
  143. }
  144. }
  145. if pd.Before(dt) && md.Before(dt) {
  146. if today == 1 {
  147. continue
  148. }
  149. }
  150. if _err != nil {
  151. fmt.Println("Error", err.Error())
  152. }
  153. cve := CVE{}
  154. if v.Impact.BaseMetricV2.CvssV2.BaseScore < 1 && v.Impact.BaseMetricV3.CvssV3.BaseScore < 1 {
  155. continue
  156. }
  157. for _, x := range v.Cve.Description.DescriptionData {
  158. cve.Descr = x.Value
  159. break
  160. }
  161. cve.ModifiedDate = md
  162. cve.PublishedDate = pd
  163. cve.ID = v.Cve.CVEDataMeta.ID
  164. cve.BaseScoreV2 = fmt.Sprintf("%f", v.Impact.BaseMetricV2.CvssV2.BaseScore)
  165. cve.BaseScoreV3 = fmt.Sprintf("%f", v.Impact.BaseMetricV3.CvssV3.BaseScore)
  166. cve.SeverityV2 = v.Impact.BaseMetricV2.Severity
  167. cve.SeverityV3 = v.Impact.BaseMetricV3.CvssV3.BaseSeverity
  168. cve.NvdURL = "https://nvd.nist.gov/vuln/detail/" + cve.ID
  169. cve.DebianURL = "https://security-tracker.debian.org/tracker/" + cve.ID
  170. cve.RHURL = "https://bugzilla.redhat.com/show_bug.cgi?id=" + cve.ID
  171. cve.UbuntuURL = "https://ubuntu.com/security/" + cve.ID
  172. // cve.Age = md.String()
  173. data, _ := json.MarshalIndent(cve, " ", "")
  174. fmt.Println(string(data))
  175. }
  176. }