Skip to content

Commit 5b1ab41

Browse files
committed
v1.1.23 - added simple filesystem operations
1 parent d6525db commit 5b1ab41

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

filesystem_manipulation.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package peirates
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func displayFile(filePath string) error {
9+
// Open the file
10+
file, err := os.Open(filePath)
11+
if err != nil {
12+
return fmt.Errorf("failed opening file: %w", err)
13+
}
14+
defer file.Close()
15+
16+
// Read the file content
17+
content, err := os.ReadFile(filePath)
18+
if err != nil {
19+
return fmt.Errorf("failed reading file: %w", err)
20+
}
21+
22+
// Print the content of the file
23+
fmt.Println(string(content))
24+
return nil
25+
}
26+
27+
func listDirectory(dirPath string) error {
28+
// Open the directory
29+
dir, err := os.Open(dirPath)
30+
if err != nil {
31+
return fmt.Errorf("failed opening directory: %w", err)
32+
}
33+
defer dir.Close()
34+
35+
// Read the directory contents
36+
files, err := dir.Readdir(-1)
37+
if err != nil {
38+
return fmt.Errorf("failed reading directory: %w", err)
39+
}
40+
41+
// Print the names of the files and directories
42+
for _, file := range files {
43+
fmt.Println(file.Name())
44+
}
45+
return nil
46+
}
47+
48+
func changeDirectory(dirPath string) error {
49+
if err := os.Chdir(dirPath); err != nil {
50+
return fmt.Errorf("failed to change directory: %w", err)
51+
}
52+
return nil
53+
}
54+
55+
func getCurrentDirectory() (string, error) {
56+
cwd, err := os.Getwd()
57+
if err != nil {
58+
return "", fmt.Errorf("failed to get current directory: %w", err)
59+
}
60+
return cwd, nil
61+
}

menu.go

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Menu |
3333
[ curl ] Make an HTTP request (GET or POST) to a user-specified URL
3434
[ tcpscan ] Run a simple all-ports TCP port scan against an IP address
3535
[ enumerate-dns ] Enumerate services via DNS *
36+
[ cd , pwd , ls , cat ] Manipulate the filesystem via Golang-native commands
3637
[ shell <command> ] Run a shell command
3738
3839
[ full ] Switch to full (classic menu) with a longer list of commands
@@ -91,6 +92,7 @@ Off-Menu +
9192
[92] Deactivate "auth can-i" checking before attempting actions [set-auth-can-i]
9293
[93] Run a simple all-ports TCP port scan against an IP address [tcpscan]
9394
[94] Enumerate services via DNS [enumerate-dns] *
95+
[] Manipulate the filesystem [ cd , pwd , ls , cat ]
9496
[] Run a shell command [shell <command and arguments>]
9597
9698
[short] Reduce the set of visible commands in this menu
@@ -270,6 +272,11 @@ func setUpCompletionMainMenu() *readline.PrefixCompleter {
270272
readline.PcItem("tcpscan"),
271273
// [94] Enumerate services via DNS [enumerate-dns] *
272274
readline.PcItem("enumerate-dns"),
275+
// [ cd __ , pwd , ls ___ , cat ___ ] Manipulate the filesystem via Golang-native commands
276+
readline.PcItem("cd"),
277+
readline.PcItem("pwd"),
278+
readline.PcItem("ls"),
279+
readline.PcItem("cat"),
273280
// [] Run a shell command [shell <command and arguments>]
274281
readline.PcItem("shell"),
275282
// [short] Reduce the set of visible commands in this menu

peirates.go

+96-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var UseAuthCanI bool = true
2727
// Main starts Peirates[]
2828
func Main() {
2929
// Peirates version string
30-
var version = "1.1.22"
30+
var version = "1.1.23"
3131

3232
var err error
3333

@@ -227,6 +227,101 @@ func Main() {
227227
continue
228228
}
229229

230+
//
231+
// Handle built-in filesystem commands before the switch menu
232+
//
233+
234+
const pwd = "pwd"
235+
if input == pwd {
236+
// Print the current working directory
237+
cwd, error := getCurrentDirectory()
238+
if error != nil {
239+
println("Error getting current directory: " + error.Error())
240+
continue
241+
}
242+
println(cwd)
243+
pauseToHitEnter(interactive)
244+
continue
245+
}
246+
247+
const cdSpace = "cd "
248+
if strings.HasPrefix(input, cdSpace) {
249+
250+
// Trim off the newline - should we do this for all input anyway?
251+
input = strings.TrimSuffix(input, "\n")
252+
// Trim off the cd, then grab the argument.
253+
// This will fail if there are spaces in the directory name - TODO: improve this.
254+
argumentsLine := strings.TrimPrefix(input, cdSpace)
255+
arguments := strings.Fields(argumentsLine)
256+
directory := arguments[0]
257+
// remove the cd, then try to change to that directory
258+
changeDirectory(directory)
259+
260+
// Get the new directory and print its name
261+
cwd, error := getCurrentDirectory()
262+
if error != nil {
263+
println("Error getting current directory: " + error.Error())
264+
continue
265+
}
266+
println(cwd)
267+
268+
pauseToHitEnter(interactive)
269+
continue
270+
}
271+
272+
// cat to display files
273+
const catSpace = "cat "
274+
if strings.HasPrefix(input, catSpace) {
275+
// Trim off the newline - should we do this for all input anyway?
276+
input = strings.TrimSuffix(input, "\n")
277+
// remove the cat, then split the rest on whitespace
278+
argumentsLine := strings.TrimPrefix(input, catSpace)
279+
spaceDelimitedSet := strings.Fields(argumentsLine)
280+
for _, file := range spaceDelimitedSet {
281+
err := displayFile(file)
282+
if err != nil {
283+
println("Error displaying file: " + file + " due to " + err.Error())
284+
}
285+
}
286+
pauseToHitEnter(interactive)
287+
continue
288+
}
289+
290+
// ls to list directories - treat this differently if it has no arguments
291+
292+
const lsSpace = "ls "
293+
if strings.HasPrefix(input, lsSpace) {
294+
// Trim off the newline - should we do this for all input anyway?
295+
input = strings.TrimSuffix(input, "\n")
296+
// remove the ls, then split the rest on whitespace
297+
argumentsLine := strings.TrimPrefix(input, lsSpace)
298+
spaceDelimitedSet := strings.Fields(argumentsLine)
299+
for _, dir := range spaceDelimitedSet {
300+
// Check for flags - reject them
301+
if strings.HasPrefix(dir, "-") {
302+
println("ERROR: Flags are not supported in this version of ls.")
303+
continue
304+
}
305+
err := listDirectory(dir)
306+
if err != nil {
307+
println("Error listing directory: " + dir + " due to " + err.Error())
308+
}
309+
}
310+
pauseToHitEnter(interactive)
311+
continue
312+
}
313+
314+
// ls with no arguments means list the current directory
315+
const ls = "ls"
316+
if strings.HasPrefix(input, ls) {
317+
error := listDirectory(".")
318+
if error != nil {
319+
println("Error listing directory: " + error.Error())
320+
}
321+
pauseToHitEnter(interactive)
322+
continue
323+
}
324+
230325
// Handle shell commands before the switch menu
231326
const shellSpace = "shell "
232327
const shell = "shell"

0 commit comments

Comments
 (0)