Black Hat Go: Go Programming for Hackers and Pentesters by Tom Steele & Chris Patten & Dan Kottmann
Author:Tom Steele & Chris Patten & Dan Kottmann
Language: eng
Format: epub, mobi
Publisher: No Starch Press, Inc.
Published: 2020-07-15T00:00:00+00:00
Implementing a MySQL Database Miner
To make your MySQL implementation work, you’ll inspect the information_schema.columns table. This table maintains metadata about all the databases and their structures, including table and column names. To make the data the simplest to consume, use the following SQL query, which removes information about some of the built-in MySQL databases that are of no consequence to your pillaging efforts:
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME FROM columns WHERE TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys') ORDER BY TABLE_SCHEMA, TABLE_NAME
The query produces results resembling the following:
+--------------+--------------+-------------+ | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | +--------------+--------------+-------------+ | store | transactions | ccnum | | store | transactions | date | | store | transactions | amount | | store | transactions | cvv | | store | transactions | exp | --snip--
Although using that query to retrieve schema information is pretty straightforward, the complexity in your code comes from logically trying to differentiate and categorize each row while defining your GetSchema() function. For example, consecutive rows of output may or may not belong to the same database or table, so associating the rows to the correct dbminer.Database and dbminer.Table instances becomes a somewhat tricky endeavor.
Listing 7-10 defines the implementation.
type MySQLMiner struct { Host string Db sql.DB } func New(host string) (*MySQLMiner, error) { m := MySQLMiner{Host: host} err := m.connect() if err != nil { return nil, err } return &m, nil } func (m *MySQLMiner) connect() error { db, err := sql.Open( "mysql", ❶ fmt.Sprintf("root:password@tcp(%s:3306)/information_schema", m.Host)) if err != nil { log.Panicln(err) } m.Db = *db return nil } func (m *MySQLMiner) GetSchema() (*dbminer.Schema, error) { var s = new(dbminer.Schema) ❷ sql := `SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME FROM columns WHERE TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys') ORDER BY TABLE_SCHEMA, TABLE_NAME` schemarows, err := m.Db.Query(sql) if err != nil { return nil, err } defer schemarows.Close() var prevschema, prevtable string var db dbminer.Database var table dbminer.Table ❸ for schemarows.Next() { var currschema, currtable, currcol string if err := schemarows.Scan(&currschema, &currtable, &currcol); err != nil { return nil, err } ❹ if currschema != prevschema { if prevschema != "" { db.Tables = append(db.Tables, table) s.Databases = append(s.Databases, db) } db = dbminer.Database{Name: currschema, Tables: []dbminer.Table{}} prevschema = currschema prevtable = "" } ❺ if currtable != prevtable { if prevtable != "" { db.Tables = append(db.Tables, table) } table = dbminer.Table{Name: currtable, Columns: []string{}} prevtable = currtable } ❻ table.Columns = append(table.Columns, currcol) } db.Tables = append(db.Tables, table) s.Databases = append(s.Databases, db) if err := schemarows.Err(); err != nil { return nil, err } return s, nil } func main() { mm, err := New(os.Args[1]) if err != nil { panic(err) } defer mm.Db.Close() if err := dbminer.Search(mm); err != nil { panic(err) } }
Listing 7-10: Creating a MySQL database miner (/ch-7/db/mysql/main.go/)
A quick glance at the code and you’ll probably realize that much of it is very, very similar to the MongoDB example in the preceding section. As a matter of fact, the main() function is identical.
The bootstrapping functions are also similar—you just change the logic to interact with MySQL rather than MongoDB.
Download
Black Hat Go: Go Programming for Hackers and Pentesters by Tom Steele & Chris Patten & Dan Kottmann.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(11893)
Hello! Python by Anthony Briggs(9373)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9343)
The Mikado Method by Ola Ellnestam Daniel Brolund(9307)
Dependency Injection in .NET by Mark Seemann(8859)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7852)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7323)
Grails in Action by Glen Smith Peter Ledbrook(7295)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7116)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5957)
Kotlin in Action by Dmitry Jemerov(4638)
Practical Vim (for Kathryn Amaral) by Drew Neil(3728)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3596)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3416)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3242)
Learning Java by Patrick Niemeyer & Daniel Leuck(2871)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2837)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(2678)
Mastering Java 9 by Dr. Edward Lavieri(2573)