-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.cpp
113 lines (102 loc) · 3.42 KB
/
main.cpp
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
#include <iostream>
#include "SearchDnsHostname.h"
#include "RegConnect.h"
void print_help() {
std::wcout << "Usage example: \n"
<< " program.exe -d missyou.com -dc dc.missyou.com -o result.csv\n"
<< "Options:\n"
<< " -d, --domain=Domain domain name to search\n"
<< " -dc, --dc=domainControllers domain controller to query\n"
<< " -t, --threads=Theads number of threads, default: 1000\n"
<< " -o, --output=outputResult output result file\n"
<< " -h, --help display this help and exit\n";
}
int main(int argc, char** argv) {
std::string domain;
std::string dc;
int threads = 0;
std::string output;
bool dcFlag = false;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-h" || arg == "--help") {
print_help();
return 0;
}
else if (arg == "-d" || arg == "--domain") {
if (i + 1 >= argc) {
std::cout << "Missing argument for " << arg << std::endl;
return 1;
}
domain = argv[++i];
}
else if (arg == "-dc" || arg == "--dc") {
if (i + 1 >= argc) {
std::cout << "Missing argument for " << arg << std::endl;
return 1;
}
dc = argv[++i];
dcFlag = true;
}
else if (arg == "-t" || arg == "--threads") {
if (i + 1 >= argc) {
std::cout << "Missing argument for " << arg << std::endl;
return 1;
}
threads = std::stoi(argv[++i]);
}
else if (arg == "-o" || arg == "--output") {
if (i + 1 >= argc) {
std::cout << "Missing argument for " << arg << std::endl;
return 1;
}
output = argv[++i];
}
else {
std::cout << "Unknown option: " << arg << std::endl;
return 1;
}
}
std::wstring dc_wstr;
if (dcFlag) {
dc_wstr = std::wstring(dc.begin(), dc.end());
}
else
{
std::cout << "Missing required argument: -dc or --dc" << std::endl;
return 1;
}
std::wstring domain_wstr;
if (!domain.empty()) {
domain_wstr = std::wstring(domain.begin(), domain.end());
}
else
{
std::cout << "Missing required argument: -d or --domain" << std::endl;
return 1;
}
std::wstring outputFile_wstr;
if (!output.empty()) {
outputFile_wstr = std::wstring(output.begin(), output.end());
}
else
{
std::cout << "Missing required argument: -o or --output" << std::endl;
return 1;
}
// 设置 console 的 CodePage, 这段代码只影响控制台的中文输出
//SetConsoleOutputCP(CP_UTF8);
// 打开文件, 并设置为追加模式
std::ofstream outputFile_of(outputFile_wstr, std::ios::app);
if (!outputFile_of.is_open()) {
std::cerr << "Failed to open output file." << std::endl;
return false;
}
// 输出 UTF-8 BOM
outputFile_of << "\xef\xbb\xbf";
outputFile_of << "Computer,OS,SID,Logon User\n";
RegConnect(domain_wstr, dc_wstr, outputFile_of, threads);
outputFile_of.close();
std::cout << "Success!" << std::endl;
return 0;
}