-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
212 lines (202 loc) · 9.11 KB
/
main.cc
File metadata and controls
212 lines (202 loc) · 9.11 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
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include "console_utils.h"
#include "pe_parser.h"
#include "dll_analyzer.h"
#include "file_operations.h"
#include "config_loader.h"
int main(int argc, char *argv[])
{
if (argc < 2 || argc > 10)
{
std::cout << "Usage: " << argv[0] << " <executable_path> [options]" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --release [depth] Release mode: recursively analyze dependencies (default depth: 2)" << std::endl;
std::cout << " and copy DLLs to the executable's directory" << std::endl;
std::cout << " --recursive [depth] Recursively analyze all DLL dependencies (default depth: 20)" << std::endl;
std::cout << " --copy <target_dir> Copy dependent DLLs to specified directory" << std::endl;
std::cout << " --copy-exe-dir Copy dependent DLLs to the executable's directory" << std::endl;
std::cout << " --copy-all Copy all DLLs including system core DLLs" << std::endl;
std::cout << " --search-dirs <file> Load additional search directories from file (one directory per line)" << std::endl;
std::cout << " --ignore-dll <file> Load ignore list from file (DLL names, paths, or directories)" << std::endl;
std::cout << "\nExamples:" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --release" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --release 3" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --recursive" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --recursive 10" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --recursive --copy C:\\DestDir" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --copy-exe-dir" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --copy-exe-dir --copy-all" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --search-dirs test_path.txt" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --release --ignore-dll ignore.txt" << std::endl;
std::cout << " " << argv[0] << " C:\\Path\\To\\YourApp.exe --release --search-dirs test_path.txt --ignore-dll ignore.txt" << std::endl;
std::cout << "\nNote: When using --recursive with --copy, all dependencies at all levels will be copied." << std::endl;
std::cout << " By default, system core DLLs will be skipped during copy." << std::endl;
std::cout << " Use --copy-all to include system core DLLs." << std::endl;
std::cout << " The --search-dirs file should contain one directory path per line." << std::endl;
std::cout << " Lines starting with # or ; are treated as comments and ignored." << std::endl;
std::cout << " The --ignore-dll file can contain DLL names, DLL paths, or directory paths." << std::endl;
std::cout << " --release mode is equivalent to --recursive 2 --copy-exe-dir" << std::endl;
return 1;
}
auto exe_path = argv[1];
bool copyMode = false;
bool recursiveMode = false;
bool releaseMode = false;
bool copyAllMode = false;
std::string destDir;
std::string searchDirsFile;
std::string ignoreDllFile;
std::vector<std::string> extraSearchDirs;
// 解析命令行参数
int argIndex = 2;
while (argIndex < argc)
{
if (strcmp(argv[argIndex], "--release") == 0)
{
releaseMode = true;
recursiveMode = true;
copyMode = true;
// 获取可执行文件所在目录作为目标目录
destDir = std::filesystem::path(exe_path).parent_path().string();
argIndex++;
// 检查是否指定了递归深度
if (argIndex < argc && argv[argIndex][0] != '-')
{
// 尝试解析为数字
try
{
int depth = std::stoi(argv[argIndex]);
if (depth <= 0)
{
std::cerr << "Error: Recursion depth must be a positive number, got: " << depth << std::endl;
return 1;
}
SetMaxRecursionDepth(depth);
std::cout << "Info: Release mode: Maximum recursion depth set to: " << depth << std::endl;
argIndex++;
}
catch (const std::exception &e)
{
std::cerr << "Error: Invalid recursion depth value: " << argv[argIndex] << std::endl;
return 1;
}
}
else
{
// 使用默认值 2
SetMaxRecursionDepth(2);
std::cout << "Info: Release mode: Maximum recursion depth set to: 2 (default)" << std::endl;
}
}
else if (strcmp(argv[argIndex], "--recursive") == 0)
{
recursiveMode = true;
argIndex++;
// 检查是否指定了递归深度
if (argIndex < argc && argv[argIndex][0] != '-')
{
// 尝试解析为数字
try
{
int depth = std::stoi(argv[argIndex]);
if (depth <= 0)
{
std::cerr << "Error: Recursion depth must be a positive number, got: " << depth << std::endl;
return 1;
}
SetMaxRecursionDepth(depth);
std::cout << "Info: Maximum recursion depth set to: " << depth << std::endl;
argIndex++;
}
catch (const std::exception &e)
{
std::cerr << "Error: Invalid recursion depth value: " << argv[argIndex] << std::endl;
return 1;
}
}
else
{
// 使用默认值 20
SetMaxRecursionDepth(20);
}
}
else if (strcmp(argv[argIndex], "--copy") == 0 && argIndex + 1 < argc)
{
copyMode = true;
destDir = argv[argIndex + 1];
argIndex += 2;
}
else if (strcmp(argv[argIndex], "--copy-exe-dir") == 0)
{
copyMode = true;
// 获取可执行文件所在目录
destDir = std::filesystem::path(exe_path).parent_path().string();
argIndex++;
}
else if (strcmp(argv[argIndex], "--copy-all") == 0)
{
copyAllMode = true;
argIndex++;
}
else if (strcmp(argv[argIndex], "--search-dirs") == 0 && argIndex + 1 < argc)
{
searchDirsFile = argv[argIndex + 1];
extraSearchDirs = LoadExtraSearchDirectories(searchDirsFile);
if (extraSearchDirs.empty())
{
std::cout << "Warning: No extra search directories loaded from: " << searchDirsFile << std::endl;
}
argIndex += 2;
}
else if (strcmp(argv[argIndex], "--ignore-dll") == 0 && argIndex + 1 < argc)
{
ignoreDllFile = argv[argIndex + 1];
LoadIgnoredDLLs(ignoreDllFile);
argIndex += 2;
}
else
{
std::cerr << "Error: Invalid command line argument: " << argv[argIndex] << std::endl;
return 1;
}
}
// 检测目标文件的架构
ConsoleColors::PrintLn(ConsoleColors::CYAN, "=== Analyzing Target Executable ===");
PEArchitecture targetArch = DetectPEArchitecture(exe_path);
if (targetArch == PEArchitecture::Unknown)
{
std::cerr << "Error: Unable to detect PE architecture of target file" << std::endl;
return 1;
}
std::cout << "Target file architecture: ";
ConsoleColors::PrintLn(ConsoleColors::BRIGHT_GREEN, ArchitectureToString(targetArch));
auto dlls = GetDependentDLLs(exe_path, recursiveMode, extraSearchDirs, targetArch);
std::cout << "\n";
ConsoleColors::PrintLn(ConsoleColors::CYAN, "=== Dependent DLL List ===");
for (const auto &dll : dlls)
{
std::cout << dll << std::endl;
}
std::cout << "Total: ";
ConsoleColors::PrintLn(ConsoleColors::BRIGHT_CYAN, std::to_string(dlls.size()) + " DLL(s)");
// 如果启用了复制模式,则复制DLL文件
if (copyMode)
{
std::cout << "\n";
ConsoleColors::PrintLn(ConsoleColors::CYAN, "=== Starting to Copy DLL Files ===");
if (CopyDependentDLLs(dlls, exe_path, destDir, extraSearchDirs, copyAllMode, targetArch))
{
ConsoleColors::PrintLn(ConsoleColors::GREEN, "✓ All DLL files copied successfully!");
}
else
{
ConsoleColors::PrintLn(ConsoleColors::RED, "✗ Some DLL files failed to copy, please check error messages");
return 1;
}
}
return 0;
}