-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDvrVxWorksDetector.cs
More file actions
208 lines (188 loc) · 7.15 KB
/
DvrVxWorksDetector.cs
File metadata and controls
208 lines (188 loc) · 7.15 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;
namespace ProcessorEmulator.Tools.FileSystems
{
public class DvrVxWorksDetector
{
private struct VxWorksVersion
{
public string Version;
public long Signature;
public long BootSignature;
public string DeviceType; // e.g., "Hopper", "Joey", "HR54", etc.
public VxWorksVersion(string version, long signature, long bootSignature, string deviceType)
{
Version = version;
Signature = signature;
BootSignature = bootSignature;
DeviceType = deviceType;
}
}
private readonly Dictionary<long, VxWorksVersion> knownVersions = new()
{
{ 0x27051956L, new VxWorksVersion("6.9", 0x27051956L, 0x0FF0AD12L, "Hopper3") },
{ 0x27051957L, new VxWorksVersion("6.9.4", 0x27051957L, 0x0FF0AD13L, "Hopper") },
{ 0x27051960L, new VxWorksVersion("6.7", 0x27051960L, 0x0FF0AD16L, "Generic") }
};
public struct EncryptionInfo
{
public string Algorithm;
private int keySize;
public int KeySize
{
get { return keySize; }
set { keySize = value; }
}
public byte[] KeyMaterial;
public byte[] IV;
}
public (string version, string deviceType, EncryptionInfo encInfo) DetectVersion(byte[] firmware)
{
// Check for VxWorks signatures at known offsets
long[] commonOffsets = { 0x0, 0x200, 0x400, 0x800, 0x1000 };
foreach (long offset in commonOffsets)
{
if (offset + 8 > firmware.Length) continue;
long signature = BitConverter.ToInt64(firmware, (int)offset);
if (knownVersions.TryGetValue(signature, out VxWorksVersion version))
{
var encInfo = DetectEncryption(firmware, version);
return (version.Version, version.DeviceType, encInfo);
}
}
// Secondary detection method - search for version strings
string versionStr = SearchVersionString(firmware);
if (!string.IsNullOrEmpty(versionStr))
{
var deviceType = DetermineDeviceType(firmware);
var encInfo = DetectEncryptionFallback(firmware);
return (versionStr, deviceType, encInfo);
}
throw new Exception("Unknown VxWorks version or not a VxWorks firmware");
}
//Stupid Sexy Flanders
private static string SearchVersionString(byte[] firmware)
{
// Common VxWorks version string patterns
string[] patterns = {
"VxWorks 6",
"VXWORKS_",
"Wind River VxWorks"
};
foreach (string pattern in patterns)
{
byte[] searchPattern = Encoding.ASCII.GetBytes(pattern);
int index = SearchBytes(firmware, searchPattern);
if (index >= 0)
{
// Extract version information around the found pattern
return ExtractVersionInfo(firmware, index);
}
}
return null;
}
private static int SearchBytes(byte[] haystack, byte[] needle)
{
for (int i = 0; i <= haystack.Length - needle.Length; i++)
{
bool found = true;
for (int j = 0; j < needle.Length; j++)
{
if (haystack[i + j] != needle[j])
{
found = false;
break;
}
}
if (found) return i;
}
return -1;
}
private static string ExtractVersionInfo(byte[] firmware, int index)
{
// Read up to 32 bytes after the pattern for version info
int maxLength = Math.Min(32, firmware.Length - index);
byte[] versionBytes = new byte[maxLength];
Array.Copy(firmware, index, versionBytes, 0, maxLength);
string version = Encoding.ASCII.GetString(versionBytes);
// Clean up the version string
int nullChar = version.IndexOf('\0');
if (nullChar >= 0)
{
version = version.Substring(0, nullChar);
}
return version.Trim();
}
private static string DetermineDeviceType(byte[] firmware)
{
// Known device identification strings
Dictionary<string, string> devicePatterns = new()
{
{ "HOPPER", "Hopper" },
{ "JOEY", "Joey" },
{ "HR54", "HR54" },
{ "HR44", "HR44" },
{ "GENIE", "Genie" }
};
foreach (var pattern in devicePatterns)
{
if (SearchBytes(firmware, Encoding.ASCII.GetBytes(pattern.Key)) >= 0)
return pattern.Value;
}
return "Unknown DVR";
}
private EncryptionInfo DetectEncryption(byte[] firmware, VxWorksVersion version)
{
// Look for known encryption signatures based on device type
return version.DeviceType switch
{
"Hopper3" => DetectHopperEncryption(firmware),
"HR54" or "HR44" => DetectGenieDvrEncryption(firmware),
_ => DetectEncryptionFallback(firmware),
};
}
private static EncryptionInfo DetectHopperEncryption(byte[] firmware)
{
// Hopper-specific encryption detection
return new EncryptionInfo
{
Algorithm = "AES-256",
KeySize = 256,
KeyMaterial = ExtractKeyMaterial(firmware, "HOPPER"),
IV = new byte[16] // IV is typically derived from device-specific data
};
}
private static EncryptionInfo DetectGenieDvrEncryption(byte[] firmware)
{
// Genie DVR-specific encryption detection
return new EncryptionInfo
{
Algorithm = "AES-128",
KeySize = 128,
KeyMaterial = ExtractKeyMaterial(firmware, "GENIE"),
IV = new byte[16]
};
}
private static EncryptionInfo DetectEncryptionFallback(byte[] firmware)
{
// Generic encryption detection
return new EncryptionInfo
{
Algorithm = "Unknown",
KeySize = 0,
KeyMaterial = new byte[32],
IV = new byte[16]
};
}
private static byte[] ExtractKeyMaterial(byte[] firmware, string deviceType)
{
// Implementation depends on specific device type
byte[] keyMaterial = new byte[32];
// Key material extraction logic here
return keyMaterial;
}
}
}