2021-01-01 15:28:28 +05:30
package file
import (
2021-03-05 12:14:46 +05:30
"strings"
2021-01-01 15:28:28 +05:30
"github.com/pkg/errors"
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
)
// Request contains a File matching mechanism for local disk operations.
type Request struct {
2021-02-26 13:13:11 +05:30
// Operators for the current request go here.
operators . Operators ` yaml:",inline" `
2021-07-27 16:03:56 +05:30
// description: |
// Extensions is the list of extensions to perform matching on.
// examples:
// - value: '[]string{".txt", ".go", ".json"}'
2021-08-23 23:50:45 +05:30
Extensions [ ] string ` yaml:"extensions,omitempty" jsonschema:"title=extensions to match,description=List of extensions to perform matching on" `
2021-07-27 16:03:56 +05:30
// description: |
// ExtensionDenylist is the list of file extensions to deny during matching.
//
// By default, it contains some non-interesting extensions that are hardcoded
// in nuclei.
// examples:
// - value: '[]string{".avi", ".mov", ".mp3"}'
2021-08-23 23:50:45 +05:30
ExtensionDenylist [ ] string ` yaml:"denylist,omitempty" jsonschema:"title=extensions to deny match,description=List of file extensions to deny during matching" `
2021-01-01 15:28:28 +05:30
2021-09-01 15:08:46 +05:30
// ID is the the optional id of the request
2021-08-23 23:50:45 +05:30
ID string ` yaml:"id,omitempty" jsonschema:"title=id of the request,description=ID is the optional ID for the request" `
2021-02-26 13:13:11 +05:30
2021-07-27 16:03:56 +05:30
// description: |
// MaxSize is the maximum size of the file to run request on.
//
// By default, nuclei will process 5MB files and not go more than that.
// It can be set to much lower or higher depending on use.
// examples:
// - value: 2048
2021-08-23 23:50:45 +05:30
MaxSize int ` yaml:"max-size,omitempty" jsonschema:"title=max size data to run request on,description=Maximum size of the file to run request on" `
2021-08-04 14:20:48 +05:30
CompiledOperators * operators . Operators ` yaml:"-" `
2021-01-01 15:28:28 +05:30
// cache any variables that may be needed for operation.
options * protocols . ExecuterOptions
extensions map [ string ] struct { }
extensionDenylist map [ string ] struct { }
2021-02-26 13:13:11 +05:30
2021-07-27 16:03:56 +05:30
// description: |
// NoRecursive specifies whether to not do recursive checks if folders are provided.
2021-08-23 23:50:45 +05:30
NoRecursive bool ` yaml:"no-recursive,omitempty" jsonschema:"title=do not perform recursion,description=Specifies whether to not do recursive checks if folders are provided" `
2021-02-26 13:13:11 +05:30
allExtensions bool
2021-01-01 15:28:28 +05:30
}
// defaultDenylist is the default list of extensions to be denied
2021-05-25 06:41:13 +05:30
var defaultDenylist = [ ] string { ".3g2" , ".3gp" , ".7z" , ".apk" , ".arj" , ".avi" , ".axd" , ".bmp" , ".css" , ".csv" , ".deb" , ".dll" , ".doc" , ".drv" , ".eot" , ".exe" , ".flv" , ".gif" , ".gifv" , ".gz" , ".h264" , ".ico" , ".iso" , ".jar" , ".jpeg" , ".jpg" , ".lock" , ".m4a" , ".m4v" , ".map" , ".mkv" , ".mov" , ".mp3" , ".mp4" , ".mpeg" , ".mpg" , ".msi" , ".ogg" , ".ogm" , ".ogv" , ".otf" , ".pdf" , ".pkg" , ".png" , ".ppt" , ".psd" , ".rar" , ".rm" , ".rpm" , ".svg" , ".swf" , ".sys" , ".tar.gz" , ".tar" , ".tif" , ".tiff" , ".ttf" , ".vob" , ".wav" , ".webm" , ".wmv" , ".woff" , ".woff2" , ".xcf" , ".xls" , ".xlsx" , ".zip" }
2021-01-01 15:28:28 +05:30
2021-01-16 14:10:24 +05:30
// GetID returns the unique ID of the request if any.
func ( r * Request ) GetID ( ) string {
return r . ID
}
2021-01-01 15:28:28 +05:30
// Compile compiles the protocol request for further execution.
func ( r * Request ) Compile ( options * protocols . ExecuterOptions ) error {
if len ( r . Matchers ) > 0 || len ( r . Extractors ) > 0 {
compiled := & r . Operators
if err := compiled . Compile ( ) ; err != nil {
return errors . Wrap ( err , "could not compile operators" )
}
r . CompiledOperators = compiled
}
// By default use 5mb as max size to read.
if r . MaxSize == 0 {
r . MaxSize = 5 * 1024 * 1024
}
r . options = options
r . extensions = make ( map [ string ] struct { } )
r . extensionDenylist = make ( map [ string ] struct { } )
for _ , extension := range r . Extensions {
2021-03-05 12:14:46 +05:30
if extension == "all" {
2021-01-01 15:31:44 +05:30
r . allExtensions = true
} else {
2021-03-08 19:20:40 +05:30
if ! strings . HasPrefix ( extension , "." ) {
2021-03-05 12:14:46 +05:30
extension = "." + extension
}
2021-01-01 15:31:44 +05:30
r . extensions [ extension ] = struct { } { }
}
2021-01-01 15:28:28 +05:30
}
for _ , extension := range defaultDenylist {
2021-03-08 19:20:40 +05:30
if ! strings . HasPrefix ( extension , "." ) {
2021-03-05 12:14:46 +05:30
extension = "." + extension
}
2021-01-01 15:28:28 +05:30
r . extensionDenylist [ extension ] = struct { } { }
}
for _ , extension := range r . ExtensionDenylist {
2021-03-08 19:20:40 +05:30
if ! strings . HasPrefix ( extension , "." ) {
2021-03-05 12:14:46 +05:30
extension = "." + extension
}
2021-01-01 15:28:28 +05:30
r . extensionDenylist [ extension ] = struct { } { }
}
return nil
}
// Requests returns the total number of requests the YAML rule will perform
func ( r * Request ) Requests ( ) int {
return 1
}