Tenable API with JSON request object Integration error

149 views Asked by At

I'm having this error when running the following code (it's not full). The error is the following and I couldn't get past it so far:

org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
    at org.json.JSONArray.<init>(JSONArray.java:107)
    at org.json.JSONArray.<init>(JSONArray.java:145)
    at tenablescscancreator.TenableSCScanCreator.getUUID(TenableSCScanCreator.java:141)
    at tenablescscancreator.TenableSCScanCreator.main(TenableSCScanCreator.java:80)
Scan created successfully. Response: {"type":"regular","response":"","error_code":13,"error_msg":"This request is not properly formatted.","warnings":[],"timestamp":1686316237}

The code is:

public class TenableSCScanCreator {

    public static void main(String[] args) throws JSONException {
        String baseUrl = "Internal Nessus Server Link";
        String username = "user";
        String password = "password";

        String scanName = "zTestScan1";
        String policyName = "Full scan including web";
        String scanZone = "Default Scan Zone";
        String importRepository = "Active Vulns";
        String credentialsCategory = "Windows";
        String credentialsName = "NessusScan";
        String[] assetsList = {"asset1", "asset2"};

        /*try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // Set up basic authentication
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(AuthScope.ANY),
                    new UsernamePasswordCredentials(username, password)
            );*/
        try {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("username", "password"));
        CloseableHttpClient httpClient
                = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
        // Create scan request JSON
        
        String requestBody = String.format(
                "{ \"uuid\": \"%s\", \"settings\": [ \"name\": \"%s\", "
                + "\"policy\": \"%s\", \"scan_zone\": \"%s\", \"import_repositories\": \"%s\", "
                + "\"credentials\": [ \"category\": \"%s\", \"name\": \"%s\" ], "
                + "\"assets\": [ \"asset\": %s ], "
                + "\"starttime\": \"now\" ] }",
                getUUID(baseUrl, httpClient),
                scanName,
                policyName,
                scanZone,
                importRepository,
                credentialsCategory,
                credentialsName,
                getAssetsListJSON(assetsList)
        );
        
        // Send scan creation request
        HttpPost httpPost = new HttpPost(baseUrl + "/rest/scan");
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(requestBody));

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();

        // Process response
        if (responseEntity != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
            String line;
            StringBuilder responseBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                responseBuilder.append(line);
            }
            reader.close();

            System.out.println("Scan created successfully. Response: " + responseBuilder.toString());
        } else {
            System.out.println("Failed to create scan.");
        }
    }
    catch (IOException e) {
            e.printStackTrace();
    }
}

I tried replacing the braces with box brackets, but it still didn't work.

I tried replacing the braces in the JSON request with box brackets. Still didn't work.

1

There are 1 answers

1
yaq6zkdcd On

you should try pyTenable Its way more simplier

https://pytenable.readthedocs.io/en/stable/api/sc/scans.html