-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCefClassifier.cs
331 lines (306 loc) · 14.8 KB
/
CefClassifier.cs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Security.AccessControl;
namespace CefDetector.Net
{
/// <summary>
/// CEF类型枚举
/// </summary>
public enum CefType
{
UNKNOWN,
EDGE,
CHROME,
JCEF,
LIBCEF,
ELECTRON,
ELECTRON2,
CEF_SHARP,
NWJS,
MINI_ELECTRON,
MINI_BLINK
}
public static class CefClassifier
{
/// <summary>
/// 储存已经搜索过的目录(不重复搜索)
/// </summary>
public static List<string> searchedDir = new();
/// <summary>
/// 根据文件字节中的特征码确定CEF类型
/// </summary>
public static Dictionary<CefType, byte[]> CefByteFingerprint = new()
{
{
CefType.LIBCEF,
Encoding.UTF8
.GetBytes( "cef_string_utf8_to_utf16" )
},
{
CefType.ELECTRON,
Encoding.UTF8
.GetBytes( "third_party/electron_node" )
},
{
CefType.ELECTRON2,
Encoding.UTF8
.GetBytes( "register_atom_browser_web_contents" )
},
{
CefType.CEF_SHARP,
Encoding.UTF8
.GetBytes( "CefSharp.Internals" )
},
{
CefType.NWJS,
Encoding.UTF8.GetBytes( "url-nwjs" )
},
{
CefType.MINI_ELECTRON,
Encoding.UTF8
.GetBytes( "napi_create_buffer" )
},
{
CefType.MINI_BLINK,
Encoding.UTF8.GetBytes( "miniblink" )
}
};
/// <summary>
/// 根据自定义的函数确认CEF类型
/// </summary>
public static Dictionary<(CefType type, PlatformID platform), Func<DirectoryInfo, (bool isSucc, string file)>>
CefCustomCheck = new()
{
{
( CefType.EDGE, PlatformID.Win32NT ),
( dir ) =>
{
var file = Path.Join( dir.FullName, "msedge.exe" );
return ( File.Exists( file ), new FileInfo( file ).FullName );
}
},
{
( CefType.EDGE, PlatformID.Unix ),
( dir ) =>
{
string file = "";
if ( RuntimeInformation.IsOSPlatform( OSPlatform.OSX ) )
{
//Contents/Frameworks/Microsoft Edge Framework.framework/Versions/107.0.1418.62
//Contents/Frameworks/Microsoft Edge Framework.framework/Versions/107.0.1418.62/Resources
file = Path.Join( dir.Parent!.FullName, "Microsoft Edge Framework" );
}
else
{
file = Path.Join( dir.FullName, "msedge" );
}
return ( File.Exists( file ), new FileInfo( file ).FullName );
}
},
{
( CefType.CHROME, PlatformID.Win32NT ),
( dir ) =>
{
var file1 = Path.Join( dir.FullName, "chrome_pwa_launcher.exe" );
var file2 = Path.Join( dir.FullName, "../chrome.exe" );
var file3 = Path.Join( dir.FullName, "chrome.exe" );
return
( File.Exists( file1 ) &&
( File.Exists( file2 ) || File.Exists( file3 ) ),
File.Exists( file2 )
? new FileInfo( file2 ).FullName
: new FileInfo( file3 ).FullName );
}
},
{
( CefType.CHROME, PlatformID.Unix ),
( dir ) =>
{
//TODO: MacOS可能需要修改
var file = Path.Join( dir.FullName, "chrome" );
return ( File.Exists( file ), new FileInfo( file ).FullName );
}
},
{
( CefType.JCEF, PlatformID.Win32NT ),
( dir ) =>
{
var file = Path.Join( dir.FullName, "../bin/jcef.dll" );
return ( File.Exists( file ), new FileInfo( file ).FullName );
}
},
{
( CefType.JCEF, PlatformID.Unix ),
( dir ) =>
{
//TODO: 需要修改
var file = Path.Join( dir.FullName, "jrt-fs.jar" );
return ( File.Exists( file ), new FileInfo( file ).FullName );
}
},
};
/// <summary>
/// 确认某文件中是否包含某些字节
/// </summary>
/// <param name="file"></param>
/// <param name="search"></param>
/// <returns></returns>
private static bool FileByteContains( byte[] file,
byte[] search )
{
return Parallel.For( 0,
file.Length - search.Length,
( i,
loopState ) =>
{
if ( file[ i ] == search[ 0 ] )
{
byte[] localCache = new byte[ search.Length ];
Array.Copy( file, i, localCache, 0, search.Length );
if ( localCache.SequenceEqual( search ) )
loopState.Stop();
}
} )
.IsCompleted ==
false;
}
/// <summary>
/// 过滤出带有可执行权限的文件或者可执行文件(Windows),减少后续判断次数
/// </summary>
/// <param name="files">文件列表</param>
/// <returns></returns>
public static List<string> FilterExecuteBinaries( List<string> files )
{
if ( Environment.OSVersion.Platform == PlatformID.Unix )
{
List<string> executeBinaries = new();
foreach ( string file in files )
{
FileInfo fi = new FileInfo( file );
if ( fi.UnixFileMode.HasFlag( UnixFileMode.UserExecute |
UnixFileMode.GroupExecute |
UnixFileMode.OtherExecute ) )
{
executeBinaries.Add( file );
}
}
return executeBinaries;
}
else if ( RuntimeInformation.IsOSPlatform( OSPlatform.Windows ) )
{
return files.Where( i => i.EndsWith( ".exe" ) ).ToList();
}
else
{
return files.ToList();
}
}
/// <summary>
/// 根据文件夹结构特征直接推断出程序
/// </summary>
/// <param name="dirPath">目录</param>
/// <returns></returns>
public static Dictionary<string, CefType> CheckDirCefType( string dirPath )
{
if ( !Directory.Exists( dirPath ) ) return new();
DirectoryInfo dir = new DirectoryInfo( dirPath );
foreach ( var (cefType, checkFunc) in CefCustomCheck )
{
//筛选操作系统
if ( cefType.platform != Environment.OSVersion.Platform ) continue;
var (isSucc, file) = checkFunc( dir );
if ( isSucc )
{
return new()
{
{file, cefType.type}
};
}
}
return new();
}
/// <summary>
/// 读取二进制文件字节,根据特征码判断CEF类型
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static CefType CheckBinaryCefType( string filePath )
{
if ( !File.Exists( filePath ) ) return CefType.UNKNOWN;
try
{
byte[] fileContent = File.ReadAllBytes( filePath );
CefType result = CefType.UNKNOWN;
//浏览器文件字节检查
foreach ( var (type, bytes) in CefByteFingerprint )
{
if ( FileByteContains( fileContent, bytes ) )
{
return type;
}
}
return result;
}
catch { }
return CefType.UNKNOWN;
}
/// <summary>
/// 遍历文件所在目录(甚至上几级目录)来判断目录中CEF的类型
/// </summary>
/// <param name="cefPath">特征文件路径(pak文件等)</param>
/// <returns></returns>
public static Dictionary<string, CefType> SearchDir( string cefPath )
{
if ( !File.Exists( cefPath ) ) return new();
Dictionary<string, CefType> cefBinaries = new();
var dir = new FileInfo( cefPath ).Directory!;
//不搜索之前搜索过的目录
if ( searchedDir.Contains( dir.FullName ) )
{
return new();
}
else
{
searchedDir.Add( dir.FullName );
}
//先根据目录检查
var dirCheckResult = CheckDirCefType( dir.FullName );
if ( dirCheckResult.Any() )
{
return dirCheckResult;
}
//再搜索目录内所有文件,检查字节
var currDir = new DirectoryInfo( dir.FullName );
var files = new List<string>();
do
{
files = FilterExecuteBinaries( currDir.GetFiles( "*", SearchOption.TopDirectoryOnly )
.Select( i => i.FullName )
.ToList() );
//如果没搜到可执行文件,则向上一层目录搜索
currDir = currDir.Parent;
if ( currDir == null )
{
//已经到根目录了,则搜不到了
return new();
}
} while ( !files.Any() );
foreach ( var file in files )
{
if ( !cefBinaries.ContainsKey( file ) )
{
var result = CheckBinaryCefType( file );
if ( result != CefType.UNKNOWN )
{
cefBinaries.Add( file, result );
}
}
}
return cefBinaries;
}
}
}