Skip to content

Commit 8149a18

Browse files
discovery
1 parent bc51e79 commit 8149a18

3 files changed

Lines changed: 174 additions & 25 deletions

File tree

cmd/init_enhanced.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ package cmd
55

66
func init() {
77
// Enhance the root command with advanced infrastructure mapping
8-
EnhanceRootCommand()
8+
// Disabled: Using the main discovery implementation in root.go instead
9+
// EnhanceRootCommand()
910
}

cmd/root.go

Lines changed: 158 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,74 @@ func runMainDiscovery(cmd *cobra.Command, args []string, log *logger.Logger, db
23032303
}
23042304
log.Info("Discovery session started", "session_id", session.ID, "target", target)
23052305

2306+
// Wait for discovery to complete and collect discovered assets
2307+
log.Info("Waiting for discovery to complete...", "session_id", session.ID)
2308+
fmt.Println("⏳ Discovery in progress...")
2309+
2310+
var discoveredAssets []*discovery.Asset
2311+
ticker := time.NewTicker(2 * time.Second)
2312+
defer ticker.Stop()
2313+
2314+
timeout := time.After(30 * time.Minute) // Maximum discovery time
2315+
var lastProgress float64 = 0
2316+
2317+
for {
2318+
select {
2319+
case <-ticker.C:
2320+
// Check session status
2321+
session, err = engine.GetSession(session.ID)
2322+
if err != nil {
2323+
return fmt.Errorf("failed to get session: %w", err)
2324+
}
2325+
2326+
// Update progress if changed
2327+
if session.Progress > lastProgress {
2328+
fmt.Printf("\r🔍 Discovery progress: %.0f%% | Assets found: %d | High-value: %d",
2329+
session.Progress, session.TotalDiscovered, session.HighValueAssets)
2330+
lastProgress = session.Progress
2331+
}
2332+
2333+
if session.Status == discovery.StatusCompleted {
2334+
fmt.Println("\n✅ Discovery completed!")
2335+
// Collect all discovered assets
2336+
for _, asset := range session.Assets {
2337+
discoveredAssets = append(discoveredAssets, asset)
2338+
}
2339+
goto discoveryComplete
2340+
} else if session.Status == discovery.StatusFailed {
2341+
fmt.Println("\n❌ Discovery failed!")
2342+
if len(session.Errors) > 0 {
2343+
for _, err := range session.Errors {
2344+
log.Error("Discovery error", "error", err)
2345+
}
2346+
}
2347+
return fmt.Errorf("discovery failed")
2348+
}
2349+
2350+
case <-timeout:
2351+
fmt.Println("\n⚠️ Discovery timeout reached")
2352+
log.Warn("Discovery timeout", "session_id", session.ID)
2353+
// Still collect what we found
2354+
for _, asset := range session.Assets {
2355+
discoveredAssets = append(discoveredAssets, asset)
2356+
}
2357+
goto discoveryComplete
2358+
}
2359+
}
2360+
2361+
discoveryComplete:
2362+
// Log discovered assets
2363+
log.Info("Discovery complete",
2364+
"session_id", session.ID,
2365+
"total_assets", len(discoveredAssets),
2366+
"high_value_assets", session.HighValueAssets)
2367+
2368+
// Group assets by type for summary
2369+
assetsByType := make(map[discovery.AssetType]int)
2370+
for _, asset := range discoveredAssets {
2371+
assetsByType[asset.Type]++
2372+
}
2373+
23062374
// Run comprehensive auth discovery if we have organization context
23072375
if orgContext != nil {
23082376
log.Info("Running comprehensive authentication discovery",
@@ -2337,26 +2405,58 @@ func runMainDiscovery(cmd *cobra.Command, args []string, log *logger.Logger, db
23372405
}
23382406
}
23392407

2340-
// Store results in SQLite database
2341-
log.Info("Storing discovery results in database")
2342-
2343-
// Create scan jobs using Nomad
2344-
log.Info("Creating Nomad jobs for scanning")
2345-
2346-
// For now, we'll use the existing scanner functionality
2347-
// TODO: Integrate with actual Nomad API
2408+
// Print discovery summary
23482409
fmt.Printf("\n📊 Discovery Summary:\n")
23492410
fmt.Printf(" Session ID: %s\n", session.ID)
23502411
fmt.Printf(" Target: %s\n", target)
2412+
fmt.Printf(" Total Assets: %d\n", len(discoveredAssets))
2413+
for assetType, count := range assetsByType {
2414+
fmt.Printf(" - %s: %d\n", assetType, count)
2415+
}
23512416
if orgContext != nil {
23522417
fmt.Printf(" Organization: %s\n", orgContext.OrgName)
2353-
fmt.Printf(" Domains: %d\n", len(orgContext.KnownDomains))
2418+
fmt.Printf(" Known Domains: %d\n", len(orgContext.KnownDomains))
23542419
fmt.Printf(" IP Ranges: %d\n", len(orgContext.KnownIPRanges))
23552420
}
23562421
fmt.Printf("\n")
23572422

2423+
// Run comprehensive auth discovery on ALL discovered assets
2424+
if len(discoveredAssets) > 0 {
2425+
log.Info("Running comprehensive authentication discovery on discovered assets",
2426+
"asset_count", len(discoveredAssets))
2427+
2428+
// Create comprehensive auth discovery
2429+
authDiscovery := authdiscovery.NewComprehensiveAuthDiscovery(log)
2430+
2431+
// Process each discovered domain/URL asset
2432+
for _, asset := range discoveredAssets {
2433+
if asset.Type == discovery.AssetTypeDomain || asset.Type == discovery.AssetTypeURL || asset.Type == discovery.AssetTypeSubdomain {
2434+
log.Info("Discovering authentication methods for asset", "asset", asset.Value)
2435+
2436+
authInventory, err := authDiscovery.DiscoverAll(ctx, asset.Value)
2437+
if err != nil {
2438+
log.Error("Failed to discover auth for asset", "asset", asset.Value, "error", err)
2439+
continue
2440+
}
2441+
2442+
// Store auth findings in database
2443+
findings := convertAuthInventoryToFindings(authInventory, asset.Value, session.ID)
2444+
if err := db.SaveFindings(ctx, findings); err != nil {
2445+
log.Error("Failed to save auth findings", "error", err)
2446+
}
2447+
2448+
log.Info("Discovered authentication methods",
2449+
"asset", asset.Value,
2450+
"network_auth", getNetworkAuthCount(authInventory.NetworkAuth),
2451+
"web_auth", getWebAuthCount(authInventory.WebAuth),
2452+
"api_auth", getAPIAuthCount(authInventory.APIAuth),
2453+
"custom_auth", len(authInventory.CustomAuth))
2454+
}
2455+
}
2456+
}
2457+
23582458
// Run all available scanners
2359-
fmt.Println("🚀 Running comprehensive security scans...")
2459+
fmt.Println("🚀 Running comprehensive security scans on all discovered assets...")
23602460

23612461
// Run comprehensive scanning on discovered assets
23622462
if err := runComprehensiveScanning(ctx, session, orgContext, log, store); err != nil {
@@ -2479,19 +2579,36 @@ func runComprehensiveScanning(ctx context.Context, session *discovery.DiscoveryS
24792579

24802580
log.Info("Nomad cluster available, submitting distributed scan jobs")
24812581

2482-
// Collect all targets for scanning
2582+
// Collect all targets for scanning from discovered assets
24832583
var targets []string
2584+
seen := make(map[string]bool)
24842585

2485-
// Add organization domains
2486-
if orgContext != nil {
2487-
targets = append(targets, orgContext.KnownDomains...)
2488-
log.Info("Added organization domains to scan targets", "count", len(orgContext.KnownDomains))
2586+
// Add all discovered assets
2587+
for _, asset := range session.Assets {
2588+
if asset.Type == discovery.AssetTypeDomain || asset.Type == discovery.AssetTypeURL {
2589+
if !seen[asset.Value] {
2590+
targets = append(targets, asset.Value)
2591+
seen[asset.Value] = true
2592+
}
2593+
}
2594+
}
2595+
2596+
// Add organization domains if no assets discovered
2597+
if len(targets) == 0 && orgContext != nil {
2598+
for _, domain := range orgContext.KnownDomains {
2599+
if !seen[domain] {
2600+
targets = append(targets, domain)
2601+
seen[domain] = true
2602+
}
2603+
}
24892604
}
24902605

2491-
// Add discovered assets from session
2606+
// Fallback to original target if nothing found
24922607
if len(targets) == 0 {
2493-
targets = append(targets, session.Target.Value) // fallback to original target
2608+
targets = append(targets, session.Target.Value)
24942609
}
2610+
2611+
log.Info("Collected scanning targets", "count", len(targets), "targets", targets)
24952612

24962613
// Submit scanner jobs to Nomad
24972614
var submittedJobs []string
@@ -2566,19 +2683,36 @@ func runComprehensiveScanning(ctx context.Context, session *discovery.DiscoveryS
25662683
func runComprehensiveScanningLocal(ctx context.Context, session *discovery.DiscoverySession, orgContext *discovery.OrganizationContext, log *logger.Logger, store core.ResultStore) error {
25672684
log.Info("Starting local comprehensive security scanning", "session_id", session.ID)
25682685

2569-
// Collect all targets for scanning
2686+
// Collect all targets for scanning from discovered assets
25702687
var targets []string
2688+
seen := make(map[string]bool)
25712689

2572-
// Add organization domains
2573-
if orgContext != nil {
2574-
targets = append(targets, orgContext.KnownDomains...)
2575-
log.Info("Added organization domains to scan targets", "count", len(orgContext.KnownDomains))
2690+
// Add all discovered assets
2691+
for _, asset := range session.Assets {
2692+
if asset.Type == discovery.AssetTypeDomain || asset.Type == discovery.AssetTypeURL {
2693+
if !seen[asset.Value] {
2694+
targets = append(targets, asset.Value)
2695+
seen[asset.Value] = true
2696+
}
2697+
}
25762698
}
25772699

2578-
// Add discovered assets from session
2700+
// Add organization domains if no assets discovered
2701+
if len(targets) == 0 && orgContext != nil {
2702+
for _, domain := range orgContext.KnownDomains {
2703+
if !seen[domain] {
2704+
targets = append(targets, domain)
2705+
seen[domain] = true
2706+
}
2707+
}
2708+
}
2709+
2710+
// Fallback to original target if nothing found
25792711
if len(targets) == 0 {
2580-
targets = append(targets, session.Target.Value) // fallback to original target
2712+
targets = append(targets, session.Target.Value)
25812713
}
2714+
2715+
log.Info("Collected scanning targets", "count", len(targets), "targets", targets)
25822716

25832717
// Run SCIM scanning locally
25842718
log.Info("Running local SCIM vulnerability scans")

internal/discovery/engine.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,20 @@ func (e *Engine) runDiscovery(session *DiscoverySession) {
410410
// Post-process assets
411411
e.postProcessAssets(session)
412412

413+
// Update session status before returning
414+
e.mutex.Lock()
415+
if storedSession, exists := e.sessions[session.ID]; exists {
416+
storedSession.Progress = 100.0
417+
storedSession.Status = StatusCompleted
418+
storedSession.TotalDiscovered = session.TotalDiscovered
419+
storedSession.HighValueAssets = session.HighValueAssets
420+
storedSession.Assets = session.Assets
421+
storedSession.Relationships = session.Relationships
422+
now := time.Now()
423+
storedSession.CompletedAt = &now
424+
}
425+
e.mutex.Unlock()
426+
413427
e.logger.Info("Discovery completed",
414428
"session_id", session.ID,
415429
"total_assets", session.TotalDiscovered,

0 commit comments

Comments
 (0)