Skip to content

Commit ad0427e

Browse files
committed
updated to OAM v0.7.0
1 parent 6acf034 commit ad0427e

File tree

8 files changed

+363
-224
lines changed

8 files changed

+363
-224
lines changed

cmd/amass/emails.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// Copyright © by Jeff Foley 2024. All rights reserved.
1+
// Copyright © by Jeff Foley 2017-2024. All rights reserved.
22
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
33
// SPDX-License-Identifier: Apache-2.0
44

55
package main
66

77
import (
88
"bytes"
9-
"context"
109
"flag"
1110
"io"
1211
"os"
@@ -15,8 +14,8 @@ import (
1514

1615
"github.com/caffix/stringset"
1716
"github.com/fatih/color"
17+
assetdb "github.com/owasp-amass/asset-db"
1818
"github.com/owasp-amass/config/config"
19-
"github.com/owasp-amass/engine/graph"
2019
oam "github.com/owasp-amass/open-asset-model"
2120
"github.com/owasp-amass/open-asset-model/contact"
2221
"github.com/owasp-amass/open-asset-model/domain"
@@ -119,7 +118,7 @@ func runEmailsCommand(clArgs []string) {
119118
showEmails(&args, db)
120119
}
121120

122-
func showEmails(args *emailsArgs, db *graph.Graph) {
121+
func showEmails(args *emailsArgs, db *assetdb.AssetDB) {
123122
var err error
124123
var outfile *os.File
125124
domains := args.Domains.Slice()
@@ -138,7 +137,7 @@ func showEmails(args *emailsArgs, db *graph.Graph) {
138137
_, _ = outfile.Seek(0, 0)
139138
}
140139

141-
addrs := getAddresses(context.Background(), domains, db)
140+
addrs := getAddresses(db, domains)
142141
if len(addrs) == 0 {
143142
r.Println("No email addresses were discovered")
144143
return
@@ -149,7 +148,7 @@ func showEmails(args *emailsArgs, db *graph.Graph) {
149148
}
150149
}
151150

152-
func getAddresses(ctx context.Context, domains []string, g *graph.Graph) []string {
151+
func getAddresses(db *assetdb.AssetDB, domains []string) []string {
153152
if len(domains) == 0 {
154153
return nil
155154
}
@@ -163,7 +162,7 @@ func getAddresses(ctx context.Context, domains []string, g *graph.Graph) []strin
163162
fqdns = append(fqdns, &domain.FQDN{Name: d})
164163
}
165164

166-
assets, err := g.DB.FindByScope(fqdns, qtime)
165+
assets, err := db.FindByScope(fqdns, qtime)
167166
if err != nil {
168167
return nil
169168
}

cmd/amass/main.go

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ package main
2525
import (
2626
"bufio"
2727
"bytes"
28+
"embed"
2829
"flag"
2930
"fmt"
3031
"io"
3132
"log/slog"
33+
"math/rand"
3234
"net"
3335
"net/netip"
3436
"os"
@@ -39,14 +41,22 @@ import (
3941

4042
"github.com/caffix/stringset"
4143
"github.com/fatih/color"
44+
"github.com/glebarez/sqlite"
4245
"github.com/owasp-amass/amass/v4/format"
46+
assetdb "github.com/owasp-amass/asset-db"
47+
db "github.com/owasp-amass/asset-db"
48+
pgmigrations "github.com/owasp-amass/asset-db/migrations/postgres"
49+
sqlitemigrations "github.com/owasp-amass/asset-db/migrations/sqlite3"
50+
"github.com/owasp-amass/asset-db/repository"
4351
"github.com/owasp-amass/config/config"
44-
"github.com/owasp-amass/engine/graph"
4552
et "github.com/owasp-amass/engine/types"
4653
"github.com/owasp-amass/open-asset-model/domain"
4754
oamnet "github.com/owasp-amass/open-asset-model/network"
55+
migrate "github.com/rubenv/sql-migrate"
4856
slogcommon "github.com/samber/slog-common"
4957
slogsyslog "github.com/samber/slog-syslog/v2"
58+
"gorm.io/driver/postgres"
59+
"gorm.io/gorm"
5060
)
5161

5262
const (
@@ -153,29 +163,88 @@ func createOutputDirectory(cfg *config.Config) {
153163
}
154164
}
155165

156-
func openGraphDatabase(cfg *config.Config) *graph.Graph {
166+
func openGraphDatabase(cfg *config.Config) *assetdb.AssetDB {
157167
// Add the local database settings to the configuration
158168
cfg.GraphDBs = append(cfg.GraphDBs, cfg.LocalDatabaseSettings(cfg.GraphDBs))
159169

160170
for _, db := range cfg.GraphDBs {
161171
if db.Primary {
162-
var g *graph.Graph
172+
var dbase *assetdb.AssetDB
163173

164174
if db.System == "local" {
165-
g = graph.NewGraph(db.System, filepath.Join(config.OutputDirectory(cfg.Dir), "amass.sqlite"), db.Options)
175+
dbase = NewGraph(db.System, filepath.Join(config.OutputDirectory(cfg.Dir), "amass.sqlite"), db.Options)
166176
} else {
167177
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s", db.Host, db.Port, db.Username, db.Password, db.DBName)
168-
g = graph.NewGraph(db.System, connStr, db.Options)
178+
dbase = NewGraph(db.System, connStr, db.Options)
169179
}
170180

171-
if g != nil {
172-
return g
181+
if dbase != nil {
182+
return dbase
173183
}
174184
break
175185
}
176186
}
177187

178-
return graph.NewGraph("memory", "", "")
188+
return NewGraph("memory", "", "")
189+
}
190+
191+
func NewGraph(system, path string, options string) *assetdb.AssetDB {
192+
var dsn string
193+
var dbtype repository.DBType
194+
195+
switch system {
196+
case "memory":
197+
dbtype = repository.SQLite
198+
dsn = fmt.Sprintf("file:sqlite%d?mode=memory&cache=shared", rand.Int31n(100))
199+
case "local":
200+
dbtype = repository.SQLite
201+
dsn = path
202+
case "postgres":
203+
dbtype = repository.Postgres
204+
dsn = path
205+
default:
206+
return nil
207+
}
208+
209+
store := db.New(dbtype, dsn)
210+
if store == nil {
211+
return nil
212+
}
213+
214+
var name string
215+
var fs embed.FS
216+
var database gorm.Dialector
217+
switch dbtype {
218+
case repository.SQLite:
219+
name = "sqlite3"
220+
fs = sqlitemigrations.Migrations()
221+
database = sqlite.Open(dsn)
222+
case repository.Postgres:
223+
name = "postgres"
224+
fs = pgmigrations.Migrations()
225+
database = postgres.Open(dsn)
226+
}
227+
228+
sql, err := gorm.Open(database, &gorm.Config{})
229+
if err != nil {
230+
return nil
231+
}
232+
233+
migrationsSource := migrate.EmbedFileSystemMigrationSource{
234+
FileSystem: fs,
235+
Root: "/",
236+
}
237+
238+
sqlDb, err := sql.DB()
239+
if err != nil {
240+
panic(err)
241+
}
242+
243+
_, err = migrate.Exec(sqlDb, name, migrationsSource, migrate.Up)
244+
if err != nil {
245+
panic(err)
246+
}
247+
return store
179248
}
180249

181250
func getWordList(reader io.Reader) ([]string, error) {

0 commit comments

Comments
 (0)