Djentacodic

Get-FileBitness.ps1

Apr 12th, 2024 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 4.03 KB | Source Code | 0 0
  1. #Get-FileBitness.ps1
  2. #Written for PowerShell 5.1
  3. [CmdletBinding()]
  4. Param(
  5.     [Parameter(Mandatory)]
  6.     [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
  7.     [String]$FilePath
  8. )
  9.  
  10. #region constants
  11. New-Variable    -Name IMAGE_NT_HEADER           -Value ([uint32]"0x00004550")   -Option Constant
  12. New-Variable    -Name IMAGE_DOS_HEADER          -Value ([uint16]"0x5A4D")       -Option Constant
  13. New-Variable    -Name IMAGE_FILE_MACHINE_I386   -Value ([uint16]"0x14C")        -Option Constant
  14. New-Variable    -Name IMAGE_FILE_MACHINE_AMD64  -Value ([uint16]"0x8664")       -Option Constant
  15. #end region
  16.  
  17. #region functions
  18. Function ConvertTo-DWord {
  19.     [CmdletBinding(DefaultParameterSetName = 'ByName')]
  20.     Param(
  21.         [Parameter(Mandatory, ParameterSetName = 'ByName', Position = 0)]
  22.         [ValidateScript({$_ -ge 0 -and $_ -lt 256})]
  23.         [byte[]]$ByteArray,
  24.         [Parameter(Mandatory, ParameterSetName = 'ByPipe', ValueFromPipeline)]
  25.         [ValidateScript({$_ -ge 0 -and $_ -lt 256})]
  26.         [byte[]]$InputObject,
  27.         [Parameter(ParameterSetName = 'ByName')]
  28.         [Parameter(ParameterSetName = 'ByPipe')]
  29.         [switch]$BigEndian
  30.     )
  31.  
  32.     begin {}
  33.     process {
  34.         If ($PSCmdlet.ParameterSetName -eq 'ByPipe') {
  35.             [byte[]]$PipeBytes += $InputObject
  36.         }
  37.     }
  38.     end {
  39.         Switch ($PSCmdlet.ParameterSetName) {
  40.             'ByName' {
  41.                 [byte[]]$Bytes = $ByteArray
  42.                 break
  43.             }
  44.             'ByPipe' {
  45.                 [byte[]]$Bytes = $PipeBytes
  46.                 break
  47.             }
  48.         }
  49.  
  50.         Switch ($Bytes | Measure-Object | Select-Object -ExpandProperty Count) {
  51.             {$_ -lt 4} {
  52.                 Write-Error "Too few array elements"
  53.                 Return $null
  54.             }
  55.             {$_ -gt 4} {
  56.                 Write-Error "Too many array elements"
  57.                 Return $null
  58.             }
  59.         }
  60.  
  61.         If ($BigEndian) {
  62.             [byte[]]$Bytes = $Bytes[3..0]
  63.         }
  64.  
  65.         [uint32]$Result = 0
  66.         for ($i = 0; $i -lt 4; $i++) {
  67.             $Result += $Bytes[$i] * [math]::Pow(256,$i)
  68.         }
  69.         Return $Result
  70.     }
  71. }
  72. Function ConvertTo-Word {
  73.     [CmdletBinding(DefaultParameterSetName = 'ByName')]
  74.     Param(
  75.         [Parameter(Mandatory, ParameterSetName = 'ByName', Position = 0)]
  76.         [ValidateScript({$_ -ge 0 -and $_ -lt 256})]
  77.         [byte[]]$ByteArray,
  78.         [Parameter(Mandatory, ParameterSetName = 'ByPipe', ValueFromPipeline)]
  79.         [ValidateScript({$_ -ge 0 -and $_ -lt 256})]
  80.         [byte[]]$InputObject,
  81.         [Parameter(ParameterSetName = 'ByName')]
  82.         [Parameter(ParameterSetName = 'ByPipe')]
  83.         [switch]$BigEndian
  84.     )
  85.  
  86.     begin {}
  87.     process {
  88.         If ($PSCmdlet.ParameterSetName -eq 'ByPipe') {
  89.             [byte[]]$PipeBytes += $InputObject
  90.         }
  91.     }
  92.     end {
  93.         Switch ($PSCmdlet.ParameterSetName) {
  94.             'ByName' {
  95.                 [byte[]]$Bytes = $ByteArray
  96.                 break
  97.             }
  98.             'ByPipe' {
  99.                 [byte[]]$Bytes = $PipeBytes
  100.                 break
  101.             }
  102.         }
  103.  
  104.         Switch ($Bytes | Measure-Object | Select-Object -ExpandProperty Count) {
  105.             {$_ -lt 2} {
  106.                 Write-Error "Too few array elements"
  107.                 Return $null
  108.             }
  109.             {$_ -gt 2} {
  110.                 Write-Error "Too many array elements"
  111.                 Return $null
  112.             }
  113.         }
  114.  
  115.         If ($BigEndian) {
  116.             [byte[]]$Bytes = $Bytes[1..0]
  117.         }
  118.  
  119.         [uint16]$Result = 0
  120.         For ($i = 0; $i -lt 2; $i++) {
  121.             $Result += $Bytes[$i] * [math]::Pow(256,$i)
  122.         }
  123.         Return $Result
  124.     }
  125. }
  126. #end region
  127.  
  128. #region main
  129. [string]$FullPath = Resolve-Path -Path $FilePath | Select-Object -ExpandProperty Path
  130. [byte[]]$bytes = Get-Content -Path $FullPath -Encoding Byte -TotalCount 512
  131.  
  132. If ((ConvertTo-Word $bytes[0,1]) -eq $IMAGE_DOS_HEADER) {
  133.    
  134.     [uint16]$PEHeaderOffset = $bytes[60,61] | ConvertTo-Word
  135.    
  136.     If ((ConvertTo-DWord $bytes[$PEHeaderOffset..($PEHeaderOffset + 3)]) -eq $IMAGE_NT_HEADER) {
  137.         Class Arch {
  138.             [string]$Arch
  139.         }
  140.  
  141.         Switch ($bytes[($PEHeaderOffset + 4),($PEHeaderOffset + 5)] | ConvertTo-Word) {
  142.             $IMAGE_FILE_MACHINE_AMD64 {
  143.                 & {$IMAGE_ARCH = New-Object Arch; [Arch]$IMAGE_ARCH = @{ARCH = 'AMD64'}; Write-Output $IMAGE_ARCH}
  144.                 Break
  145.             }
  146.             $IMAGE_FILE_MACHINE_I386 {
  147.                 & {$IMAGE_ARCH = New-Object Arch; [Arch]$IMAGE_ARCH = @{ARCH = 'I386'}; Write-Output $IMAGE_ARCH}
  148.                 Break
  149.             }
  150.             Default {
  151.                 Write-Error "Unsupported machine architecture"
  152.             }
  153.         }
  154.     }
  155.     Else {
  156.         Write-Error "Not a valid Win32 image"
  157.     }
  158. }
  159. Else {
  160.     Write-Error "Not a valid executable image"
  161. }
  162. #end region
Add Comment
Please, Sign In to add comment