Skip to content

Commit

Permalink
create util/system pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsullivan committed Aug 3, 2022
1 parent 810e468 commit 6019eb0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
24 changes: 5 additions & 19 deletions befehl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
Expand All @@ -18,6 +17,8 @@ import (
"github.com/fatih/color"
"github.com/howeyc/gopass"
"github.com/spf13/viper"

"github.com/sgsullivan/befehl/util/system"
)

type queue struct {
Expand All @@ -41,7 +42,7 @@ func New(config *viper.Viper) *Instance {
}

func (instance *Instance) Fire(targets, payload string, routines int) {
bytePayload := readFile(payload)
bytePayload := system.ReadFile(payload)
instance.populateSshKey()
instance.fireTorpedos(bytePayload, targets, routines)
}
Expand All @@ -56,7 +57,7 @@ func (instance *Instance) populateSshKey() {
if instance.viperConfig.GetString("auth.privatekeyfile") != "" {
privKeyFile = instance.viperConfig.GetString("auth.privatekeyfile")
}
rawKey := readFile(privKeyFile)
rawKey := system.ReadFile(privKeyFile)
privKeyBytes, _ := pem.Decode(rawKey)

if x509.IsEncryptedPEMBlock(privKeyBytes) {
Expand Down Expand Up @@ -205,7 +206,7 @@ func (instance *Instance) logPayloadRun(host string, output string) {
logDir = instance.viperConfig.GetString("general.logdir")
}
logFile := logDir + "/" + host
if !pathExists(logDir) {
if !system.PathExists(logDir) {
if err := os.MkdirAll(logDir, os.FileMode(0700)); err != nil {
panic(fmt.Sprintf("Failed creating [%s]: %s\n", logDir, err))
}
Expand All @@ -223,21 +224,6 @@ func (instance *Instance) logPayloadRun(host string, output string) {
log.Printf("payload completed on %s! logfile at: %s\n", host, logFile)
}

func readFile(file string) []byte {
read, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
return read
}

func pathExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}

func wgTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
Expand Down
21 changes: 21 additions & 0 deletions util/system/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package system

import (
"io/ioutil"
"os"
)

func ReadFile(file string) []byte {
read, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
return read
}

func PathExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}

0 comments on commit 6019eb0

Please sign in to comment.