Advertisement
Guest User

Untitled

a guest
Jan 17th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $combined_file   = "C:\test\3.jpg"
  2. $output_location = "C:\test\"
  3.  
  4. #Magic string to search for, example is taken from
  5. #https://en.wikipedia.org/wiki/List_of_file_signatures as jpg example
  6. $magic_sequence_of_bytes = "FF D8 FF E0 00 10 4A 46 49 46 00 01"  
  7. $ext = "jpg"
  8.  
  9. $byte_array_of_combined_file = [System.IO.File]::ReadAllBytes($combined_file)
  10. $byte_start_offset = @()
  11.  
  12. $ar = $magic_sequence_of_bytes.Split(" ")
  13. $findindex = 0;
  14.  
  15. for ($i = 0 ; $i -lt $byte_array_of_combined_file.Length; $i++)
  16. {
  17.    $v = '{0:X2}' -f $byte_array_of_combined_file[$i]
  18.    if ($v -eq $ar[$findindex])
  19.    {
  20.       if ($findindex -eq $ar.Count-1)
  21.       {
  22.          Write-Host "Found string magic!" -ForegroundColor Green
  23.          $pos = $i - ($ar.Count-1)
  24.          write-host "$magic_sequence_of_bytes starts at offset" $pos
  25.          $byte_start_offset +=$pos
  26.       }
  27.       $findindex=$findindex+1
  28.    }
  29.    else
  30.    {
  31.       $findindex = 0
  32.    }
  33. }
  34.  
  35. for ($f=0; $f -lt $byte_start_offset.Count; $f++)
  36. {
  37.    $filenameout = "$output_location$f-recreated.$ext"
  38.  
  39.    if ($f -eq $byte_start_offset.Count -1)
  40.    {
  41.        $s = $byte_start_offset[$f]
  42.        $e = $byte_array_of_combined_file.Length  
  43.    }
  44.    else
  45.    {
  46.        $s =  $byte_start_offset[$f]
  47.        $e = ($byte_start_offset[$f+1] -1)
  48.    }
  49.    write-host  "Creating file:" $filenameout
  50.    [System.IO.File]::WriteAllBytes($filenameout, $byte_array_of_combined_file[$s..$e])
  51. }
  52.  
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement