Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max file size support #20

Merged
merged 7 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ INPUT:
OUTPUT:
-es, -estimate estimate permutation count without generating payloads
-o, -output string output file to write altered subdomain list
-v, -verbose display verbose output
-ms, -max-size int Max export data size in bytes (default 9223372036854775807) -v, -verbose display verbose output
-silent display results only
-version display alterx version

Expand Down
1 change: 1 addition & 0 deletions cmd/alterx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {
Payloads: cliOpts.Payloads,
Limit: cliOpts.Limit,
Enrich: cliOpts.Enrich, // enrich payloads
MaxSize: cliOpts.MaxSize,
}

if cliOpts.PermutationConfig != "" {
Expand Down
7 changes: 7 additions & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runner
import (
"fmt"
"io"
"math"
"os"
"strings"

Expand All @@ -26,6 +27,7 @@ type Options struct {
Silent bool
Enrich bool
Limit int
MaxSize int
// internal/unexported fields
wordlists goflags.RuntimeMap
}
Expand All @@ -44,6 +46,7 @@ func ParseFlags() *Options {
flagSet.CreateGroup("output", "Output",
flagSet.BoolVarP(&opts.Estimate, "estimate", "es", false, "estimate permutation count without generating payloads"),
flagSet.StringVarP(&opts.Output, "output", "o", "", "output file to write altered subdomain list"),
flagSet.IntVarP(&opts.MaxSize, "max-size", "ms", math.MaxInt, "Max export data size in bytes"),
flagSet.BoolVarP(&opts.Verbose, "verbose", "v", false, "display verbose output"),
flagSet.BoolVar(&opts.Silent, "silent", false, "display results only"),
flagSet.CallbackVar(printVersion, "version", "display alterx version"),
Expand Down Expand Up @@ -71,6 +74,10 @@ func ParseFlags() *Options {
}
}

if opts.MaxSize < 0 {
gologger.Fatal().Msgf("max-size cannot be negative")
}

if opts.Silent {
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
} else if opts.Verbose {
Expand Down
19 changes: 17 additions & 2 deletions mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Options struct {
// Enrich when true alterx extra possible words from input
// and adds them to default payloads word,number
Enrich bool
// MaxSize limits output data size
MaxSize int
}

// Mutator
Expand Down Expand Up @@ -138,6 +140,7 @@ func (m *Mutator) ExecuteWithWriter(Writer io.Writer) error {
}
resChan := m.Execute(context.TODO())
m.payloadCount = 0
maxFileSize := m.Options.MaxSize
for {
value, ok := <-resChan
if !ok {
Expand All @@ -148,11 +151,23 @@ func (m *Mutator) ExecuteWithWriter(Writer io.Writer) error {
// we can't early exit, due to abstraction we have to conclude the elaboration to drain all dedupers
continue
}
_, err := Writer.Write([]byte(value + "\n"))
m.payloadCount++
if maxFileSize <= 0 {
// drain all dedupers when max-file size reached
continue
}
outputData := []byte(value + "\n")
if len(outputData) > maxFileSize {
maxFileSize = 0
continue
}

n, err := Writer.Write(outputData)
if err != nil {
return err
}
// update maxFileSize limit after each write
maxFileSize -= n
m.payloadCount++
}
}

Expand Down