diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/aws-enumerator.iml b/.idea/aws-enumerator.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/aws-enumerator.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a917358 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod index ba09cb9..2904980 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/shabarkin/aws-enumerator go 1.15 require ( + github.com/aws/aws-sdk-go-v2 v1.3.2 // indirect github.com/aws/aws-sdk-go-v2/config v1.1.5 github.com/aws/aws-sdk-go-v2/service/acm v1.2.2 github.com/aws/aws-sdk-go-v2/service/amplify v1.1.5 diff --git a/helper/helper.go b/helper/helper.go index d612300..fadd5e9 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -3,9 +3,9 @@ package helper import ( "flag" "fmt" - "github.com/shabarkin/aws-enumerator/servicemaster" "github.com/shabarkin/aws-enumerator/servicestructs" + "github.com/shabarkin/aws-enumerator/utils" ) @@ -20,18 +20,20 @@ func changeSpeedForTime(speed string) (time int) { return time } -func SetEnumerationPipeline(services, speed *string) { +func SetEnumerationPipeline(services, speed *string, profile *string) { // Load Global Variables from file - utils.LoadEnv() - - if servicemaster.CheckAWSCredentials() { + if *profile == "" { + fmt.Println(utils.Green("Message: "), utils.Yellow("No AWS profile specified, attempting to load from .env file")) + utils.LoadEnv() + } + fmt.Println(utils.Green("Message: "), utils.Yellow("profile name: "), utils.Cyan(*profile)) + if servicemaster.CheckAWSCredentials(profile) { servicemaster.ServiceCall( - servicestructs.GetServices(), + servicestructs.GetServices(profile), utils.ProcessServiceArgument(*services), changeSpeedForTime(*speed), ) - } } @@ -80,10 +82,11 @@ aws-enumerator enum [command] Flags: -services Enumerate permissions specifying services divided by comma or 'all' for total enumeration -speed Speed parameter has three defitions : fast or normal or slow (default is normal) + -profile Specify AWS profile to use for authentication Example: - ./aws-enumerator enum -services iam,sts,s3,ec2 -speed normal - ./aws-enumerator enum -services all + ./aws-enumerator enum -services iam,sts,s3,ec2 -speed normal -profile pwnedlabs + ./aws-enumerator enum -services all -profile pwnedlabs ` var Cloudrider_dump_help string = `Usage: @@ -120,6 +123,7 @@ var AWS_session_token *string = Cred.String("aws_session_token", "", "") var Enum *flag.FlagSet = flag.NewFlagSet("enum", flag.ExitOnError) var Services_enum *string = Enum.String("services", "all", "") var Speed *string = Enum.String("speed", "normal", "") +var Profile *string = Enum.String("profile", "", "") var Dump *flag.FlagSet = flag.NewFlagSet("dump", flag.ExitOnError) var Services_dump *string = Dump.String("services", "all", "") diff --git a/main.go b/main.go index 93c0418..1eca92d 100644 --- a/main.go +++ b/main.go @@ -33,8 +33,8 @@ func main() { fmt.Println(utils.Green("Message: "), utils.Yellow("File"), utils.Red(".env"), utils.Yellow("with AWS credentials were created in current folder")) case "enum": helper.Enum.Parse(os.Args[2:]) - helper.SetEnumerationPipeline(helper.Services_enum, helper.Speed) - fmt.Println(utils.Green("Message: "), utils.Yellow("Enumeration finished")) + helper.SetEnumerationPipeline(helper.Services_enum, helper.Speed, helper.Profile) + fmt.Println(utils.Green("Message:"), utils.Yellow("Enumeration Finished")) case "dump": helper.Dump.Parse(os.Args[2:]) diff --git a/servicemaster/servicemaster.go b/servicemaster/servicemaster.go index 9df3536..416a98d 100644 --- a/servicemaster/servicemaster.go +++ b/servicemaster/servicemaster.go @@ -3,6 +3,7 @@ package servicemaster import ( "context" "fmt" + "github.com/aws/aws-sdk-go-v2/aws" "io/ioutil" "log" "os" @@ -16,6 +17,8 @@ import ( "github.com/shabarkin/aws-enumerator/utils" ) +var PositiveResults []string + type ServiceMaster struct { Svc interface{} SvcName string @@ -74,6 +77,10 @@ func (svc *ServiceMaster) control_node() { defer close(svc.api_call_result_channel) defer close(svc.api_call_error_channel) fmt.Println(utils.Green("Message: "), utils.Yellow("Successful"), utils.Yellow(strings.ToUpper(svc.SvcName))+utils.Yellow(":"), utils.Green(svc.result_counter-svc.error_counter), utils.Yellow("/"), utils.Red(svc.result_counter)) + if svc.result_counter-svc.error_counter > 0 { + message := utils.Green("Message: ") + utils.Yellow("Successful ") + utils.Yellow(strings.ToUpper(svc.SvcName)) + utils.Yellow(": ") + utils.Green(svc.result_counter-svc.error_counter) + utils.Yellow("/") + utils.Red(svc.result_counter) + PositiveResults = append(PositiveResults, message) + } break } @@ -138,28 +145,35 @@ func (svc *ServiceMaster) save_result_to_file() { ioutil.WriteFile(utils.ERROR_FILEPATH+svc.SvcName+"_errors.json", []byte(file_errors), 0644) } -func CheckAWSCredentials() bool { - if utils.CheckEnvFileExistance() { - cfg, err := config.LoadDefaultConfig(context.TODO()) - if err != nil { - fmt.Println(utils.Red("Error:"), utils.Yellow("Unable to load SDK config,")) - fmt.Println(utils.Green("Fix:"), utils.Yellow("The problem should be on our side, contact support please")) - fmt.Println(utils.Red("Trace:"), utils.Yellow(err)) - os.Exit(1) - } +func CheckAWSCredentials(profile *string) bool { + var ( + cfg aws.Config + err error + ) - sts_svc := sts.NewFromConfig(cfg) - _, aws_err := sts_svc.GetCallerIdentity(context.TODO(), &sts.GetCallerIdentityInput{}) - if aws_err != nil { - fmt.Println(utils.Red("Error:"), utils.Yellow("AWS Credentials are not valid")) - fmt.Println(utils.Green("Fix:"), utils.Yellow("Provide AWS Credentials, use `./aws-enumerator cred -h` command")) - fmt.Println(utils.Red("Trace:"), utils.Yellow(aws_err)) - os.Exit(1) - } - return true - } else { - return false + if *profile == "" { + cfg, err = config.LoadDefaultConfig(context.TODO()) + } + + cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(*profile)) + if err != nil { + fmt.Println(utils.Red("Error:"), utils.Yellow("Unable to load SDK config,")) + fmt.Println(utils.Green("Fix:"), utils.Yellow("The problem should be on our side, contact support please")) + fmt.Println(utils.Red("Trace:"), utils.Yellow(err)) + os.Exit(1) + } + + sts_svc := sts.NewFromConfig(cfg) + output, aws_err := sts_svc.GetCallerIdentity(context.TODO(), &sts.GetCallerIdentityInput{}) + if aws_err != nil { + fmt.Println(utils.Red("Error:"), utils.Yellow("AWS Credentials are not valid")) + fmt.Println(utils.Green("Fix:"), utils.Yellow("Provide AWS Credentials, use `./aws-enumerator cred -h` command")) + fmt.Println(utils.Red("Trace:"), utils.Yellow(aws_err)) + os.Exit(1) } + fmt.Println(utils.Green("Message: "), utils.Yellow("AWS Credentials are valid: "), utils.Cyan(*output.Arn)) + return true + } func sleep_delay(i, speed int) { @@ -195,7 +209,12 @@ func ServiceCall(AllAWSServices []ServiceMaster, wanted_services []string, speed } wg.Wait() } + t := time.Now() elapsed := t.Sub(start) fmt.Println(utils.Green("Time:"), elapsed) + fmt.Println(utils.Green("Positive Results:")) + for i := 0; i < len(PositiveResults); i++ { + fmt.Println(PositiveResults[i]) + } } diff --git a/servicestructs/servicestructs.go b/servicestructs/servicestructs.go index ecf59e0..ef6fd44 100644 --- a/servicestructs/servicestructs.go +++ b/servicestructs/servicestructs.go @@ -2,6 +2,7 @@ package servicestructs import ( "context" + "github.com/aws/aws-sdk-go-v2/aws" "log" "github.com/aws/aws-sdk-go-v2/config" @@ -114,10 +115,18 @@ import ( "github.com/shabarkin/aws-enumerator/utils" ) -func GetServices() []servicemaster.ServiceMaster { +func GetServices(profile *string) []servicemaster.ServiceMaster { - cfg, err := config.LoadDefaultConfig(context.TODO()) + var ( + cfg aws.Config + err error + ) + if *profile == "" { + cfg, err = config.LoadDefaultConfig(context.TODO()) + } + + cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(*profile)) if err != nil { log.Fatalln(utils.Red("Error:"), utils.Yellow("Unable to load SDK config,")) } diff --git a/utils/utils.go b/utils/utils.go index 6224ea1..6f42fa3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "reflect" + "runtime" "strings" "github.com/fatih/color" @@ -44,15 +45,29 @@ func countPartWidth(terminal_width, service int) (n int) { func PrintDividedLine(service string) { - terminal_width, _ := terminal.Width() - n := countPartWidth(int(terminal_width), len(service)) + if runtime.GOOS == "windows" { + n := 20 + + fmt.Println() + if service == "" { + fmt.Println(White(strings.Repeat("-", 20))) + } else { + fmt.Println(White(strings.Repeat("-", n)), Magenta(strings.ToUpper(service)), White(strings.Repeat("-", n))) + } - fmt.Println() - if service == "" { - fmt.Println(White(strings.Repeat("-", int(terminal_width)))) } else { - fmt.Println(White(strings.Repeat("-", n)), Magenta(strings.ToUpper(service)), White(strings.Repeat("-", n))) + + terminal_width, _ := terminal.Width() + n := countPartWidth(int(terminal_width), len(service)) + + fmt.Println() + if service == "" { + fmt.Println(White(strings.Repeat("-", int(terminal_width)))) + } else { + fmt.Println(White(strings.Repeat("-", n)), Magenta(strings.ToUpper(service)), White(strings.Repeat("-", n))) + } } + } func ProcessServiceArgument(str string) []string {