Skip to content

Commit

Permalink
Added option to purge old data when doing an AD dump (for scripting)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarlslund committed Sep 28, 2022
1 parent 2a4ea61 commit bf1421e
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions modules/integrations/activedirectory/collect/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var (
gpopath = Command.Flags().String("gpopath", "", "Override path to GPOs, useful for non Windows OS'es with mounted drive (/mnt/policies/ or similar), but will break ACL feature")
AuthmodeString = Command.Flags().String("authmode", "ntlm", "Bind mode: unauth/anonymous, basic/simple, digest/md5, ntlm, ntlmpth (password is hash), negotiate/sspi")

purgeolddata = Command.Flags().Bool("purgeolddata", false, "Purge existing data from the datapath if connection to DC is successfull")

authmode AuthMode
tlsmode TLSmode
)
Expand Down Expand Up @@ -184,14 +186,6 @@ func Execute(cmd *cobra.Command, args []string) error {
datapath = idp.Value.String()
}

// Should be moved to main prerun, but I can't figure it out right now
if _, err := os.Open(datapath); os.IsNotExist(err) {
err = os.MkdirAll(datapath, 0755)
if err != nil {
return err
}
}

cp, _ := util.ParseBool(*collectgpos)
var gpostocollect []*activedirectory.RawObject
var netbiosname string
Expand Down Expand Up @@ -320,6 +314,25 @@ func Execute(cmd *cobra.Command, args []string) error {
}
}

// Auto adjust this to local domain, most users don't understand that each domain needs it's own path
if datapath == "data" {
datapath = filepath.Join("data", domainContext)
}

// Clean up old data if requested
if _, err := os.Stat(datapath); err == nil && *purgeolddata {
ui.Info().Msgf("Removing old data from %v", datapath)
os.RemoveAll(datapath)
}

// Ensure output folder exists
if _, err := os.Open(datapath); os.IsNotExist(err) {
err = os.MkdirAll(datapath, 0755)
if err != nil {
return err
}
}

ui.Info().Msg("Saving RootDSE ...")
_, err = ad.Dump(DumpOptions{
SearchBase: "",
Expand Down

0 comments on commit bf1421e

Please sign in to comment.