-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) add immediate screenshot mode and skip screenshot flags
- Loading branch information
Showing
12 changed files
with
345 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package writers | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/sensepost/gowitness/pkg/models" | ||
) | ||
|
||
// MemoryWriter is a memory-based results queue with a maximum slot count | ||
type MemoryWriter struct { | ||
slots int | ||
results []*models.Result | ||
mutex sync.Mutex | ||
} | ||
|
||
// NewMemoryWriter initializes a MemoryWriter with the specified number of slots | ||
func NewMemoryWriter(slots int) (*MemoryWriter, error) { | ||
if slots <= 0 { | ||
return nil, errors.New("slots need to be a positive integer") | ||
} | ||
|
||
return &MemoryWriter{ | ||
slots: slots, | ||
results: make([]*models.Result, 0, slots), | ||
mutex: sync.Mutex{}, | ||
}, nil | ||
} | ||
|
||
// Write adds a new result to the MemoryWriter. | ||
func (s *MemoryWriter) Write(result *models.Result) error { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
|
||
if len(s.results) >= s.slots { | ||
s.results = s.results[1:] | ||
} | ||
|
||
s.results = append(s.results, result) | ||
|
||
return nil | ||
} | ||
|
||
// GetLatest retrieves the most recently added result. | ||
func (s *MemoryWriter) GetLatest() *models.Result { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
|
||
if len(s.results) == 0 { | ||
return nil | ||
} | ||
|
||
return s.results[len(s.results)-1] | ||
} | ||
|
||
// GetFirst retrieves the oldest result in the MemoryWriter. | ||
func (s *MemoryWriter) GetFirst() *models.Result { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
|
||
if len(s.results) == 0 { | ||
return nil | ||
} | ||
|
||
return s.results[0] | ||
} | ||
|
||
// GetAllResults returns a copy of all current results. | ||
func (s *MemoryWriter) GetAllResults() []*models.Result { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
|
||
// Create a copy to prevent external modification | ||
resultsCopy := make([]*models.Result, len(s.results)) | ||
copy(resultsCopy, s.results) | ||
|
||
return resultsCopy | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/sensepost/gowitness/pkg/log" | ||
"github.com/sensepost/gowitness/pkg/runner" | ||
driver "github.com/sensepost/gowitness/pkg/runner/drivers" | ||
"github.com/sensepost/gowitness/pkg/writers" | ||
) | ||
|
||
type submitSingleRequest struct { | ||
URL string `json:"url"` | ||
Options *submitRequestOptions `json:"options"` | ||
} | ||
|
||
// SubmitSingleHandler submits a URL to scan, returning the result. | ||
// | ||
// @Summary Submit a single URL for probing | ||
// @Description Starts a new probing routine for a URL and options, returning the results when done. | ||
// @Tags Results | ||
// @Accept json | ||
// @Produce json | ||
// @Param query body submitSingleRequest true "The URL scanning request object" | ||
// @Success 200 {string} string "Probing started" | ||
// @Router /submit/single [post] | ||
func (h *ApiHandler) SubmitSingleHandler(w http.ResponseWriter, r *http.Request) { | ||
var request submitSingleRequest | ||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | ||
log.Error("failed to read json request", "err", err) | ||
http.Error(w, "Error reading JSON request", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if request.URL == "" { | ||
http.Error(w, "No URL provided", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
options := runner.NewDefaultOptions() | ||
options.Scan.ScreenshotToWriter = true | ||
options.Scan.ScreenshotSkipSave = true | ||
|
||
// Override default values with request options | ||
if request.Options != nil { | ||
if request.Options.X != 0 { | ||
options.Chrome.WindowX = request.Options.X | ||
} | ||
if request.Options.Y != 0 { | ||
options.Chrome.WindowY = request.Options.Y | ||
} | ||
if request.Options.UserAgent != "" { | ||
options.Chrome.UserAgent = request.Options.UserAgent | ||
} | ||
if request.Options.Timeout != 0 { | ||
options.Scan.Timeout = request.Options.Timeout | ||
} | ||
if request.Options.Format != "" { | ||
options.Scan.ScreenshotFormat = request.Options.Format | ||
} | ||
} | ||
|
||
writer, err := writers.NewMemoryWriter(1) | ||
if err != nil { | ||
http.Error(w, "Error getting a memory writer", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
logger := slog.New(log.Logger) | ||
|
||
driver, err := driver.NewChromedp(logger, *options) | ||
if err != nil { | ||
http.Error(w, "Error sarting driver", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
runner, err := runner.NewRunner(logger, driver, *options, []writers.Writer{writer}) | ||
if err != nil { | ||
log.Error("error starting runner", "err", err) | ||
http.Error(w, "Error starting runner", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
go func() { | ||
runner.Targets <- request.URL | ||
close(runner.Targets) | ||
}() | ||
|
||
runner.Run() | ||
runner.Close() | ||
|
||
jsonData, err := json.Marshal(writer.GetLatest()) | ||
if err != nil { | ||
http.Error(w, "Error creating JSON response", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Write(jsonData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.