isDriver determines if the PE is a Windows driver. This method is inherited from the saferwall parser with the imports defensive check removed as some driver samples may not contain an import directory, but section names may reveal the PE is a kernel driver.
()
| 409 | // driver samples may not contain an import directory, but section names may |
| 410 | // reveal the PE is a kernel driver. |
| 411 | func (pe *PE) isDriver() bool { |
| 412 | // Prevent false positives such as ntdll.dll |
| 413 | // because it has the PAGE section which is |
| 414 | // driver-typical |
| 415 | if pe.IsDLL { |
| 416 | return false |
| 417 | } |
| 418 | // DIRECTORY_ENTRY_IMPORT may exist, although it may be empty. |
| 419 | // If it imports from "ntoskrnl.exe" or other kernel components it should |
| 420 | // be a driver. |
| 421 | systemDLLs := []string{"ntoskrnl.exe", "hal.dll", "ndis.sys", |
| 422 | "bootvid.dll", "kdcom.dll"} |
| 423 | for _, imp := range pe.Imports { |
| 424 | for _, dll := range systemDLLs { |
| 425 | if strings.ToLower(imp) == dll { |
| 426 | return true |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // If still we couldn't tell, check common driver section with combination |
| 432 | // of IMAGE_SUBSYSTEM_NATIVE or IMAGE_SUBSYSTEM_NATIVE_WINDOWS. |
| 433 | subsystem := peparser.ImageOptionalHeaderSubsystemType(0) |
| 434 | oh32 := peparser.ImageOptionalHeader32{} |
| 435 | oh64 := peparser.ImageOptionalHeader64{} |
| 436 | switch pe.Is64 { |
| 437 | case true: |
| 438 | oh64 = pe.ntHeader.OptionalHeader.(peparser.ImageOptionalHeader64) |
| 439 | subsystem = oh64.Subsystem |
| 440 | case false: |
| 441 | oh32 = pe.ntHeader.OptionalHeader.(peparser.ImageOptionalHeader32) |
| 442 | subsystem = oh32.Subsystem |
| 443 | } |
| 444 | commonDriverSectionNames := []string{"page", "paged", "nonpage", "init"} |
| 445 | for _, section := range pe.Sections { |
| 446 | s := strings.ToLower(section.Name) |
| 447 | for _, driverSection := range commonDriverSectionNames { |
| 448 | if s == driverSection && |
| 449 | (subsystem&peparser.ImageSubsystemNativeWindows != 0 || |
| 450 | subsystem&peparser.ImageSubsystemNative != 0) { |
| 451 | return true |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return false |
| 456 | } |