| skipped 10 lines |
11 | 11 | | "os" |
12 | 12 | | "strings" |
13 | 13 | | "sync" |
| 14 | + | "reflect" |
14 | 15 | | |
15 | 16 | | "github.com/logrusorgru/aurora" |
16 | 17 | | "github.com/projectdiscovery/iputil" |
| skipped 31 lines |
48 | 49 | | |
49 | 50 | | var verbose bool |
50 | 51 | | flag.BoolVar(&verbose, "v", false, "Verbose") |
| 52 | + | |
| 53 | + | var compareFile string |
| 54 | + | flag.StringVar(&compareFile, "compare", "", "Compare data with a JSON file") |
51 | 55 | | |
52 | 56 | | flag.Parse() |
53 | 57 | | |
| skipped 28 lines |
82 | 86 | | }() |
83 | 87 | | } |
84 | 88 | | |
85 | | - | if jsonFile == "" { |
| 89 | + | if jsonFile == "" && compareFile == "" { |
86 | 90 | | for i := 0; i < len(targets); i++ { |
87 | 91 | | printResult(<-channel, noCPEs, noHostnames, noTags, noVulns, noColor) |
88 | 92 | | } |
89 | 93 | | } |
90 | 94 | | |
91 | | - | wg.Wait() |
92 | | - | close(channel) |
| 95 | + | go func() { |
| 96 | + | wg.Wait() |
| 97 | + | close(channel) |
| 98 | + | }() |
93 | 99 | | |
94 | 100 | | if jsonFile != "" { |
95 | 101 | | saveJson(channel, jsonFile) |
96 | 102 | | return |
97 | 103 | | } |
| 104 | + | |
| 105 | + | if compareFile != "" { |
| 106 | + | newData := make(map[string]Response) |
| 107 | + | for ret := range channel { |
| 108 | + | if ret.IP != "" { |
| 109 | + | newData[ret.IP] = ret |
| 110 | + | } |
| 111 | + | } |
| 112 | + | monitorData(newData, compareFile) |
| 113 | + | |
| 114 | + | var jsonDatas []Response |
| 115 | + | for _, jsonData := range newData { |
| 116 | + | if jsonData.IP != "" { |
| 117 | + | jsonDatas = append(jsonDatas, jsonData) |
| 118 | + | } |
| 119 | + | } |
| 120 | + | if len(jsonDatas) != 0 { |
| 121 | + | stringData, _ := json.Marshal(jsonDatas) |
| 122 | + | _ = ioutil.WriteFile(compareFile, stringData, 0644) |
| 123 | + | } |
| 124 | + | return |
| 125 | + | } |
| 126 | + | } |
| 127 | + | |
| 128 | + | |
| 129 | + | func monitorData(newData map[string]Response, jsonFile string) { |
| 130 | + | |
| 131 | + | var jsonDatas []Response |
| 132 | + | oldData := make(map[string]Response) |
| 133 | + | |
| 134 | + | theFile, err := os.Open(jsonFile) |
| 135 | + | if err != nil { |
| 136 | + | fmt.Println(err) |
| 137 | + | } |
| 138 | + | defer theFile.Close() |
| 139 | + | |
| 140 | + | byteValue, _ := ioutil.ReadAll(theFile) |
| 141 | + | json.Unmarshal(byteValue, &jsonDatas) |
| 142 | + | |
| 143 | + | for _, jsonData := range jsonDatas { |
| 144 | + | oldData[jsonData.IP] = jsonData |
| 145 | + | } |
| 146 | + | |
| 147 | + | for _, nData := range newData { |
| 148 | + | oData, isInOld := oldData[nData.IP] |
| 149 | + | if isInOld { |
| 150 | + | compareData(oData, nData) |
| 151 | + | } else { |
| 152 | + | newPorts := nData.Ports |
| 153 | + | ports := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(newPorts)), ", "), "[]") |
| 154 | + | fmt.Println(nData.IP) |
| 155 | + | fmt.Println(ports) |
| 156 | + | } |
| 157 | + | } |
| 158 | + | |
| 159 | + | return |
| 160 | + | } |
| 161 | + | |
| 162 | + | |
| 163 | + | func compareData(oldData Response, newData Response) { |
| 164 | + | if !reflect.DeepEqual(oldData.Ports, newData.Ports) { |
| 165 | + | for _, nP := range newData.Ports { |
| 166 | + | isNew := true |
| 167 | + | for _, oP := range oldData.Ports { |
| 168 | + | if nP == oP { |
| 169 | + | isNew = false |
| 170 | + | } |
| 171 | + | } |
| 172 | + | if isNew { |
| 173 | + | fmt.Println(newData.IP) |
| 174 | + | fmt.Println(fmt.Sprint(nP) + "\n") |
| 175 | + | } |
| 176 | + | } |
| 177 | + | } |
| 178 | + | if !reflect.DeepEqual(oldData.Vulns, newData.Vulns) { |
| 179 | + | for _, nV := range newData.Vulns { |
| 180 | + | isNew := true |
| 181 | + | for _, oV := range oldData.Vulns { |
| 182 | + | if nV == oV { |
| 183 | + | isNew = false |
| 184 | + | } |
| 185 | + | } |
| 186 | + | if isNew { |
| 187 | + | fmt.Println(newData.IP) |
| 188 | + | fmt.Println(fmt.Sprint(nV) + "\n") |
| 189 | + | } |
| 190 | + | } |
| 191 | + | } |
| 192 | + | return |
98 | 193 | | } |
99 | 194 | | |
100 | 195 | | |
| skipped 139 lines |