11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33using AttackSurfaceAnalyzer . Objects ;
4+ using AttackSurfaceAnalyzer . Types ;
45using AttackSurfaceAnalyzer . Utils ;
56using Mono . Unix ;
67using Serilog ;
@@ -179,124 +180,52 @@ e is ArgumentException
179180 return signature ;
180181 }
181182 }
182- catch ( Exception e ) {
183+ catch ( Exception e )
184+ {
183185 Log . Verbose ( "Failed to get Mac CodeSign information for {0} ({1}:{2})" , Path , e . GetType ( ) , e . Message ) ;
184186 }
185187 return null ;
186188 }
187189
188- public static bool ? IsExecutable ( string ? Path , ulong ? Size )
190+ public static EXECUTABLE_TYPE GetExecutableType ( string Path )
189191 {
190- if ( Path is null || Size is null ) { return null ; }
191- if ( Size < 4 ) { return false ; }
192-
193- // Shortcut to help with system files we can't read directly
194- if ( Path . EndsWith ( ".dll" ) || Path . EndsWith ( ".exe" ) )
195- {
196- return true ;
197- }
198-
199- byte [ ] fourBytes = new byte [ 4 ] ;
200- try
201- {
202- using ( var fileStream = File . OpenRead ( Path ) )
203- {
204- fileStream . Read ( fourBytes , 0 , 4 ) ;
205- }
206- }
207- catch ( Exception e ) when (
208- e is ArgumentException
209- || e is ArgumentNullException
210- || e is PathTooLongException
211- || e is DirectoryNotFoundException
212- || e is IOException
213- || e is UnauthorizedAccessException
214- || e is ArgumentOutOfRangeException
215- || e is FileNotFoundException
216- || e is NotSupportedException
217- || e is ObjectDisposedException )
218- {
219- Log . Verbose ( "Couldn't chomp 4 bytes of {0} ({1}:{2})" , Path , e . GetType ( ) . ToString ( ) , e . Message ) ;
220- return false ;
221- }
222-
223- return fourBytes . SequenceEqual ( ElfMagicNumber ) || fourBytes . SequenceEqual ( JavaMagicNumber ) || MacMagicNumbers . Contains ( fourBytes ) || fourBytes [ 0 ..2 ] . SequenceEqual ( WindowsMagicNumber ) ;
192+ using var fs = new FileStream ( Path , FileMode . Open ) ;
193+ return GetExecutableType ( Path , fs ) ;
224194 }
225195
226- public static bool IsMacExecutable ( string ? Path , ulong ? Size )
196+ public static EXECUTABLE_TYPE GetExecutableType ( string ? Path , Stream input )
227197 {
228- if ( Path is null ) { return false ; }
229- if ( Size < 4 ) { return false ; }
198+ if ( input == null ) { return EXECUTABLE_TYPE . UNKNOWN ; }
199+ if ( input . Length < 4 ) { return EXECUTABLE_TYPE . NONE ; }
230200
231- // Shortcut to help with system files we can't read directly
232- if ( Path . EndsWith ( ".app" ) )
233- {
234- return true ;
235- }
201+ var fourBytes = new byte [ 4 ] ;
202+ var initialPosition = input . Position ;
236203
237- byte [ ] fourBytes = new byte [ 4 ] ;
238204 try
239205 {
240- using ( var fileStream = File . Open ( Path , FileMode . Open ) )
241- {
242- fileStream . Read ( fourBytes , 0 , 4 ) ;
243- }
206+ input . Read ( fourBytes ) ;
207+ input . Position = initialPosition ;
244208 }
245- catch ( Exception e ) when (
246- e is ArgumentException
247- || e is ArgumentNullException
248- || e is PathTooLongException
249- || e is DirectoryNotFoundException
250- || e is IOException
251- || e is UnauthorizedAccessException
252- || e is ArgumentOutOfRangeException
253- || e is FileNotFoundException
254- || e is NotSupportedException
255- || e is ObjectDisposedException )
209+ catch ( Exception e )
256210 {
257211 Log . Verbose ( "Couldn't chomp 4 bytes of {0} ({1}:{2})" , Path , e . GetType ( ) . ToString ( ) , e . Message ) ;
258- return false ;
212+ return EXECUTABLE_TYPE . UNKNOWN ;
259213 }
260214
261- return MacMagicNumbers . Contains ( fourBytes ) ;
262- }
263-
264- public static bool IsWindowsExecutable ( string ? Path , ulong ? Size )
265- {
266- if ( Path is null ) { return false ; }
267- if ( Size < 4 ) { return false ; }
268-
269- // Shortcut to help with system files we can't read directly
270- if ( Path . EndsWith ( ".dll" ) || Path . EndsWith ( ".exe" ) )
215+ switch ( fourBytes )
271216 {
272- return true ;
273- }
217+ case var span when span . SequenceEqual ( ElfMagicNumber ) :
218+ return EXECUTABLE_TYPE . LINUX ;
219+ case var span when span . SequenceEqual ( JavaMagicNumber ) :
220+ return EXECUTABLE_TYPE . JAVA ;
221+ case var span when MacMagicNumbers . Contains ( span ) :
222+ return EXECUTABLE_TYPE . MACOS ;
223+ case var span when span [ 0 ..2 ] . SequenceEqual ( WindowsMagicNumber ) :
224+ return EXECUTABLE_TYPE . WINDOWS ;
225+ default :
226+ return EXECUTABLE_TYPE . NONE ;
274227
275- byte [ ] fourBytes = new byte [ 4 ] ;
276- try
277- {
278- using ( var fileStream = File . OpenRead ( Path ) )
279- {
280- fileStream . Read ( fourBytes , 0 , 4 ) ;
281- }
282- }
283- catch ( Exception e ) when (
284- e is ArgumentException
285- || e is ArgumentNullException
286- || e is PathTooLongException
287- || e is DirectoryNotFoundException
288- || e is IOException
289- || e is UnauthorizedAccessException
290- || e is ArgumentOutOfRangeException
291- || e is FileNotFoundException
292- || e is NotSupportedException
293- || e is ObjectDisposedException )
294- {
295- Log . Verbose ( "Couldn't chomp 4 bytes of {0} ({1}:{2})" , Path , e . GetType ( ) . ToString ( ) , e . Message ) ;
296- return false ;
297228 }
298-
299- return fourBytes [ 0 ..2 ] . SequenceEqual ( WindowsMagicNumber ) ;
300229 }
301230
302231 public static string GetFileHash ( string path )
@@ -313,35 +242,25 @@ public static string GetFileHash(string path)
313242
314243 public static string ? GetFileHash ( FileSystemInfo fileInfo )
315244 {
245+ string ? hashValue = null ;
246+
316247 if ( fileInfo != null )
317248 {
318249 Log . Debug ( "{0} {1}" , Strings . Get ( "FileHash" ) , fileInfo . FullName ) ;
319250
320- string ? hashValue = null ;
321251 try
322252 {
323253 using ( var stream = File . OpenRead ( fileInfo . FullName ) )
324254 {
325255 hashValue = CryptoHelpers . CreateHash ( stream ) ;
326256 }
327257 }
328- catch ( Exception e ) when (
329- e is ArgumentNullException
330- || e is ArgumentException
331- || e is NotSupportedException
332- || e is FileNotFoundException
333- || e is IOException
334- || e is System . Security . SecurityException
335- || e is DirectoryNotFoundException
336- || e is UnauthorizedAccessException
337- || e is PathTooLongException
338- || e is ArgumentOutOfRangeException )
258+ catch ( Exception e )
339259 {
340260 Log . Verbose ( "{0}: {1} {2}" , Strings . Get ( "Err_UnableToHash" ) , fileInfo . FullName , e . GetType ( ) . ToString ( ) ) ;
341261 }
342- return hashValue ;
343262 }
344- return null ;
263+ return hashValue ;
345264 }
346265 }
347266}
0 commit comments