Skip to content

Commit

Permalink
updated logs, outputs, and write to file
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanwalker committed Jan 14, 2023
1 parent bf6b6b6 commit cb8cb4e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ It leverages [AWS Lambda](https://aws.amazon.com/lambda/) as a backend to invoke
- Specify any Nuclei arguments just like you would locally
- Specify a single host or from a file
- Run the http server to take scans from the API
- Run the http server to the status of the scans
- Run the http server to get status of the scans
- Query findings through Athena for searching

## Usage
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var runCmd = &cobra.Command{
}

var startServer = &cobra.Command{
Use: "serve",
Use: "service",
Short: "Launch API to launch run tasks to the nuclei runner.",
Long: "Executes nuclei through an API through asynchronous lambda functions",
Run: func(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func ExecuteScans(batches [][]string, output string, lambdaName string, nucleiAr

// Print the results if not silent mode
if !silent {
log.Println("Completed all parallel operations in", time.Since(start), ", best of luck!")
log.Println("Completed all parallel operations, best of luck! Completed in", time.Since(start))
}
}
36 changes: 4 additions & 32 deletions pkg/lambda/lambda.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package lambda

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/DevSecOpsDocs/nuclearpond/pkg/outputs"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
Expand Down Expand Up @@ -48,37 +46,11 @@ func InvokeLambdas(payload LambdaInvoke, lambda string, output string) {

// Change outputs depending on the output type
if output == "s3" {
// if lambdaResponse contains the string "No findings" then return
if strings.Contains(lambdaResponse.(string), "No findings") {
log.Println("Scan completed with no findings")
return
}
log.Println("Saved results in", lambdaResponse)
outputs.S3Output(lambdaResponse)
} else if output == "cmd" {
// convert lambdaResponse to string
lambdaResponseString := lambdaResponse.(string)
// convert response from base64 to colorized terminal output
decodedBytes, _ := base64.StdEncoding.DecodeString(lambdaResponseString)
// if decodedBytes is empty then return
if len(decodedBytes) == 0 {
log.Println("Scan completed with no output")
return
}
log.Println("Scan complete with output:")
fmt.Println(string(decodedBytes))
outputs.CmdOutput(lambdaResponse)
} else if output == "json" {
// if lambdaResponse contains the string "No findings" then return
if strings.Contains(lambdaResponse.(string), "No findings") {
return
}
// pretty print json in lambdaResponse indented by 4 spaces
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, []byte(lambdaResponse.(string)), "", " ")
if error != nil {
log.Println("JSON parse error: ", error)
return
}
fmt.Println(string(prettyJSON.Bytes()))
outputs.JsonOutput(lambdaResponse)
}
}

Expand Down
76 changes: 76 additions & 0 deletions pkg/outputs/outputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package outputs

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"strings"
)

// function for s3 output
func S3Output(lambdaResponse interface{}) {
if strings.Contains(lambdaResponse.(string), "No findings") {
log.Println("Scan completed with no findings")
return
}
log.Println("Saved results in", lambdaResponse)
}

// function for cmd output
func CmdOutput(lambdaResponse interface{}) {
// convert lambdaResponse to string
lambdaResponseString := lambdaResponse.(string)
// convert response from base64 to colorized terminal output
decodedBytes, _ := base64.StdEncoding.DecodeString(lambdaResponseString)
// if decodedBytes is empty then return
if len(decodedBytes) == 0 {
log.Println("Scan completed with no output")
return
}
log.Println("Scan complete with output:")
fmt.Println(string(decodedBytes))
}

// function for json output
func JsonOutput(lambdaResponse interface{}) {
// if lambdaResponse contains the string "No findings" then return
if strings.Contains(lambdaResponse.(string), "No findings") {
log.Println("Scan completed with no findings")
return
}
// pretty print json in lambdaResponse indented by 4 spaces
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, []byte(lambdaResponse.(string)), "", " ")
if error != nil {
log.Println("JSON parse error: ", error)
return
}
// if results.json exists, append to it
if _, err := os.Stat("results.json"); err == nil {
f, err := os.OpenFile("results.json", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.Write(prettyJSON.Bytes())
if err != nil {
log.Fatal(err)
}
log.Println("Appended results in results.json")
return
}
// write prettyJSON to file
f, err := os.Create("results.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.Write(prettyJSON.Bytes())
if err != nil {
log.Fatal(err)
}
log.Println("Saved results in results.json")
}
3 changes: 0 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ func scanStatusHandler(w http.ResponseWriter, r *http.Request) {
return
}
scanId := parts[2]

log.Println("Getting scan state for", scanId)

log.Println("Getting scan state for", scanId)

state, err := getScanState(scanId)
Expand Down

0 comments on commit cb8cb4e

Please sign in to comment.