This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_sleuth.rb
118 lines (111 loc) · 3.98 KB
/
database_sleuth.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class DatabaseSleuth
attr_accessor :application, :locations, :exists_at, :located_at, :regex, :dbname, :dbuser, :dbpass, :dbhost, :dbtableprefix
@@supported_applications = [:joomla, :wordpress, :drupal, :phpbb, :gallery, :zencart, :mediawiki]
@@app_metadata = {}
@@app_metadata[:drupal] = {
:locations => ["sites/default/settings.php"],
:regex => {
:dbname => Regexp.new("^\\$db_url.*?\\/\\/.*?\\/(.*?)'"),
:dbuser => Regexp.new("^\\$db_url.*?\\/\\/(.*)?:.*?'"),
:dbpass => Regexp.new("^\\$db_url.*?\\/\\/.*?:(.*)?@.*?'"),
:dbhost => Regexp.new("^\\$db_url.*?\\/\\/.*?@(.*)?\\/.*?'"),
},
}
@@app_metadata[:gallery] = {
:locations => ["config.php"],
:regex => {
:dbname => Regexp.new("\\$storeConfig\\['database'.*?'(.*?)'"),
:dbuser => Regexp.new("\\$storeConfig\\['username'.*?'(.*?)'"),
:dbpass => Regexp.new("\\$storeConfig\\['password'.*?'(.*?)'"),
:dbhost => Regexp.new("\\$storeConfig\\['hostname'.*?'(.*?)'"),
},
}
@@app_metadata[:joomla] = {
:locations => ["configuration.php"],
:regex => {
:dbname => Regexp.new("\\$db\\s*=\\s*'(.*?)'"),
:dbuser => Regexp.new("\\$user\\s*=\\s*'(.*?)'"),
:dbpass => Regexp.new("\\$password\\s*=\\s*'(.*?)'"),
:dbhost => Regexp.new("\\$host\\s*=\\s*'(.*?)'"),
},
}
@@app_metadata[:mediawiki] = {
:locations => ["LocalSettings.php"],
:regex => {
:dbname => Regexp.new("\\$wgDBname\\s*=\\s*'(.*?)'"),
:dbuser => Regexp.new("\\$wgDBuser\\s*=\\s*'(.*?)'"),
:dbpass => Regexp.new("\\$wgDBpassword\\s*=\\s*'(.*?)'"),
:dbhost => Regexp.new("\\$wgDBserver\\s*=\\s*'(.*?)'"),
},
}
@@app_metadata[:phpbb] = {
:locations => ["config.php"],
:regex => {
:dbname => Regexp.new("dbname\\s*=\\s*'(.*?)'"),
:dbuser => Regexp.new("dbuser\\s*=\\s*'(.*?)'"),
:dbpass => Regexp.new("dbpasswd\\s*=\\s*'(.*?)'"),
:dbhost => Regexp.new("dbhost\\s*=\\s*'(.*?)'"),
},
}
@@app_metadata[:wordpress] = {
:locations => ["wp-config.php"],
:regex => {
:dbname => Regexp.new("DB_NAME'.*?'(.*?)'"),
:dbuser => Regexp.new("DB_USER'.*?'(.*?)'"),
:dbpass => Regexp.new("DB_PASSWORD'.*?'(.*?)'"),
:dbhost => Regexp.new("DB_HOST'.*?'(.*?)'"),
:dbtableprefix => Regexp.new("\\$table_prefix\\s*=\\s*'(.*?)'"),
},
}
@@app_metadata[:zencart] = {
:locations => ["includes/configure.php"],
:regex => {
:dbname => Regexp.new("DB_DATABASE'.*?'(.*?)'"),
:dbuser => Regexp.new("DB_SERVER_USERNAME'.*?'(.*?)'"),
:dbpass => Regexp.new("DB_SERVER_PASSWORD'.*?'(.*?)'"),
:dbhost => Regexp.new("DB_SERVER'.*?'(.*?)'"),
},
}
def initialize(app)
@exists_at = []
@dbname = nil
@dbuser = nil
@dbpass = nil
@dbhost = nil
self.application = app
self.locations = @@app_metadata[@application][:locations]
@regex = @@app_metadata[@application][:regex]
end
def application=(app)
a = String(app).downcase.to_sym
if @@supported_applications.include?(a)
instance_variable_set(:@application, a)
else
raise "That application is not supported yet."
end
end
def find_db_info
locate_files
@exists_at.each do |file|
File.foreach(file) do |line|
@dbname ||= @regex[:dbname].match(line)[1] if @regex[:dbname] === line
@dbuser ||= @regex[:dbuser].match(line)[1] if @regex[:dbuser] === line
@dbpass ||= @regex[:dbpass].match(line)[1] if @regex[:dbpass] === line
@dbhost ||= @regex[:dbhost].match(line)[1] if @regex[:dbhost] === line
@dbtableprefix ||= @regex[:dbtableprefix].match(line)[1] if @regex[:dbtableprefix] === line && @regex.has_key?(:dbtableprefix)
@located_at = file if @dbname
end
end
end
def manual_connection_string
"mysql -u #{@dbuser} -p#{@dbpass} -h #{@dbhost} #{@dbname}"
end
private
def locate_files
@locations.each do |loc|
if File.exists?(loc)
@exists_at << loc
end
end
end
end