2023-09-26 16:55:25 +05:30
|
|
|
package fs
|
|
|
|
|
|
|
|
|
|
import (
|
2025-07-18 13:40:58 -05:00
|
|
|
"context"
|
2023-09-26 16:55:25 +05:30
|
|
|
"os"
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
|
2023-09-26 16:55:25 +05:30
|
|
|
)
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// ListDir lists itemType values within a directory
|
2023-09-26 16:55:25 +05:30
|
|
|
// depending on the itemType provided
|
2024-02-07 21:45:40 +05:30
|
|
|
// itemType can be any one of ['file','dir',”]
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const fs = require('nuclei/fs');
|
|
|
|
|
// // this will only return files in /tmp directory
|
|
|
|
|
// const files = fs.ListDir('/tmp', 'file');
|
|
|
|
|
// ```
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const fs = require('nuclei/fs');
|
|
|
|
|
// // this will only return directories in /tmp directory
|
|
|
|
|
// const dirs = fs.ListDir('/tmp', 'dir');
|
|
|
|
|
// ```
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const fs = require('nuclei/fs');
|
|
|
|
|
// // when no itemType is provided, it will return both files and directories
|
|
|
|
|
// const items = fs.ListDir('/tmp');
|
|
|
|
|
// ```
|
2025-07-18 13:40:58 -05:00
|
|
|
func ListDir(ctx context.Context, path string, itemType string) ([]string, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
finalPath, err := protocolstate.NormalizePathWithExecutionId(executionId, path)
|
2023-09-26 16:55:25 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
values, err := os.ReadDir(finalPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var results []string
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
if itemType == "file" && value.IsDir() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if itemType == "dir" && !value.IsDir() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
results = append(results, value.Name())
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadFile reads file contents within permitted paths
|
2024-02-07 21:45:40 +05:30
|
|
|
// and returns content as byte array
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const fs = require('nuclei/fs');
|
|
|
|
|
// // here permitted directories are $HOME/nuclei-templates/*
|
|
|
|
|
// const content = fs.ReadFile('helpers/usernames.txt');
|
|
|
|
|
// ```
|
2025-07-18 13:40:58 -05:00
|
|
|
func ReadFile(ctx context.Context, path string) ([]byte, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
finalPath, err := protocolstate.NormalizePathWithExecutionId(executionId, path)
|
2023-09-26 16:55:25 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
bin, err := os.ReadFile(finalPath)
|
|
|
|
|
return bin, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadFileAsString reads file contents within permitted paths
|
|
|
|
|
// and returns content as string
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const fs = require('nuclei/fs');
|
|
|
|
|
// // here permitted directories are $HOME/nuclei-templates/*
|
|
|
|
|
// const content = fs.ReadFileAsString('helpers/usernames.txt');
|
|
|
|
|
// ```
|
2025-07-18 13:40:58 -05:00
|
|
|
func ReadFileAsString(ctx context.Context, path string) (string, error) {
|
|
|
|
|
bin, err := ReadFile(ctx, path)
|
2023-09-26 16:55:25 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return string(bin), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadFilesFromDir reads all files from a directory
|
2024-02-07 21:45:40 +05:30
|
|
|
// and returns a string array with file contents of all files
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const fs = require('nuclei/fs');
|
|
|
|
|
// // here permitted directories are $HOME/nuclei-templates/*
|
|
|
|
|
// const contents = fs.ReadFilesFromDir('helpers/ssh-keys');
|
|
|
|
|
// log(contents);
|
|
|
|
|
// ```
|
2025-07-18 13:40:58 -05:00
|
|
|
func ReadFilesFromDir(ctx context.Context, dir string) ([]string, error) {
|
|
|
|
|
files, err := ListDir(ctx, dir, "file")
|
2023-09-26 16:55:25 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var results []string
|
|
|
|
|
for _, file := range files {
|
2025-07-18 13:40:58 -05:00
|
|
|
content, err := ReadFileAsString(ctx, dir+"/"+file)
|
2023-09-26 16:55:25 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
results = append(results, content)
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|