How to use FileMatches method of upload Package

Best Selenoid code snippet using upload.FileMatches

main.go

Source:main.go Github

copy

Full Screen

1package main2import (3 cm "./common"4 ft "./timestamps"5 "bufio"6 "bytes"7 "compress/gzip"8 "crypto/md5"9 "encoding/base64"10 "encoding/hex"11 "encoding/json"12 "flag"13 "fmt"14 "io"15 "io/ioutil"16 "log"17 "os"18 "os/user"19 "path/filepath"20 "regexp"21 "strings"22 "sync"23 "time"24)25var matched = 026var cleared = 027var filesToScan = make(chan string, 1000)28/*29### TODO30Test Cases31CPU/Mem Limits32### Server Detection (Scan Profiles)33IIS34- Web.Config parsing (ISAPI Filters/Handlers)35-- Detect all web roots to scan automatically36Apache37- Detect web roots to scan38Tomcat39- Catalina Logs40- War File Deployment Logs41*/42func processMatches(j string, r regexp.Regexp) (fileMatches map[string]int, size int64) {43 fileHandle, err := os.Open(j)44 if err != nil {45 log.Fatal(err)46 }47 defer fileHandle.Close()48 fi, err := os.Stat(j)49 if err != nil {50 log.Println(err)51 }52 fileScanner := bufio.NewScanner(fileHandle)53 fileMatches = make(map[string]int)54 for fileScanner.Scan() {55 matches := r.FindStringSubmatch(strings.ToLower(fileScanner.Text()))56 if len(matches) > 0 {57 for _, it := range matches {58 fileMatches[it] += 159 }60 }61 }62 return fileMatches, fi.Size()63}64func md5HashFile(filePath string) (string, error) {65 var returnMD5String string66 file, err := os.Open(filePath)67 if err != nil {68 return returnMD5String, err69 }70 defer file.Close()71 hash := md5.New()72 if _, err := io.Copy(hash, file); err != nil {73 return returnMD5String, err74 }75 hashInBytes := hash.Sum(nil)[:16]76 returnMD5String = hex.EncodeToString(hashInBytes)77 return returnMD5String, nil78}79func compressEncode(filePath string, fileSize int64) string {80 fileItem, err := os.Open(filePath)81 if err != nil {82 return ""83 }84 defer fileItem.Close()85 buf := make([]byte, fileSize)86 fReader := bufio.NewReader(fileItem)87 fReader.Read(buf)88 var b bytes.Buffer89 gz := gzip.NewWriter(&b)90 if _, err := gz.Write(buf); err != nil {91 fmt.Println(err)92 return ""93 }94 if err := gz.Flush(); err != nil {95 fmt.Println(err)96 return ""97 }98 if err := gz.Close(); err != nil {99 fmt.Println(err)100 return ""101 }102 readBuf, _ := ioutil.ReadAll(&b)103 imgBase64Str := base64.StdEncoding.EncodeToString(readBuf)104 return imgBase64Str105}106func Scan_worker(r regexp.Regexp, wg *sync.WaitGroup, rawContents bool) {107 for j := range filesToScan {108 //fmt.Println("Worker:", id, "File:", j)109 //fmt.Println(len(filesToScan))110 Jdata := cm.FileObj{}111 Jdata.FilePath = j112 fileMatches, size := processMatches(j, r)113 Jdata.Size = size114 Jdata.Matches = fileMatches115 if len(fileMatches) != 0 {116 matched = matched + 1117 } else if len(fileMatches) == 0 {118 cleared = cleared + 1119 continue120 }121 fHash, err := md5HashFile(j)122 if err != nil {123 log.Println(err)124 }125 Jdata.MD5 = fHash126 if rawContents {127 Jdata.RawContents = compressEncode(j, Jdata.Size)128 }129 // File Timestamps130 timestamps, err := ft.StatTimes(j)131 Jdata.Timestamps = timestamps132 // PROD133 data, err := json.Marshal(Jdata)134 if err != nil {135 log.Fatal(err)136 }137 fmt.Printf("%s\n", data)138 // DEVELOP139 //data, err := json.MarshalIndent(Jdata, "", " ")140 //fmt.Printf("%s\n", data)141 }142 wg.Done()143}144func main() {145 start := time.Now()146 var dir = flag.String("dir", "", "Directory to scan for webshells")147 var customRegex = flag.String("regex", "", "Override default regex with your own")148 var size = flag.Int64("size", 10, "Specify max file size to scan (default is 10 MB)")149 var exts = flag.String("exts", "", "Specify extensions to target. Multiple extensions should be passed with pipe separator (asp|aspx|php|cfm). Default is all extensions")150 var rawContents = flag.Bool("raw_contents", false, "If a match is found, grab the raw contents and base64 + gzip compress the file into the JSON object.")151 flag.Parse()152 if *dir == "" {153 flag.PrintDefaults()154 os.Exit(1)155 }156 regexString := ""157 if *customRegex == "" {158 regexString = `Filesman|eval\(|Load\(Request\.BinaryRead\(int\.Parse\(Request\.Cookies|Html \= Replace\(Html\, \"\%26raquo\;\"\, \"?\"\)|pinkok|internal class reDuh|c0derz shell|md5 cracker|umer rock|Function CP\(S\,D\)\{sf\=CreateObject\(\"java\"\,\"java\.io\.File|Arguments\=xcmd\.text|asp cmd shell|Maceo|TEXTAREA id\=TEXTAREA1 name\=SqlQuery|CMD Bilgileri|sbusqlmod|php assert\(\$\_POST\[|oWshShellNet\.UserName|PHP C0nsole|rhtools|WinX Shell|system\(\$\_GET\[\'cmd\'|Successfully uploadet|\'Are you sure delete|sbusqlcmd|CFSWITCH EXPRESSION\=\#Form\.chopper|php\\HFile|\"ws\"\+\"cr\"\+\"ipt\.s\"\+\"hell\"|eval\(request\(|string rootkey|uZE Shell|Copyed success\!|InStr\(\"\$rar\$mdb\$zip\$exe\$com\$ico\$\"|Folder dosen\'t exists|Buradan Dosya Upload|echo passthru\(\$\_GET\[\'cmd\'|javascript:Bin\_PostBack|The file you want Downloadable|arguments\=\"/c \#cmd\#\"|cmdshell|AvFBP8k9CDlSP79lDl|AK-74 Security Team Web Shell|cfexecute name \= \"\#Form\.cmd\#\"|execute any shell commandn|Gamma Web Shell|System\.Reflection\.Assembly\.Load\(Request\.BinaryRead\(int\.Parse\(Request\.Cookies|fcreateshell|bash to execute a stack overflow|Safe Mode Shell|ASPX Shell|dingen\.php|azrailphp|\$\_POST\[\'sa\']\(\$\_POST\[\'sb\']\)|AspSpy|ntdaddy|\.HitU\. team|National Cracker Crew|eval\(base64\_decode\(\$\_REQUEST\[\'comment\'|Rootshell|geshi\\tsql\.php|tuifei\.asp|GRP WebShell|No Permission :\(|powered by zehir|will be delete all|WebFileManager Browsing|Dive Shell|diez\=server\.urlencode|@eval\(\$\_POST\[\'|ifupload\=\"ItsOk\"|eval\(request\.item|\(eval request\(|wsshn\.username|connect to reDuh|eval\(gzinflate\(base64\_decode|Ru24PostWebShell|ASPXTOOL\"|aspshell|File upload successfully you can download here|eval request\(|if\(is\_uploaded\_file\(\$HTTP|Sub RunSQLCMD|STNC WebShell|doosib|WinExec\(Target\_copy\_of\_cmd|php passthru\(getenv|win\.com cmd\.exe /c cacls\.exe|TUM HAKLARI SAKLIDIR|Created by PowerDream|Then Request\.Files\(0\)\.SaveAs\(Server\.MapPath\(Request|cfmshell|\{ Request\.Files\[0]\.SaveAs\(Server\.MapPath\(Request|\%execute\(request\(\"|php eval\(\$\_POST\[|lama\'s\'hell|RHTOOLS|data\=request\(\"dama\"|digitalapocalypse|hackingway\.tk|\.htaccess stealth web shell|strDat\.IndexOf\(\"EXEC \"|ExecuteGlobal request\(|Deleted file have finished|bin\_filern|CurrentVersionRunBackdoor|Chr\(124\)\.O\.Chr\(124\)|does not have permission to execute CMD\.EXE|G-Security Webshell|system\( \"\./findsock|configwizard|textarea style\=\"width:600\;height:200\" name\=\"cmd\"|ASPShell|repair/sam|BypasS Command eXecute|\%execute\(request\(|arguments\=\"/c \#hotmail|Coded by Loader|Call oS\.Run\(\"win\.com cmd\.exe|DESERTSUN SERVER CRASHER|ASPXSpy|cfparam name\=\"form\.shellpath\"|IIS Spy Using ADSI|p4ssw0rD|WARNING: Failed to daemonise|C0mmand line|phpinfo\(\) function has non-permissible|letaksekarang|Execute Shell Command|DXGLOBALSHIT|IISSpy|execute request\(|Chmod Ok\!|Upload Gagal|awen asp\.net|execute\(request\(\"|oSNet\.ComputerName"`159 } else {160 regexString = *customRegex161 }162 r := regexp.MustCompile(regexString)163 totalFilesScanned := 0164 var wg sync.WaitGroup165 for w := 1; w <= 10; w++ {166 wg.Add(1)167 go Scan_worker(*r, &wg, *rawContents)168 }169 _ = filepath.Walk(*dir, func(path string, f os.FileInfo, err error) error {170 if err != nil {171 return err172 }173 if !f.IsDir() {174 if f.Size() < (*size * 1024 * 1024) {175 //fmt.Println(f.Size(), *size * 1024 * 1024)176 //fmt.Println(path, f.Size())177 /// Scan all files with all extensions178 if *exts == "" {179 filesToScan <- path180 totalFilesScanned = totalFilesScanned + 1181 /// Scan files with specific extensions182 } else {183 items := strings.SplitAfter(*exts, "|")184 for _, e := range items {185 if strings.HasSuffix(path, e) {186 filesToScan <- path187 totalFilesScanned = totalFilesScanned + 1188 }189 }190 }191 }192 }193 return nil194 })195 close(filesToScan)196 wg.Wait()197 metrics := cm.Metrics{}198 metrics.Scanned = totalFilesScanned199 metrics.Clear = cleared200 metrics.Matched = matched201 metrics.ScannedDir = *dir202 metrics.ScanTime = time.Since(start).Minutes()203 // Items empty if error204 osName, _ := os.Hostname()205 envVars := os.Environ()206 theUser, _ := user.Current()207 metrics.SystemInfo.Hostname = osName208 metrics.SystemInfo.EnvVars = envVars209 metrics.SystemInfo.Username = theUser.Username210 metrics.SystemInfo.UserID = theUser.Uid211 metrics.SystemInfo.RealName = theUser.Name212 metrics.SystemInfo.UserHomeDir = theUser.HomeDir213 data, err := json.Marshal(metrics)214 if err != nil {215 log.Fatal(err)216 }217 fmt.Printf("%s\n", data)218}...

Full Screen

Full Screen

s3.go

Source:s3.go Github

copy

Full Screen

...66}67func (s3 *S3Uploader) Upload(createdFile event.CreatedFile) (bool, error) {68 if s3.manager != nil {69 filename := createdFile.Name70 fileMatches, err := FileMatches(s3.IncludeFiles, s3.ExcludeFiles, filename)71 if err != nil {72 return false, fmt.Errorf("invalid pattern: %v", err)73 }74 if !fileMatches {75 log.Printf("[%d] [SKIPPING_FILE] [%s] [Does not match specified patterns]", createdFile.RequestId, createdFile.Name)76 return false, nil77 }78 key := GetS3Key(s3.KeyPattern, createdFile)79 file, err := os.Open(filename)80 defer file.Close()81 if err != nil {82 return false, fmt.Errorf("failed to open file %s: %v", filename, err)83 }84 uploadInput := &s3manager.UploadInput{85 Bucket: aws.String(s3.BucketName),86 Key: aws.String(key),87 Body: file,88 }89 contentType := mime.TypeByExtension(filepath.Ext(filename))90 if contentType != "" {91 uploadInput.ContentType = aws.String(contentType)92 }93 if s3.ReducedRedundancy {94 uploadInput.StorageClass = aws.String("REDUCED_REDUNDANCY")95 }96 _, err = s3.manager.Upload(uploadInput)97 if err != nil {98 return false, fmt.Errorf("failed to S3 upload %s as %s: %v", filename, key, err)99 }100 if !s3.KeepFiles {101 err := os.Remove(filename)102 if err != nil {103 return true, fmt.Errorf("failed to remove uploaded file %s: %v", filename, err)104 }105 }106 return true, nil107 }108 return false, errors.New("S3 uploader is not initialized")109}110func FileMatches(includedFiles string, excludedFiles string, filename string) (bool, error) {111 fileIncluded := true112 if includedFiles != "" {113 fi, err := filepath.Match(includedFiles, filepath.Base(filename))114 if err != nil {115 return false, fmt.Errorf("failed to match included file: %v", err)116 }117 fileIncluded = fi118 }119 fileExcluded := false120 if excludedFiles != "" {121 fe, err := filepath.Match(excludedFiles, filepath.Base(filename))122 if err != nil {123 return false, fmt.Errorf("failed to match excluded file: %v", err)124 }...

Full Screen

Full Screen

s3_test.go

Source:s3_test.go Github

copy

Full Screen

...93 input.Session.Caps.S3KeyPattern = "$fileName"94 key = upload.GetS3Key(testPattern, input)95 AssertThat(t, key, EqualTo{"Some-File.txt"})96}97func TestFileMatches(t *testing.T) {98 matches, err := upload.FileMatches("", "", "any-file-name")99 AssertThat(t, err, Is{nil})100 AssertThat(t, matches, Is{true})101 matches, err = upload.FileMatches("[", "", "/path/to/file.mp4")102 AssertThat(t, err, Not{nil})103 AssertThat(t, matches, Is{false})104 matches, err = upload.FileMatches("", "[", "/path/to/file.mp4")105 AssertThat(t, err, Not{nil})106 AssertThat(t, matches, Is{false})107 matches, err = upload.FileMatches("*.mp4", "", "/path/to/file.mp4")108 AssertThat(t, err, Is{nil})109 AssertThat(t, matches, Is{true})110 matches, err = upload.FileMatches("*.mp4", "", "/path/to/file.log")111 AssertThat(t, err, Is{nil})112 AssertThat(t, matches, Is{false})113 matches, err = upload.FileMatches("*.mp4", "", "/path/to/file.log")114 AssertThat(t, err, Is{nil})115 AssertThat(t, matches, Is{false})116 matches, err = upload.FileMatches("", "*.log", "/path/to/file.log")117 AssertThat(t, err, Is{nil})118 AssertThat(t, matches, Is{false})119}...

Full Screen

Full Screen

FileMatches

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 store := filestore.FileStore{4 }5 handler, err := tusd.NewHandler(tusd.Config{6 })7 if err != nil {8 log.Fatal(err)9 }10 if err != nil {11 log.Fatal(err)12 }13 file, err := os.Open("1.go")14 if err != nil {15 log.Fatal(err)16 }17 defer file.Close()18 upload, err = tusd.NewUpload(file, nil)19 if err != nil {20 log.Fatal(err)21 }22 info, err := file.Stat()23 if err != nil {24 log.Fatal(err)25 }26 size := info.Size()27 offset := int64(0)28 buf := make([]byte, 1024)29 for offset < size {30 n, err := file.Read(buf)31 if err != nil {32 log.Fatal(err)33 }34 n, err = upload.Write(buf

Full Screen

Full Screen

FileMatches

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 client, err := storage.NewClient(ctx, option.WithCredentialsFile("C:/Users/abc.json"))5 if err != nil {6 fmt.Println(err)7 }8 upload := storage.NewUpload(client, "my-bucket", "file.txt")9 ok, err := upload.FileMatches(ctx, "file.txt")10 if err != nil {11 fmt.Println(err)12 }13 fmt.Println(ok)14}15Your name to display (optional):

Full Screen

Full Screen

FileMatches

Using AI Code Generation

copy

Full Screen

1import (2func TestUpload(t *testing.T) {3 api := rest.NewApi()4 api.Use(rest.DefaultDevStack...)5 api.Use(&rest.UploadMiddleware{6 })7 router, err := rest.MakeRouter(8 rest.Post("/upload", uploadFile),9 rest.Get("/download", downloadFile),10 if err != nil {11 t.Fatal(err)12 }13 api.SetApp(router)14 handler := api.MakeHandler()15 recorder := httptest.NewRecorder()16 file, err := os.Open("./example.txt")17 if err != nil {18 t.Fatal(err)19 }20 defer file.Close()21 strings.NewReader("data"))22 if err != nil {23 t.Fatal(err)24 }25 req.Header.Set("Content-Type", "multipart/form-data; boundary=1234")26 req.Header.Set("Content-Length", "100")27 req.Header.Set("Content-Disposition", `form-data; name="file"; filename="example.txt"`)28 req.Body = ioutil.NopCloser(file)29 handler.ServeHTTP(recorder, req)30 if recorder.Code != 200 {31 t.Fatal(recorder.Code)32 }33 recorder = httptest.NewRecorder()34 if err != nil {35 t.Fatal(err)36 }37 handler.ServeHTTP(recorder, req)38 if recorder.Code != 200 {39 t.Fatal(recorder.Code)40 }41 if recorder.Header().Get("Content-Type") != "text/plain" {42 t.Fatal(recorder.Header().Get("Content-Type"))43 }44 if recorder.Header().Get("Content-Disposition") != `attachment; filename="example.txt"` {45 t.Fatal(recorder.Header().Get("Content-Disposition"))46 }47 if recorder.Body.String() != "example file" {48 t.Fatal(recorder.Body.String())49 }50}51func uploadFile(w rest.ResponseWriter, r *rest

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selenoid automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful