-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-RealFileType.ps1
More file actions
188 lines (168 loc) · 9.27 KB
/
Copy pathGet-RealFileType.ps1
File metadata and controls
188 lines (168 loc) · 9.27 KB
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
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Get-RealFileType {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('FullName')]
[string]$Path
)
process {
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) {
Write-Warning "File not found or is a directory: $Path"
return
}
$resolvedPath = (Resolve-Path -LiteralPath $Path).Path
$currentExt = [System.IO.Path]::GetExtension($resolvedPath)
# Read the first 32 bytes for magic number detection
try {
$stream = [System.IO.File]::OpenRead($resolvedPath)
$buffer = New-Object byte[] 32
$bytesRead = $stream.Read($buffer, 0, 32)
$stream.Close()
}
catch {
Write-Warning "Could not read file: $resolvedPath ($($_.Exception.Message))"
return
}
if ($bytesRead -eq 0) {
return [PSCustomObject]@{
File = $resolvedPath
CurrentExt = if ($currentExt) { $currentExt } else { "(none)" }
DetectedExt = "(empty)"
Category = "Empty"
Description = "Empty File (0 bytes)"
HexSignature = ""
}
}
$hex = ($buffer[0..($bytesRead - 1)] | ForEach-Object { $_.ToString("X2") }) -join ""
$cat = "Unknown"
$ext = "Unknown"
$desc = "Unrecognized Signature"
# ----------------- 1. MODERN ZIP / OPENXML DEEP INSPECTION -----------------
if ($hex -match "^504B0304") {
$cat = "Archive"
$ext = ".zip"
$desc = "Standard ZIP Archive"
try {
$zipStream = [System.IO.File]::OpenRead($resolvedPath)
$archive = New-Object System.IO.Compression.ZipArchive($zipStream, [System.IO.Compression.ZipArchiveMode]::Read)
$entries = $archive.Entries | ForEach-Object { $_.FullName }
$archive.Dispose()
$zipStream.Close()
if ($entries -match "^word/") {
$ext = ".docx"
$cat = "Document"
$desc = "Microsoft Word Document (OpenXML)"
}
elseif ($entries -match "^xl/") {
$ext = ".xlsx"
$cat = "Document"
$desc = "Microsoft Excel Spreadsheet (OpenXML)"
}
elseif ($entries -match "^ppt/") {
$ext = ".pptx"
$cat = "Document"
$desc = "Microsoft PowerPoint Presentation (OpenXML)"
}
elseif ($entries -match "AndroidManifest\.xml") {
$ext = ".apk"
$cat = "Package"
$desc = "Android Application Package"
}
elseif ($entries -match "^META-INF/MANIFEST\.MF") {
$ext = ".jar"
$cat = "Package"
$desc = "Java Archive"
}
elseif ($entries -match "mimetype") {
$ext = ".epub / .odf"
$cat = "Document"
$desc = "EPUB Book or OpenDocument File"
}
}
catch {
$desc = "ZIP Container (Corrupted or Encrypted)"
}
}
# ----------------- 2. LEGACY OLE COMPOUND FILE (DOC/XLS/PPT) DEEP INSPECTION -----------------
elseif ($hex -match "^D0CF11E0A1B11AE1") {
$cat = "Document"
$ext = ".doc / .xls / .ppt"
$desc = "Legacy MS Office Binary Document (OLE CFB)"
try {
$oleStream = [System.IO.File]::OpenRead($resolvedPath)
$readLength = [Math]::Min(16384, [int]$oleStream.Length)
$oleBytes = New-Object byte[] $readLength
[void]$oleStream.Read($oleBytes, 0, $readLength)
$oleStream.Close()
# Internal OLE directory names are encoded in UTF-16LE
$oleText = [System.Text.Encoding]::Unicode.GetString($oleBytes)
if ($oleText -match "WordDocument") {
$ext = ".doc"
$desc = "Legacy Microsoft Word 97-2003 Document"
}
elseif ($oleText -match "Workbook|Book") {
$ext = ".xls"
$desc = "Legacy Microsoft Excel 97-2003 Spreadsheet"
}
elseif ($oleText -match "PowerPoint Document|Current User") {
$ext = ".ppt"
$desc = "Legacy Microsoft PowerPoint 97-2003 Presentation"
}
elseif ($oleText -match "__substg1.0_") {
$ext = ".msg"
$desc = "Outlook Message Document"
}
elseif ($oleText -match "VisioDocument") {
$ext = ".vsd"
$desc = "Legacy Microsoft Visio Drawing"
}
}
catch {
# Fall back to generic OLE
}
}
# ----------------- 3. IMAGES -----------------
elseif ($hex -match "^89504E470D0A1A0A") { $ext = ".png"; $cat = "Image"; $desc = "PNG Image" }
elseif ($hex -match "^FFD8FF") { $ext = ".jpg"; $cat = "Image"; $desc = "JPEG Image" }
elseif ($hex -match "^47494638") { $ext = ".gif"; $cat = "Image"; $desc = "GIF Image" }
elseif ($hex -match "^424D") { $ext = ".bmp"; $cat = "Image"; $desc = "Windows Bitmap" }
elseif ($hex -match "^52494646.{8}57454250") { $ext = ".webp"; $cat = "Image"; $desc = "WebP Image" }
elseif ($hex -match "^49492A00|^4D4D002A") { $ext = ".tiff"; $cat = "Image"; $desc = "TIFF Image" }
elseif ($hex -match "^00000100") { $ext = ".ico"; $cat = "Image"; $desc = "Windows Icon" }
elseif ($hex -match "^38425053") { $ext = ".psd"; $cat = "Image"; $desc = "Photoshop Document" }
# ----------------- 4. AUDIO & VIDEO -----------------
elseif ($hex -match "^1A45DFA3") { $ext = ".mkv / .webm"; $cat = "Video"; $desc = "Matroska / WebM Container" }
elseif ($hex -match "^.{8}66747970") { $ext = ".mp4"; $cat = "Video"; $desc = "MP4 / QuickTime Video" }
elseif ($hex -match "^52494646.{8}41564920") { $ext = ".avi"; $cat = "Video"; $desc = "AVI Video" }
elseif ($hex -match "^52494646.{8}57415645") { $ext = ".wav"; $cat = "Audio"; $desc = "WAV Audio" }
elseif ($hex -match "^494433|^FFF[B|3|2]") { $ext = ".mp3"; $cat = "Audio"; $desc = "MP3 Audio" }
elseif ($hex -match "^664C6143") { $ext = ".flac"; $cat = "Audio"; $desc = "FLAC Audio" }
elseif ($hex -match "^4F676753") { $ext = ".ogg"; $cat = "Audio/Video"; $desc = "Ogg Container" }
elseif ($hex -match "^3026B2752266CF11") { $ext = ".wmv / .wma"; $cat = "Audio/Video"; $desc = "Windows Media (ASF)" }
# ----------------- 5. OTHER DOCUMENTS & DATA -----------------
elseif ($hex -match "^25504446") { $ext = ".pdf"; $cat = "Document"; $desc = "PDF Document" }
elseif ($hex -match "^7B5C727466") { $ext = ".rtf"; $cat = "Document"; $desc = "Rich Text Format" }
elseif ($hex -match "^3C3F786D6C|^3C737667") { $ext = ".xml / .svg"; $cat = "Data"; $desc = "XML / SVG Document" }
elseif ($hex -match "^53514C69746520666F726D6174203300") { $ext = ".sqlite / .db"; $cat = "Database"; $desc = "SQLite 3 Database" }
# ----------------- 6. OTHER ARCHIVES -----------------
elseif ($hex -match "^377ABCAF271C") { $ext = ".7z"; $cat = "Archive"; $desc = "7-Zip Archive" }
elseif ($hex -match "^526172211A07") { $ext = ".rar"; $cat = "Archive"; $desc = "RAR Archive" }
elseif ($hex -match "^1F8B") { $ext = ".gz"; $cat = "Archive"; $desc = "GZIP Compressed Archive" }
elseif ($hex -match "^425A68") { $ext = ".bz2"; $cat = "Archive"; $desc = "BZIP2 Compressed Archive" }
elseif ($hex -match "^FD377A585A00") { $ext = ".xz"; $cat = "Archive"; $desc = "XZ Archive" }
# ----------------- 7. EXECUTABLES -----------------
elseif ($hex -match "^4D5A") { $ext = ".exe / .dll"; $cat = "Executable"; $desc = "DOS/Windows Executable or DLL" }
elseif ($hex -match "^7F454C46") { $ext = ".elf"; $cat = "Executable"; $desc = "Linux ELF Binary" }
elseif ($hex -match "^CAFEBABE") { $ext = ".class / Mach-O"; $cat = "Executable"; $desc = "Java Class / Mach-O Binary" }
# Output object
[PSCustomObject]@{
File = $resolvedPath
CurrentExt = if ($currentExt) { $currentExt } else { "(none)" }
DetectedExt = $ext
Category = $cat
Description = $desc
HexSignature = ($buffer[0..([Math]::Min(7, $bytesRead - 1))] | ForEach-Object { $_.ToString("X2") }) -join " "
}
}
}