protected override void ProcessRecord() { var profile = new NessusProfile { Server = Server, UserName = Credential.UserName, Port = Port, Password = Credential.Password }; if (InMemoryOnly) { WriteObject(profile); return; } var protectedProfile = profile.ToProtectedString(); NessusProfile.FromProtectedString(protectedProfile); if (!string.IsNullOrWhiteSpace(OutFile)) { OutFile = Environment.ExpandEnvironmentVariables(OutFile); Directory.CreateDirectory(Path.GetDirectoryName(OutFile)); File.WriteAllText(OutFile, protectedProfile); WriteVerbose($"Profile successfully saved to {Path.GetFullPath(OutFile)}"); } else { WriteObject(protectedProfile); } }
protected override void BeginProcessing() { _tokenSource = new CancellationTokenSource(); if (Profile == null) { if (string.IsNullOrWhiteSpace(ProfileFile)) { var defaultProfile = FindProfileInDefaultLocations(); if (defaultProfile == null) { throw new FileNotFoundException( $@"Profile file cannot be found. Please use -{ nameof(ProfileFile) } parameter or create it using New-NessusProfile -OutFile <path to file> in one of default locations:{ Environment.NewLine }{string.Join(Environment.NewLine, DefaultProfileLocations)}"); } ProfileFile = defaultProfile.Path; Profile = defaultProfile.Profile; } else { ProfileFile = Environment.ExpandEnvironmentVariables(ProfileFile); Profile = NessusProfile.FromProtectedString(File.ReadAllText(ProfileFile)); } WriteVerbose($"Using {Path.GetFullPath(ProfileFile)} profile."); } _nessusConnection = new NessusConnection(Profile.Server, Profile.Port, Profile.UserName, Profile.Password); try { WriteVerbose($"Connecting to Nessus server at {Profile.Server}:{Profile.Port}"); _nessusConnection.OpenAsync(_tokenSource.Token).Wait(_tokenSource.Token); InitialLoad(_nessusConnection, _tokenSource.Token); } catch (AggregateException e) { var err = e.Flatten().InnerException; WriteError(new ErrorRecord(err, string.Empty, ErrorCategory.ConnectionError, _nessusConnection)); } catch (Exception e) { WriteError(new ErrorRecord(e, string.Empty, ErrorCategory.ConnectionError, _nessusConnection)); } }
private static DefaultProfileInfo FindProfileInDefaultLocations() { var paths = DefaultProfileLocations; var profile = paths.Select(x => { try { return(new DefaultProfileInfo { Path = x, Profile = NessusProfile.FromProtectedString(File.ReadAllText(x)) }); } catch { return(new DefaultProfileInfo()); } }).FirstOrDefault(x => x.Profile != null); return(profile); }
protected override void ProcessRecord() { try { var profile = Profile; if (profile == null) { ProfileFile = Environment.ExpandEnvironmentVariables(ProfileFile); WriteVerbose($@"Using profile from file ""{ProfileFile}"""); profile = NessusProfile.FromProtectedString(File.ReadAllText(ProfileFile)); } if (TryLoginToServer) { using (var c = new NessusConnection(profile.Server, profile.Port, profile.UserName, profile.Password)) { try { c.OpenAsync(CancellationToken.None).Wait(); WriteObject(CreateValidationResult(profile, @"Login Successful")); } catch (AggregateException e) { WriteObject(CreateValidationResult(profile, e.Flatten().InnerException?.Message)); } } } else { WriteObject(CreateValidationResult(profile, @"OK")); } } catch (Exception e) { WriteObject(CreateValidationResult(null, e.Message)); } }