1 using Newtonsoft.Json; 2 using Newtonsoft.Json.Linq; 3 using System; 4 using System.IO; 5 using System.Net; 6 using System.Net.Security; 7 using System.Security.Cryptography.X509Certificates; 8 using System.Text; 9 using System.Windows.Forms; 10 11 namespace 批量添加AWVS默认扫描 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 string urls; 21 22 private void button1_Click(object sender, EventArgs e) 23 { 24 //文件类型过滤 25 openFileDialog1.Filter = "文本文件(*.txt)|*.txt"; 26 if (openFileDialog1.ShowDialog() == DialogResult.OK) 27 { 28 //得到文件路径全名 29 this.textBox1.Text = openFileDialog1.FileName; 30 31 string strfromtxt = File.ReadAllText(this.textBox1.Text, Encoding.GetEncoding("GB2312")); 32 33 urls = strfromtxt; 34 35 string[] ab = strfromtxt.Split(new string[] { "\r\n" }, StringSplitOptions.None); 36 37 MessageBox.Show("共上传" + ab.Length + "个目标"); 38 } 39 } 40 41 public static int GoAwvs(string url, string a) 42 { 43 //if (url.IndexOf("443") > 0) 44 //{ 45 // url = "https://" + url; 46 //} 47 //else 48 //{ 49 // url = "http://" + url; 50 //} 51 52 url = "http://" + url; 53 54 //固定cooke 目前解决COOKIE获取 55 string cookie = "ui_session=" + a; 56 //第一步 获取target_id 57 string param = "{\"address\":\"" + url + "\",\"description\":\"\",\"criticality\":\"10\"}"; 58 59 string target_id = GetTarget_id(cookie, param, a); 60 61 //string param2 = "{\"target_id\":\"" + target_id + "\",\"profile_id\":\"11111111-1111-1111-1111-111111111113\",\"schedule\":{\"disable\":false,\"start_date\":null,\"time_sensitive\":false},\"ui_session_id\":\"" + cookie + "\"}"; 62 string param2 = "{\"target_id\":\"" + target_id + "\",\"profile_id\":\"11111111-1111-1111-1111-111111111111\",\"schedule\":{\"disable\":false,\"start_date\":null,\"time_sensitive\":false}}"; 63 if (target_id == null) 64 { 65 return 0; 66 } 67 else 68 { 69 if (GoScan(cookie, param2, a) == null) 70 { 71 return 1; 72 } 73 else 74 { 75 return 2; 76 } 77 78 } 79 80 } 81 public static string GetTarget_id(string cookie, string param, string a) 82 { 83 try 84 { 85 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 86 string strURL = "https://localhost:3443/api/v1/targets"; 87 System.Net.HttpWebRequest request; 88 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); 89 request.Method = "POST"; 90 request.Host = "localhost:3443"; 91 request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"; 92 request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; 93 request.Headers.Add("Cookie", cookie); 94 request.ContentType = "application/json;charset=UTF-8"; 95 request.Referer = "https://localhost:3443/"; 96 request.Headers.Add("X-Auth", a); 97 string paraUrlCoded = param; 98 byte[] payload; 99 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 100 request.ContentLength = payload.Length; 101 Stream writer = request.GetRequestStream(); 102 writer.Write(payload, 0, payload.Length); 103 writer.Close(); 104 System.Net.HttpWebResponse response; 105 response = (System.Net.HttpWebResponse)request.GetResponse(); 106 System.IO.Stream s; 107 s = response.GetResponseStream(); 108 109 string StrDate = ""; 110 111 StreamReader Reader = new StreamReader(s, Encoding.UTF8); 112 StrDate = Reader.ReadToEnd(); 113 114 115 116 JObject newObj1 = (JObject)JsonConvert.DeserializeObject(StrDate); 117 return newObj1["target_id"].ToString(); 118 } 119 catch (Exception ex) 120 { 121 122 return null; 123 } 124 125 } 126 public static string GoScan(string cookie, string param, string a) 127 { 128 129 try 130 { 131 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 132 string strURL = "https://localhost:3443/api/v1/scans"; 133 System.Net.HttpWebRequest request; 134 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); 135 request.Method = "POST"; 136 request.Host = "localhost:3443"; 137 //request.Headers.Add("Connection", "keep-alive"); 138 request.Headers.Add("Origin", "https://localhost:3443"); 139 request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"; 140 request.ContentType = "application/json;charset=UTF-8"; 141 request.Accept = "application/json, text/plain, */*"; 142 request.Headers.Add("X-Auth", a); 143 request.Referer = "https://localhost:3443/"; 144 request.Headers.Add("Accept-Encoding", "gzip, deflate, br"); 145 request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4"); 146 request.Headers.Add("Cookie", cookie); 147 string paraUrlCoded = param; 148 byte[] payload; 149 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 150 request.ContentLength = payload.Length; 151 Stream writer = request.GetRequestStream(); 152 writer.Write(payload, 0, payload.Length); 153 writer.Close(); 154 System.Net.HttpWebResponse response; 155 response = (System.Net.HttpWebResponse)request.GetResponse(); 156 System.IO.Stream s; 157 s = response.GetResponseStream(); 158 string StrDate = ""; 159 160 StreamReader Reader = new StreamReader(s, Encoding.UTF8); 161 StrDate = Reader.ReadLine(); 162 163 164 165 return "OK!"; 166 } 167 catch (Exception ex) 168 { 169 170 return null; 171 } 172 173 } 174 175 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 176 { // 总是接受 177 return true; 178 } 179 180 private void button2_Click(object sender, EventArgs e) 181 { 182 int a = 0;//创建失败 183 int b = 0;//创建成功,添加扫描失败 184 int c = 0;//创建成功,添加扫描成功 185 186 187 188 if (string.IsNullOrEmpty(urls)) 189 { 190 MessageBox.Show("请先上传URL"); 191 return; 192 } 193 if (string.IsNullOrEmpty(this.textBox2.Text)) 194 { 195 MessageBox.Show("请输入cookie"); 196 return; 197 } 198 199 string[] ab = urls.Split(new string[] { "\r\n" }, StringSplitOptions.None); 200 201 foreach (var item in ab) 202 { 203 int end = GoAwvs(item, this.textBox2.Text); 204 205 if (end == 0) 206 { 207 a++; 208 } 209 else if (end == 1) 210 { 211 b++; 212 } 213 else if (end == 2) 214 { 215 c++; 216 } 217 218 } 219 220 MessageBox.Show("创建失败" + a + "条," + "创建成功,添加扫描失败" + b + "条," + "创建成功,添加扫描成功" + c + "条"); 221 222 223 } 224 } 225 }
原文地址:https://www.cnblogs.com/had3s/p/9755063.html
时间: 2024-11-06 21:41:07