Java tutorial
/* * The MIT License * * Copyright 2014 Security Tools SDK for Java. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package securitytools.nessus; import java.io.Closeable; import java.io.IOException; import java.security.NoSuchAlgorithmException; import org.apache.http.HttpHost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.CloseableHttpClient; import securitytools.common.ClientConfiguration; import securitytools.common.http.HttpClientFactory; import securitytools.common.http.ResponseHandlers; import securitytools.nessus.http.request.CreateScanRequest; import securitytools.nessus.http.request.GetFeedRequest; import securitytools.nessus.http.request.LoginRequest; import securitytools.nessus.http.request.LogoutRequest; /** * Client for accessing a Nessus server via the XMLRPC API. * * @author Adam Parsons * @version 0.0.1, 04/29/14 * @since 1.0.0 */ public class NessusClient implements Nessus, Closeable { private final HttpHost target; private final CloseableHttpClient client; private final HttpClientContext context; private boolean authenticated = false; public NessusClient(HttpHost targetHost, ClientConfiguration clientConfiguration) { this.target = targetHost; context = HttpClientContext.create(); try { client = HttpClientFactory.build(clientConfiguration); } catch (NoSuchAlgorithmException nsae) { throw new NessusClientException(nsae.getMessage(), nsae); } } @Override public void close() throws IOException { if (authenticated) { logout(); } client.close(); } @Override public void createScan(String scanName, int policyId, String[] targets) throws NessusClientException, NessusServiceException { try { String r = client.execute(target, new CreateScanRequest(scanName, policyId, targets), ResponseHandlers.getStringHandler(), context); System.out.println(r); } catch (IOException ioe) { throw new NessusClientException("Unable to complete Nessus request.", ioe); } } @Override public void getFeed() throws NessusClientException, NessusServiceException { try { String r = client.execute(target, new GetFeedRequest(), ResponseHandlers.getStringHandler(), context); System.out.println(r); } catch (IOException ioe) { throw new NessusClientException("Unable to complete Nessus request.", ioe); } } public boolean isAuthenticated() { return authenticated; } @Override public void login(NessusCredentials credentials) throws NessusClientException, NessusServiceException { if (authenticated) { throw new NessusClientException("Client has already logged in."); } try { String r = client.execute(target, new LoginRequest(credentials), ResponseHandlers.getStringHandler(), context); System.out.println(r); authenticated = true; } catch (IOException ioe) { throw new NessusClientException("Unable to complete Nessus request.", ioe); } } @Override public void logout() throws NessusClientException, NessusServiceException { if (!authenticated) { throw new NessusClientException("Client has already logged out."); } try { String r = client.execute(target, new LogoutRequest(), ResponseHandlers.getStringHandler(), context); System.out.println(r); authenticated = false; } catch (IOException ioe) { throw new NessusClientException("Unable to complete Nessus request.", ioe); } } }