forked from Gerenios/AADInternals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneDrive_utils.ps1
409 lines (346 loc) · 13.5 KB
/
OneDrive_utils.ps1
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Utility functions for OneDrive native client
# OneDrive settings class
class OneDriveSettings {
[string]$Url
[string]$AuthenticationCookie
[string]$DefaultDocumentLibraryId
[string]$DownloadUrlTemplate
[int]$ItemCount
}
# Gets the authentication cookie for OneDrive native client
# Nov 26th 2019
function Get-ODAuthenticationCookie
{
<#
.SYNOPSIS
Gets authentication cookie for OneDrive
.DESCRIPTION
Gets authentication cookie for OneDrive native client
.Parameter AccessToken
AccessToken for OneDrive
.Example
Get-AADIntODAuthenticationCookie
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[String]$AccessToken
)
Process
{
# Get the tenant url
$tenant = ((Read-Accesstoken $AccessToken).aud.Split("/"))[2]
$url = "https://$tenant/_api/SP.OAuth.NativeClient/Authenticate?client-request-id=$((New-Guid).toString())"
$headers=@{
"Authorization" = "Bearer $AccessToken"
"Accept"= "application/json;odata=verbose"
"User-Agent"="Microsoft SkyDriveSync 19.192.0926.0012 ship; Windows NT 10.0 (17763)"
"X-GeoMoveOptions" = "HttpRedirection"
"X-IDCRL_ACCEPTED" ="t"
"X-UserScenario"= "AUO,SignIn"
}
# Call the authentication API
$response = Invoke-WebRequest -UseBasicParsing -uri $url -MaximumRedirection 0 -ErrorAction SilentlyContinue -Method Post -ContentType "application/x-www-form-urlencoded" -Headers $headers
# Return the SPOIDCRL cookie
($response.headers["Set-Cookie"].split(";"))[0]
}
}
# Invokes the OD API commands
# Nov 26th
function Invoke-ODCommand
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
$OneDriveSettings,
[Parameter(Mandatory=$True)]
[String]$Command,
[Parameter(Mandatory=$False)]
[String]$Accept="application/json;odata=verbose",
[Parameter(Mandatory=$False)]
[String]$Scenario="AUO,SignIn",
[Parameter(Mandatory=$False)]
[byte[]]$Body,
[Parameter(Mandatory=$False)]
$headers=@{},
[Parameter(Mandatory=$False)]
[Switch]$UseStreamReader,
[Parameter(Mandatory=$False)]
[PSObject][ref]$ResponseHeaders,
[Parameter(Mandatory=$False)]
[boolean]$Mac=$False
)
Process
{
# Set the headers
$headers["Accept"] = $Accept
if($MAC)
{
$headers["User-Agent"] = "Microsoft SkyDriveSync 20.169.0823.0006 ship; Mac OS X 10.15.7"
}
else
{
$headers["User-Agent"] = "Microsoft SkyDriveSync 19.192.0926.0012 ship; Windows NT 10.0 (17763)"
}
if(![string]::IsNullOrEmpty($Scenario))
{
$headers["X-UserScenario"] = $Scenario
}
# Create a web session for the authentication cookie
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$webCookie = New-Object System.Net.Cookie
$webCookie.Name = ($OneDriveSettings.AuthenticationCookie.Split("="))[0]
$webCookie.Value = $OneDriveSettings.AuthenticationCookie.Substring($webCookie.Name.Length + 1)
$webCookie.Domain = ($OneDriveSettings.Url.Split("/"))[2]
$session.Cookies.Add($webCookie)
# Create the url
$url = $OneDriveSettings.Url
$url += $Command
# Call the API
try
{
if($UseStreamReader)
{
if($Body -ne $null)
{
$fullResponse = Invoke-WebRequest -UseBasicParsing -uri $url -Method Post -Headers $headers -WebSession $session -Body $Body
}
else
{
$fullResponse = Invoke-WebRequest -UseBasicParsing -uri $url -Method Get -Headers $headers -WebSession $session
}
$response = [System.IO.StreamReader]::new($fullResponse.RawContentStream, [System.Text.Encoding]::UTF8).ReadToEnd()
if($ResponseHeaders -ne $null)
{
$ResponseHeaders.Value = $fullResponse.headers
}
}
else
{
$response = Invoke-RestMethod -UseBasicParsing -uri $url -Method Get -Headers $headers -WebSession $session
}
}
catch
{
if($_.Exception -like "*(501)*")
{
Write-Error "Got 501 - try using a -Mac switch or proper domain guid"
}
elseif($Body -ne $null -and $_.Exception -like "*(403)*" -and $ResponseHeaders -ne $null)
{
# This is part of the normal file upload flow
$ResponseHeaders.Value = $_.Exception.Response.Headers
}
else
{
Write-Error $_.Exception
}
return
}
# Return
$response
}
}
# Creates an OneDrive settings object to be used in OneDrive functions
function New-OneDriveSettings
{
<#
.SYNOPSIS
Creates a new OneDriveSettings object
.DESCRIPTION
Creates a new OneDriveSettings object used with OneDrive functions
.Example
$os = New-AADIntOneDriveSettings
PS C:\> Get-AADIntOneDriveFiles -OneDriveSettings $os | Format-Table
Path Size Created Modified ResourceID
---- ---- ------- -------- ----------
\RootFolder\Document1.docx 11032 2.12.2019 20.47.23 2.12.2019 20.48.46 5e7acf393a2e45f18c1ce6caa7...
\RootFolder\Book.xlsx 8388 2.12.2019 20.49.14 2.12.2019 20.50.14 b26c0a38d4d14b23b785576e29...
\RootFolder\Docs\Document1.docx 84567 9.12.2019 11.24.40 9.12.2019 12.17.50 d9d51e47b66c4805aff3a08763...
\RootFolder\Docs\Document2.docx 31145 7.12.2019 17.28.37 7.12.2019 17.28.37 972f9c317e1e468fb2b6080ac2...
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName='Credentials',Mandatory=$False)]
[System.Management.Automation.PSCredential]$Credentials,
[Parameter(ParameterSetName='SAML',Mandatory=$True)]
[String]$SAMLToken,
[Parameter(ParameterSetName='Kerberos',Mandatory=$True)]
[String]$KerberosTicket,
[Parameter(ParameterSetName='Kerberos',Mandatory=$True)]
[String]$Domain
)
Process
{
# Create a new settings object
$ODSettings=[OneDriveSettings]::new()
# Get AccessToken for OfficeApps
$OAtoken=Get-AccessToken -Resource "https://officeapps.live.com" -ClientId "ab9b8c07-8f02-4f72-87fa-80105867a763" -KerberosTicket $KerberosTicket -Domain $Domain -SAMLToken $SAMLToken -Credentials $Credentials -IncludeRefreshToken $true
# Get the connection details
$connections = Get-UserConnections -AccessToken $OAtoken[0]
# Get the url
foreach($connection in $connections)
{
if($connection.EnabledCapabilities -eq 2051) # Should be OneDrive
{
$url = $connection.ConnectionUrl
# Strip the "/Documents" from the end of the url
$ODSettings.Url = $url.Substring(0,$url.LastIndexOf("/"))
break
}
}
if([string]::IsNullOrEmpty($ODSettings.Url))
{
# The user doesn't have onedrive :(
$upn = (Read-Accesstoken $OAtoken[0]).upn
Write-Error "The user $upn doesn't have OneDrive :("
return
}
# Get AccessToken for OneDrive
$ODtoken=Get-AccessTokenWithRefreshToken -Resource "https://$(($ODSettings.Url.Split("/"))[2])" -ClientId "ab9b8c07-8f02-4f72-87fa-80105867a763" -RefreshToken $OAtoken[1] -TenantId ((Read-Accesstoken -AccessToken $OAtoken[0]).tid)
# Get the authentication cookie
$ODSettings.AuthenticationCookie = Get-ODAuthenticationCookie -AccessToken $ODtoken
# Get the document library id
$ODSettings.DefaultDocumentLibraryId = Get-ODDefaultDocLibId -OneDriveSettings $ODSettings
# Get the sync policy
$syncPolicy = Get-ODSyncPolicy -OneDriveSettings $ODSettings
# Set the download url template
$dlUrl = $syncPolicy.DownloadUrlTemplate
$ODSettings.DownloadUrlTemplate = $dlUrl.Substring(0,$dlUrl.IndexOf("{"))
# Set the ItemCount
$ODSettings.ItemCount = [int]$syncPolicy.ItemCount
# return
$ODSettings
}
}
# QuickXorHash by Microsoft https://docs.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash
# Dec 9th 2019
$xorhash_code = @"
using System;
public class QuickXorHash : System.Security.Cryptography.HashAlgorithm
{
private const int BitsInLastCell = 32;
private const byte Shift = 11;
private const int Threshold = 600;
private const byte WidthInBits = 160;
private UInt64[] _data;
private Int64 _lengthSoFar;
private int _shiftSoFar;
public QuickXorHash()
{
this.Initialize();
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
unchecked
{
int currentShift = this._shiftSoFar;
// The bitvector where we'll start xoring
int vectorArrayIndex = currentShift / 64;
// The position within the bit vector at which we begin xoring
int vectorOffset = currentShift % 64;
int iterations = Math.Min(cbSize, QuickXorHash.WidthInBits);
for (int i = 0; i < iterations; i++)
{
bool isLastCell = vectorArrayIndex == this._data.Length - 1;
int bitsInVectorCell = isLastCell ? QuickXorHash.BitsInLastCell : 64;
// There's at least 2 bitvectors before we reach the end of the array
if (vectorOffset <= bitsInVectorCell - 8)
{
for (int j = ibStart + i; j < cbSize + ibStart; j += QuickXorHash.WidthInBits)
{
this._data[vectorArrayIndex] ^= (ulong)array[j] << vectorOffset;
}
}
else
{
int index1 = vectorArrayIndex;
int index2 = isLastCell ? 0 : (vectorArrayIndex + 1);
byte low = (byte)(bitsInVectorCell - vectorOffset);
byte xoredByte = 0;
for (int j = ibStart + i; j < cbSize + ibStart; j += QuickXorHash.WidthInBits)
{
xoredByte ^= array[j];
}
this._data[index1] ^= (ulong)xoredByte << vectorOffset;
this._data[index2] ^= (ulong)xoredByte >> low;
}
vectorOffset += QuickXorHash.Shift;
while (vectorOffset >= bitsInVectorCell)
{
vectorArrayIndex = isLastCell ? 0 : vectorArrayIndex + 1;
vectorOffset -= bitsInVectorCell;
}
}
// Update the starting position in a circular shift pattern
this._shiftSoFar = (this._shiftSoFar + QuickXorHash.Shift * (cbSize % QuickXorHash.WidthInBits)) % QuickXorHash.WidthInBits;
}
this._lengthSoFar += cbSize;
}
protected override byte[] HashFinal()
{
// Create a byte array big enough to hold all our data
byte[] rgb = new byte[(QuickXorHash.WidthInBits - 1) / 8 + 1];
// Block copy all our bitvectors to this byte array
for (Int32 i = 0; i < this._data.Length - 1; i++)
{
Buffer.BlockCopy(
BitConverter.GetBytes(this._data[i]), 0,
rgb, i * 8,
8);
}
Buffer.BlockCopy(
BitConverter.GetBytes(this._data[this._data.Length - 1]), 0,
rgb, (this._data.Length - 1) * 8,
rgb.Length - (this._data.Length - 1) * 8);
// XOR the file length with the least significant bits
// Note that GetBytes is architecture-dependent, so care should
// be taken with porting. The expected value is 8-bytes in length in little-endian format
var lengthBytes = BitConverter.GetBytes(this._lengthSoFar);
System.Diagnostics.Debug.Assert(lengthBytes.Length == 8);
for (int i = 0; i < lengthBytes.Length; i++)
{
rgb[(QuickXorHash.WidthInBits / 8) - lengthBytes.Length + i] ^= lengthBytes[i];
}
return rgb;
}
public override sealed void Initialize()
{
this._data = new ulong[(QuickXorHash.WidthInBits - 1) / 64 + 1];
this._shiftSoFar = 0;
this._lengthSoFar = 0;
}
public override int HashSize
{
get
{
return QuickXorHash.WidthInBits;
}
}
}
"@
Add-Type -TypeDefinition $xorhash_code -Language CSharp
Remove-Variable $xorhash_code
# Calculates XorHash for OneDrive files
# Dec 9th 2019
function Get-XorHash
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$FileName
)
Process
{
# Get the full path..
$fullpath = (Get-Item $FileName).FullName
# Create a stream to read bytes
$stream = [System.IO.FileStream]::new($fullpath,[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read)
# Create the hash object and do the magic
$xorhash = [quickxorhash]::new()
$hash = $xorhash.ComputeHash($stream)
$b64Hash = [convert]::ToBase64String($hash)
# Return
$b64Hash
}
}