public bool Set_opt(string opt, string val) { if (this.args.ContainsKey(opt)) { var no = this.args[opt]; this.args.Remove(opt); this.args.Add(opt, new NmapOption(val, no.odesc)); return(true); } if (this.nmap_args.ContainsKey(opt)) { var no = this.nmap_args[opt]; this.nmap_args.Remove(opt); this.nmap_args.Add(opt, new NmapOption(val, no.odesc)); return(true); } var nw = new NseWriter(); nw.Warn("No such option: " + opt); return(false); }
public string Run(Dictionary <string, NmapOption> global_flags, List <string> hosts) { string flags = String.Join(" ", this.flags); string script = ""; string script_args = ""; string extra_args = ""; if (this.script.Length > 0) { script = "--script " + this.name; script_args = "--script-args \""; foreach (KeyValuePair <string, NmapOption> kv in this.args) { if (kv.Key == "RHOST" || kv.Key == "RPORT") { continue; } if (kv.Value.oval.Trim().Length == 0) { continue; } script_args = $"{script_args},{kv.Key}={kv.Value.oval}".TrimStart(','); } script_args = script_args + "\""; if (script_args.Length < 20) { script_args = ""; } } else { // e.g. built in nmap commands foreach (KeyValuePair <string, NmapOption> kv in this.args) { if (kv.Key == "RHOST" || kv.Key == "RPORT") { continue; } if (kv.Value.oval.Trim().Length == 0) { continue; } extra_args = $"{extra_args} {kv.Key} {kv.Value.oval}"; } } string g_flags = ""; foreach (KeyValuePair <string, NmapOption> kv in global_flags) { if (kv.Value.oval.Length > 0) { g_flags = g_flags + " " + kv.Key + " " + kv.Value.oval; } } string n_args = ""; foreach (KeyValuePair <string, NmapOption> kv in this.nmap_args) { if (kv.Value.oval.Length > 0) { n_args = n_args + " " + kv.Key + " " + kv.Value.oval; } } string ports; try { ports = this.args["RPORT"].oval; } catch { ports = ""; } if (ports.Length > 0) { ports = "-p " + ports; } string hostspec = this.args["RHOST"].oval; if (hostspec == "default" || hostspec.Trim().Length == 0) { hostspec = String.Join(' ', hosts); } string cmd = $" {ports} {extra_args} {flags} {n_args} {g_flags} {script} {script_args} {hostspec}"; Console.WriteLine(this.nmap_path + " " + cmd); Process process = new Process { StartInfo = new ProcessStartInfo { FileName = this.nmap_path, Arguments = cmd, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; process.Start(); string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); process.WaitForExit(); process.Close(); if (error.Length > 0) { var nw = new NseWriter(); nw.Error($"Error seen: {error}"); } return(output); }