-
Notifications
You must be signed in to change notification settings - Fork 1
/
SteamClientData.cs
229 lines (182 loc) · 7.18 KB
/
SteamClientData.cs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Win32;
using Spectre.Console;
using ValveKeyValue;
#pragma warning disable CA1031 // Do not catch general exception types
namespace SteamTokenDumper;
internal static class SteamClientData
{
public static void ReadFromSteamClient(Payload payload, KnownDepotIds knownDepotIds)
{
var table = new Table
{
Title = new("Steam client"),
Border = TableBorder.Rounded
};
AnsiConsole.Live(table)
.Start(ctx =>
{
table.AddColumn("Reading tokens from Steam client files");
var steamLocation = GetSteamPath();
if (steamLocation == default)
{
table.AddRow("Did not find Steam client.");
return;
}
table.AddRow($"Found Steam at {steamLocation}");
ctx.Refresh();
try
{
ReadAppInfo(table, payload, Path.Join(steamLocation, "appcache", "appinfo.vdf"));
}
catch (Exception e)
{
table.AddRow($"Failed to parse appinfo: {e}");
}
ctx.Refresh();
try
{
ReadPackageInfo(table, payload, Path.Join(steamLocation, "appcache", "packageinfo.vdf"));
}
catch (Exception e)
{
table.AddRow($"Failed to parse packageinfo: {e}");
}
ctx.Refresh();
try
{
ReadDepotKeys(table, payload, knownDepotIds, Path.Join(steamLocation, "config", "config.vdf"));
}
catch (Exception e)
{
table.AddRow($"Failed to parse config: {e}");
}
});
}
private static void ReadAppInfo(Table table, Payload payload, string filename)
{
using var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new BinaryReader(fs);
var magic = reader.ReadUInt32();
if (magic is not 0x07_56_44_27 and not 0x07_56_44_28 and not 0x07_56_44_29)
{
throw new InvalidDataException($"Unknown appinfo.vdf magic: {magic:X}");
}
fs.Position += 4; // universe
if (magic == 0x07_56_44_29)
{
fs.Position += 8; // offset to string pool
}
do
{
var appid = reader.ReadUInt32();
if (appid == 0)
{
break;
}
var nextOffset = reader.ReadUInt32() + fs.Position; // size
fs.Position += 4 + 4; // infoState + lastUpdated
var token = reader.ReadUInt64();
if (token > 0)
{
payload.Apps[appid.ToString(CultureInfo.InvariantCulture)] = token.ToString(CultureInfo.InvariantCulture);
}
fs.Position = nextOffset;
} while (true);
table.AddRow($"Got {payload.Apps.Count} app tokens from appinfo.vdf");
}
private static void ReadPackageInfo(Table table, Payload payload, string filename)
{
using var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new BinaryReader(fs);
var magic = reader.ReadUInt32();
if (magic == 0x06_56_55_27)
{
table.AddRow("Old Steam client has no package tokens in packageinfo.vdf, skipping");
return;
}
if (magic != 0x06_56_55_28)
{
throw new InvalidDataException($"Unknown packageinfo.vdf magic: {magic:X}");
}
reader.ReadUInt32(); // universe
var deserializer = KVSerializer.Create(KVSerializationFormat.KeyValues1Binary);
do
{
var subid = reader.ReadUInt32();
if (subid == 0xFFFFFFFF)
{
break;
}
fs.Position += 20 + 4;
var token = reader.ReadUInt64();
if (token > 0)
{
payload.Subs[subid.ToString(CultureInfo.InvariantCulture)] = token.ToString(CultureInfo.InvariantCulture);
}
deserializer.Deserialize(fs);
} while (true);
table.AddRow($"Got {payload.Subs.Count} package tokens from packageinfo.vdf");
}
private static void ReadDepotKeys(Table table, Payload payload, KnownDepotIds knownDepotIds, string filename)
{
KVObject data;
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
data = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize(fs, new KVSerializerOptions
{
HasEscapeSequences = true,
});
}
// For some inexplicable reason these keys can have different capilizations
var depots = (data.Children
?.FirstOrDefault(k => k.Name.Equals("software", StringComparison.OrdinalIgnoreCase))
?.FirstOrDefault(k => k.Name.Equals("valve", StringComparison.OrdinalIgnoreCase))
?.FirstOrDefault(k => k.Name.Equals("steam", StringComparison.OrdinalIgnoreCase))
?.FirstOrDefault(k => k.Name.Equals("depots", StringComparison.OrdinalIgnoreCase)))
?? throw new InvalidDataException("Failed to find depots section in config.vdf");
foreach (var depot in depots)
{
var depotKey = depot["DecryptionKey"];
if (depotKey != null)
{
var depotId = uint.Parse(depot.Name, CultureInfo.InvariantCulture);
if (knownDepotIds.PreviouslySent.Contains(depotId) || knownDepotIds.Server.Contains(depotId))
{
continue;
}
payload.Depots[depot.Name] = depotKey.ToString(CultureInfo.InvariantCulture).ToUpperInvariant();
}
}
table.AddRow($"Got {depots.Count()} depot keys from config.vdf");
}
private static string GetSteamPath()
{
if (OperatingSystem.IsWindows())
{
using var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Valve\\Steam") ??
Registry.LocalMachine.OpenSubKey("SOFTWARE\\Valve\\Steam");
if (key?.GetValue("SteamPath") is string steamPath)
{
return steamPath;
}
}
else if (OperatingSystem.IsLinux())
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var paths = new[] { ".steam", ".steam/steam", ".steam/root", ".local/share/Steam" };
return paths
.Select(path => Path.Join(home, path))
.FirstOrDefault(steamPath => Directory.Exists(Path.Join(steamPath, "appcache")));
}
else if (OperatingSystem.IsMacOS())
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
return Path.Join(home, "Steam");
}
return default;
}
}