forked from craigz28/firmwalker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirmwalker.cpp
More file actions
255 lines (252 loc) · 7.29 KB
/
firmwalker.cpp
File metadata and controls
255 lines (252 loc) · 7.29 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <fstream>
#include <filesystem>
#include <regex>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <locale>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace std;
namespace fs = std::filesystem;
int main(int argc, char *argv[]) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
if(argc > 3 || argc < 2) {
cout << "使用方法\n" << argv[0] << " + 输入文件 + [可选:输出文件,默认firmwalker.txt]" << endl;
return 1;
}
string firmdir = argv[1];
string output_file = (argc == 3) ? argv[2] : "firmwalker.txt";
if(!fs::exists(firmdir)) {
cout << "输入的文件夹不存在" << endl;
return 1;
}
ofstream out(output_file);
auto msg = [&](const string &s) {
cout << s << endl;
out << s << endl;
};
auto getArray = [&](const string &path) -> vector<string> {
vector<string> ret;
ifstream in(path);
if(!in.is_open()) {
msg("无法打开文件: " + path);
return ret;
}
string line;
while(getline(in, line)) {
ret.emplace_back(line);
}
in.close();
return ret;
};
msg("------------firmwalker-------------");
msg("");
msg("***固件地址***");
msg(firmdir);
msg("****正在搜索密码文件***");
auto passfiles = getArray("data/passfiles");
for(auto &passfile : passfiles) {
msg("############################################# " + passfile);
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().filename() == passfile) {
msg(entry.path().string());
}
}
msg("");
}
msg("");
msg("***正在搜索Unix-MD5哈希***");
regex md5_regex(R"(\$1\$\w{8}\S{23})");
set<string> md5_set;
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(!fs::is_regular_file(entry.path())) continue;
ifstream in(entry.path());
if(!in.is_open()) continue;
string line;
while(getline(in, line)) {
sregex_iterator it(line.begin(), line.end(), md5_regex);
sregex_iterator end;
for(; it != end; ++it) {
md5_set.insert(it->str());
}
}
in.close();
}
for(const auto &md5 : md5_set) {
msg(md5);
}
msg("");
// 跳过SSL相关文件
msg("***正在搜索SSH相关文件***");
auto sshfiles = getArray("data/sshfiles");
for(auto &sshfile : sshfiles) {
msg("############################################# " + sshfile);
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().filename() == sshfile) {
msg(entry.path().string());
}
}
msg("");
}
msg("");
msg("***正在搜索files文件夹***");
auto files = getArray("data/files");
for(auto &file : files) {
msg("############################################# " + file);
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().filename() == file) {
msg(entry.path().string());
}
}
msg("");
}
msg("");
msg("***正在搜索数据库文件***");
auto dbfiles = getArray("data/dbfiles");
for(auto &dbfile : dbfiles) {
msg("############################################# " + dbfile);
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().filename() == dbfile) {
msg(entry.path().string());
}
}
msg("");
}
msg("");
msg("***正在搜索shell脚本***");
msg("############################################# " "shell脚本");
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().extension() == ".sh") {
msg(entry.path().string());
}
}
msg("");
msg("***正在搜索其他.bin文件***");
msg("############################################# " "其他.bin文件");
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().extension() == ".bin") {
msg(entry.path().string());
}
}
msg("");
msg("***正在搜索文件样式***");
auto patterns = getArray("data/patterns");
for(auto &pattern : patterns) {
msg("-----------------" + pattern + "-----------------");
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(!fs::is_regular_file(entry.path())) continue;
ifstream in(entry.path());
if(!in.is_open()) continue;
string line;
while(getline(in, line)) {
if(line.find(pattern) != string::npos) {
msg(entry.path().string());
break;
}
}
in.close();
}
msg("");
}
msg("");
msg("***正在搜索网络服务器***");
msg("############################################# " "网络服务器");
auto webservers = getArray("data/webservers");
for(auto &webserver : webservers) {
msg("############################################# " + webserver);
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().filename() == webserver) {
msg(entry.path().string());
}
}
msg("");
}
msg("");
msg("***正在搜索重要二进制文件***");
msg("############################################# " "重要二进制文件");
auto binaries = getArray("data/binaries");
for(auto &binary : binaries) {
msg("############################################# " + binary);
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(entry.path().filename() == binary) {
msg(entry.path().string());
}
}
msg("");
}
msg("");
msg("***正在搜索IP地址***");
msg("############################################# IP地址");
regex ip_regex(R"(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)");
set<string> ip_set;
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(!fs::is_regular_file(entry.path())) continue;
ifstream in(entry.path());
if(!in.is_open()) continue;
string line;
while(getline(in, line)) {
sregex_iterator it(line.begin(), line.end(), ip_regex);
sregex_iterator end;
for(; it != end; ++it) {
ip_set.insert(it->str());
}
}
in.close();
}
for(const auto &ip : ip_set) {
msg(ip);
}
msg("");
msg("***正在搜索URL***");
msg("############################################# " "URL");
regex url_regex(R"((http|https)://[^/"]+)");
set<string> url_set;
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(!fs::is_regular_file(entry.path())) continue;
ifstream in(entry.path());
if(!in.is_open()) continue;
string line;
while(getline(in, line)) {
sregex_iterator it(line.begin(), line.end(), url_regex);
sregex_iterator end;
for(; it != end; ++it) {
url_set.insert(it->str());
}
}
in.close();
}
for(const auto &url : url_set) {
msg(url);
}
msg("");
msg("***正在搜索邮箱***");
msg("############################################# " "邮箱");
regex email_regex(R"(([[:alnum:]_.-]+@[[:alnum:]_.-]+?\.[[:alpha:].]{2,6}))");
set<string> email_set;
for(auto &entry : fs::recursive_directory_iterator(firmdir)) {
if(!fs::is_regular_file(entry.path())) continue;
ifstream in(entry.path());
if(!in.is_open()) continue;
string line;
while(getline(in, line)) {
sregex_iterator it(line.begin(), line.end(), email_regex);
sregex_iterator end;
for(; it != end; ++it) {
email_set.insert(it->str());
}
}
in.close();
}
for(const auto &email : email_set) {
msg(email);
}
msg("");
msg("搜索完毕");
return 0;
}