Repository navigation
Expand file tree
/
Copy pathmicrosoftupdate.lua
More file actions
1452 lines (1391 loc) · 49.5 KB
/
Copy pathmicrosoftupdate.lua
File metadata and controls
1452 lines (1391 loc) · 49.5 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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local urlparse = require("socket.url")
local http = require("socket.http")
local https = require("ssl.https")
local cjson = require("cjson")
local utf8 = require("utf8")
local html_entities = require("htmlEntities")
local base64 = require("base64")
local basexx = require("basexx")
local iconv = require("iconv")
local openssl_digest = require("openssl.digest")
local uuid = require("uuid")
math.randomseed(os.time())
uuid.set_rng(uuid.rng.math_random())
local item_dir = os.getenv("item_dir")
local warc_file_base = os.getenv("warc_file_base")
local concurrency = tonumber(os.getenv("concurrency"))
local item_type = nil
local item_name = nil
local item_value = nil
local item_user = nil
local url_count = 0
local tries = 0
local downloaded = {}
local seen_200 = {}
local addedtolist = {}
local abortgrab = false
local killgrab = false
local logged_response = false
local discovered_outlinks = {}
local discovered_items = {}
local discovered_binaries = {}
local discovered_updateids = {}
local discovered_classifications = {}
local discovered_periodic = {}
local bad_items = {}
local ids = {}
local b32digests = cjson.decode(os.getenv("b32digests"))
local uuid_searches = cjson.decode(os.getenv("uuid_searches"))
local retry_url = false
local is_initial_url = true
abort_item = function(item)
abortgrab = true
--killgrab = true
if not item then
item = item_name
end
if not bad_items[item] then
io.stdout:write("Aborting item " .. item .. ".\n")
io.stdout:flush()
bad_items[item] = true
end
end
kill_grab = function(item)
io.stdout:write("Aborting crawling.\n")
killgrab = true
end
read_file = function(file)
if file then
local f = assert(io.open(file))
local data = f:read("*all")
f:close()
return data
else
return ""
end
end
processed = function(url)
if downloaded[url] or addedtolist[url] then
return true
end
return false
end
discover_item = function(target, item)
if not target[item] then
--print("discovered", item)
target[item] = true
return true
end
return false
end
find_item = function(url)
if ids[url] then
return nil
end
local value, revision = string.match(url, "^https?://www%.catalog%.update%.microsoft%.com/ScopedViewRedirect%.aspx%?updateid=([0-9a-f%-]+)&revisionnumber=([0-9]+)$")
if value then
return {
["value"]=value,
["type"]="id",
["revision"]=revision
}
end
local type_ = nil
for pattern, name in pairs({
["^https?://www%.catalog%.update%.microsoft%.com/ScopedViewInline%.aspx%?updateid=([0-9a-f%-]+)$"]="id",
["^https?://www%.catalog%.update%.microsoft%.com/ScopedViewRedirect%.aspx%?updateid=([0-9a-f%-]+)$"]="id",
["^https?://catalog%.s%.download%.windowsupdate%.com/(.+)$"]="bin",
["^(https?://download%.microsoft%.com/.+)$"]="binurl",
["^(https?://catalog%.sf%.dl%.delivery%.mp%.microsoft%.com/.+)$"]="binurl",
["^https?://www%.microsoft%.com/[0-9a-z%-]+/download/details%.aspx%?id=([0-9]+)$"]="dlc",
["^https?://www%.catalog%.update%.microsoft%.com/Search%.aspx%?q=([^&]+)$"]="search"
}) do
value = string.match(url, pattern)
type_ = name
if value then
break
end
end
if value and type_ then
return {
["value"]=value,
["type"]=type_
}
end
end
is_binary_item = function(type_)
return type_ == "bin" or type_ == "binurl"
end
finish_item = function()
if item_type == "id" then
if abortgrab then
return true
end
for digest in pairs(context["encrypted"]) do
if not context["decryption"][digest] then
error("Did not finish the decryption check.")
end
end
if context["metadata_seen"]
and not context["revision_item"]
and context["revision_checks"] ~= math.max(3, math.floor(tonumber(context["revision"]) / 100) + 1) then
error("Did not finish the revision check.")
end
if not context["download_found"]
and not context["metadata_seen"]
and not context["driver_set_seen"] then
error("Did not finish the catalog or metadata check.")
end
return true
elseif item_type == "dlc" then
if not abortgrab and not context["download_found"] then
error("Did not find a DLC download.")
end
return true
elseif not is_binary_item(item_type) then
return true
end
local count = 0
for _, matches in pairs(context["matches"]) do
if matches == false then
error("Incorrect matching SHA1 found.")
elseif matches == true then
count = count + 1
end
end
if count < 1 then
error("Incorrect number of matching URLs found.")
end
return true
end
set_item = function(url)
found = find_item(url)
if found then
local newcontext = {
["decryption"]={},
["encrypted"]={},
["matches"]={},
["retried"]={},
["revision_checks"]=0,
["todo_binaries"]={}
}
new_item_type = found["type"]
new_item_value = found["value"]
if new_item_type == "bin" then
local temp = string.match(new_item_value, "^[cd]/(.*)$")
if temp then
new_item_value = temp
end
elseif new_item_type == "search" then
newcontext["search_escaped"] = new_item_value
new_item_value = string.gsub(new_item_value, "%+", " ")
new_item_value = urlparse.unescape(new_item_value)
newcontext["search"] = new_item_value
local search_term, star_term = string.match(new_item_value, "^(.-) *([0-9a-f]*)%*$")
newcontext["search_term"] = search_term
newcontext["star_term"] = star_term
if star_term then
star_term_check = uuid_searches[new_item_value]
if not star_term_check then
error("Expected a UUID search query.")
elseif star_term_check ~= star_term then
error("Inconsistent star terms found.")
end
new_item_type = "uuid-search"
new_item_value = star_term .. ":" .. search_term
end
end
if found["revision"] then
newcontext["revision_item"] = true
newcontext["revision"] = found["revision"]
discover_item(discovered_updateids, "id:" .. new_item_value)
end
local extra = ""
if is_binary_item(new_item_type) then
local b32digest = b32digests[new_item_value]
extra = b32digest .. ":"
end
new_item_name = new_item_type .. ":" .. extra .. new_item_value
if newcontext["revision_item"] then
new_item_name = new_item_name .. ":" .. newcontext["revision"]
end
if new_item_name ~= item_name then
finish_item()
ids = {}
context = newcontext
item_value = new_item_value
item_type = new_item_type
ids[string.lower(item_value)] = true
if context["search"] then
ids[string.lower(context["search"])] = true
ids[string.lower(context["search_escaped"])] = true
end
abortgrab = false
tries = 0
retry_url = false
is_initial_url = true
item_name = new_item_name
print("Archiving item " .. item_name)
end
end
end
allowed = function(url, parenturl)
local noscheme = string.match(url, "^https?://(.*)$")
if ids[url]
or (noscheme and ids[string.lower(noscheme)]) then
return true
end
if ids[string.match(url, "^https?://[^/]+/(.*)$")]
or ids[string.match(url, "^https?://[^/]+/[^/]+/(.*)$")]
or (
item_type == "id"
and (
string.match(url, "/DownloadDialog%.aspx")
or string.match(url, "^https://sws%.update%.microsoft%.com/[^/]+/[^/]+%.asmx$")
or string.match(url, "^https://sws%.update%.microsoft%.com/[^/]+/[^/]+%.asmx%?op=[0-9a-zA-Z]+[&]?.*$")
or string.match(url, "^https://www%.microsoft%.com/en%-us/wdsi/definitions/antimalware%-definition%-release%-notes%?requestVersion=[0-9]+%.[0-9]+%.[0-9]+%.[0-9]+$")
)
)
or (
item_type == "dlc"
and string.match(url, "^https?://www%.microsoft%.com/[0-9a-z%-]+/download/details%.aspx%?id=" .. item_value .. "$")
) then
return true
end
if item_type == "dlc"
and string.match(url, "^https?://download%.microsoft%.com/.+$") then
discover_item(discovered_binaries, "binurl::" .. percent_encode_url(string.gsub(url, "^http://", "https://")))
return false
end
if not string.match(url, "^https?://[^/]*update%.microsoft%.com/")
and not string.match(url, "^https?://[^/]*download%.windowsupdate%.com/") then
discover_item(discovered_outlinks, string.match(percent_encode_url(url), "^([^%s]+)"))
return false
end
for _, pattern in pairs({
"([0-9a-fA-F%-]+)",
"([^%?=&;]+)"
}) do
for s in string.gmatch(url, pattern) do
if ids[string.lower(s)] then
return true
end
end
end
return false
end
wget.callbacks.download_child_p = function(urlpos, parent, depth, start_url_parsed, iri, verdict, reason)
local url = urlpos["url"]["url"]
local html = urlpos["link_expect_html"]
--[[if allowed(url, parent["url"])
and not processed(url)
and string.match(url, "^https://")
and not addedtolist[url] then
addedtolist[url] = true
return true
end]]
return false
end
decode_codepoint = function(newurl)
newurl = string.gsub(
newurl, "\\[uU]([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])",
function (s)
return utf8.char(tonumber(s, 16))
end
)
return newurl
end
percent_encode_url = function(newurl)
result = string.gsub(
newurl, "(.)",
function (s)
local b = string.byte(s)
if b <= 32 or b > 126 then
return string.format("%%%02X", b)
end
return s
end
)
return result
end
wget.callbacks.get_urls = function(file, url, is_css, iri)
local urls = {}
local html = nil
local json = nil
local post_data = nil
local soap_action = nil
local software_distribution = "http://www.microsoft.com/SoftwareDistribution"
local server_sync_url = "https://sws.update.microsoft.com/ServerSyncWebService/ServerSyncWebService.asmx"
local protocol_version = "1.21"
downloaded[url] = true
if abortgrab then
return {}
end
local function fix_case(newurl)
if not newurl then
newurl = ""
end
if not string.match(newurl, "^https?://[^/]") then
return newurl
end
if string.match(newurl, "^https?://[^/]+$") then
newurl = newurl .. "/"
end
local a, b = string.match(newurl, "^(https?://[^/]+/)(.*)$")
return string.lower(a) .. b
end
local function check(newurl)
if not string.match(newurl, "^https?://") then
return nil
end
if not newurl then
newurl = ""
end
newurl = decode_codepoint(newurl)
newurl = fix_case(newurl)
local origurl = url
if string.len(url) == 0
or string.len(newurl) == 0 then
return nil
end
local url = string.match(newurl, "^([^#]+)")
local url_ = string.match(url, "^(.-)[%.\\]*$")
while string.find(url_, "&") do
url_ = string.gsub(url_, "&", "&")
end
local request_id = url_ .. (post_data or "")
if not processed(request_id)
and allowed(url_, origurl) then
local headers = {}
if string.match(url_, "/DownloadDialog%.aspx") then
if not post_data then
return nil
end
headers["Content-Type"] = "application/x-www-form-urlencoded"
table.insert(urls, {
url=url_,
headers=headers,
body_data=post_data,
method="POST"
})
elseif soap_action then
headers["Content-Type"] = "text/xml; charset=utf-8"
headers["SOAPAction"] = "\"" .. soap_action .. "\""
table.insert(urls, {
url=url_,
headers=headers,
body_data=post_data,
method="POST"
})
elseif post_data then
error("Did not expect post data.")
else
table.insert(urls, {
url=url_,
headers=headers
})
end
addedtolist[request_id] = true
addedtolist[url] = true
end
end
local function checknewurl(newurl)
if not newurl then
newurl = ""
end
newurl = decode_codepoint(newurl)
if string.match(newurl, "['\"><]") then
return nil
end
if string.match(newurl, "^https?:////") then
check(string.gsub(newurl, ":////", "://"))
elseif string.match(newurl, "^https?://") then
check(newurl)
elseif string.match(newurl, "^https?:\\/\\?/") then
check(string.gsub(newurl, "\\", ""))
elseif string.match(newurl, "^\\/\\/") then
checknewurl(string.gsub(newurl, "\\", ""))
elseif string.match(newurl, "^//") then
check(urlparse.absolute(url, newurl))
elseif string.match(newurl, "^\\/") then
checknewurl(string.gsub(newurl, "\\", ""))
elseif string.match(newurl, "^/") then
check(urlparse.absolute(url, newurl))
elseif string.match(newurl, "^%.%./") then
if string.match(url, "^https?://[^/]+/[^/]+/") then
check(urlparse.absolute(url, newurl))
else
checknewurl(string.match(newurl, "^%.%.(/.+)$"))
end
elseif string.match(newurl, "^%./") then
check(urlparse.absolute(url, newurl))
end
end
local function checknewshorturl(newurl)
if not newurl then
newurl = ""
end
newurl = decode_codepoint(newurl)
if string.match(newurl, "^%?") then
check(urlparse.absolute(url, newurl))
elseif not (
string.match(newurl, "^https?:\\?/\\?//?/?")
or string.match(newurl, "^[/\\]")
or string.match(newurl, "^%./")
or string.match(newurl, "^[jJ]ava[sS]cript:")
or string.match(newurl, "^[mM]ail[tT]o:")
or string.match(newurl, "^vine:")
or string.match(newurl, "^android%-app:")
or string.match(newurl, "^ios%-app:")
or string.match(newurl, "^data:")
or string.match(newurl, "^irc:")
or string.match(newurl, "^%${")
) then
check(urlparse.absolute(url, newurl))
end
end
local function set_new_params(newurl, data)
for param, value in pairs(data) do
if value == nil then
value = ""
elseif type(value) == "string" then
value = "=" .. value
end
if string.match(newurl, "[%?&]" .. param .. "[=&]") then
newurl = string.gsub(newurl, "([%?&]" .. param .. ")=?[^%?&;]*", "%1" .. value)
else
if string.match(newurl, "%?") then
newurl = newurl .. "&"
else
newurl = newurl .. "?"
end
newurl = newurl .. param .. value
end
end
return newurl
end
local function increment_param(newurl, param, default, step)
local value = string.match(newurl, "[%?&]" .. param .. "=([0-9]+)")
if value then
value = tonumber(value)
value = value + step
return set_new_params(newurl, {[param]=tostring(value)})
else
if default ~= nil then
default = tostring(default)
end
return set_new_params(newurl, {[param]=default})
end
end
local function get_count(data)
local count = 0
for _ in pairs(data) do
count = count + 1
end
return count
end
local function check_soap(newurl, action, data, params, params_only)
soap_action = action
post_data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
.. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
.. "<soap:Body>" .. data .. "</soap:Body>"
.. "</soap:Envelope>"
local query_url = newurl .. "?op=" .. string.match(action, "([^/]+)$")
if not params_only then
check(newurl)
if not params then
check(query_url)
end
end
if type(params) == "string" then
params = {params}
end
if params then
for _, value in ipairs(params) do
check(query_url .. "&" .. value)
end
end
post_data = nil
soap_action = nil
end
local function update_identity(revision)
return "<UpdateIdentity>"
.. "<UpdateID>" .. item_value .. "</UpdateID>"
.. "<RevisionNumber>" .. revision .. "</RevisionNumber>"
.. "</UpdateIdentity>"
end
local function discover_binary(digest, newurl)
if item_type == "id"
and context["encrypted"]
and context["encrypted"][digest]
and not context["decryption"][digest] then
context["todo_binaries"][digest .. ":" .. newurl] = {digest, newurl}
return
end
local b32digest = ""
if digest then
b32digest = basexx.to_base32(base64.decode(digest))
end
newurl = string.gsub(newurl, "^(https?://)www%.", "%1")
local domain, path = string.match(newurl, "^https?://([^/]+)/(.+)$")
if domain == "catalog.sf.dl.delivery.mp.microsoft.com"
or domain == "download.microsoft.com" then
discover_item(discovered_binaries, "binurl:" .. b32digest .. ":" .. percent_encode_url(string.gsub(newurl, "^http://", "https://")))
elseif domain == "catalog.s.download.windowsupdate.com"
or domain == "download.windowsupdate.com"
or domain == "b1.download.windowsupdate.com"
or domain == "au.download.windowsupdate.com"
or domain == "au.b1.download.windowsupdate.com" then
path = string.match(path, "^[cd]/(.+)$") or path
discover_item(discovered_binaries, "bin:" .. b32digest .. ":" .. path)
else
error("Found unexpected address " .. domain .. ".")
end
context["download_found"] = true
end
if item_type == "bin" then
for _, domain in pairs({
"https://catalog.s.download.windowsupdate.com/",
"http://download.windowsupdate.com/",
"http://b1.download.windowsupdate.com/",
"http://au.download.windowsupdate.com/",
"http://au.b1.download.windowsupdate.com/"
}) do
for _, path in pairs({"", "c/", "d/"}) do
check(domain .. path .. item_value)
end
end
end
if allowed(url)
and (
status_code < 300
or (
status_code == 500
and string.match(url, "[%?&]revisionstart=[0-9]+&revisionend=[0-9]+")
)
)
and not is_binary_item(item_type) then
html = read_file(file)
if string.match(url, "^https://www%.microsoft%.com/en%-us/wdsi/definitions/antimalware%-definition%-release%-notes%?requestVersion=") then
local requested = string.match(url, "requestVersion=([0-9]+%.[0-9]+%.[0-9]+%.[0-9]+)$")
local returned = string.match(html, "<snap id=\"titleVersion\">([0-9]+%.[0-9]+%.[0-9]+%.[0-9]+)</snap>")
if returned ~= requested then
error("Response has wrong version " .. tostring(returned) .. " for " .. requested .. ".")
end
return urls
end
if string.match(url, "/ScopedViewRedirect%.aspx%?updateid=") then
check_soap(
server_sync_url,
software_distribution .. "/GetAuthConfig",
"<GetAuthConfig xmlns=\"" .. software_distribution .. "\" />"
)
end
if string.match(url, "/ScopedViewInline%.aspx%?updateid=") then
post_data = "updateIDs=%5B%7B%22size%22%3A0%2C%22languages%22%3A%22%22%2C%22uidInfo%22%3A%22" .. item_value .. "%22%2C%22updateID%22%3A%22" .. item_value .. "%22%7D%5D"
.. "&updateIDsBlockedForImport="
.. "&wsusApiPresent="
.. "&contentImport="
.. "&sku="
.. "&serverName="
.. "&ssl="
.. "&portNumber="
.. "&version="
for _, params in pairs({
"",
"?updateid=" .. item_value,
"?scopedview=true",
"?updateid=" .. item_value .. "&scopedview=true"
}) do
check("https://www.catalog.update.microsoft.com/DownloadDialog.aspx" .. params)
end
post_data = nil
for _, endpoint in pairs({
"ViewBasket.aspx?updateids=",
"ScopedView.aspx?updateid=",
"ScopedViewBasic.aspx?updateid=",
"ScopedViewGeneric.aspx?updateid="
}) do
check("https://www.catalog.update.microsoft.com/" .. endpoint .. item_value)
end
end
if string.match(url, "/DownloadDialog%.aspx") then
for download_information in string.gmatch(html, "downloadInformation%[([0-9]+)%]%s*=") do
if download_information ~= "0" then
error("Unexpected downloadInformation ID " .. download_information .. ".")
end
local base_s = "downloadInformation%[" .. download_information .. "%]%.files%["
local max_num = -1
local count = 0
local files = {}
for files_id in string.gmatch(html, base_s .. "([0-9]+)%]%s*=") do
local num = tonumber(files_id)
if num > max_num then
max_num = num
end
local file_data = {}
for k, v in string.gmatch(html, base_s .. files_id .. "%]%.([0-9a-zA-Z]+)%s*=%s*(.-);\r?\n") do
local temp = string.match(v, "^'(.*)'$")
if temp then
v = temp
end
file_data[k] = v
end
files[files_id] = file_data
count = count + 1
end
if max_num + 1 ~= count then
error("Got incorrect file count.")
end
for _, file_data in pairs(files) do
discover_binary(file_data["digest"], file_data["url"])
end
end
end
if string.match(url, "^https://sws%.update%.microsoft%.com/") then
local revision_start, revision_end = string.match(url, "[%?&]revisionstart=([0-9]+)&revisionend=([0-9]+)")
if revision_start then
revision_start = tonumber(revision_start)
revision_end = tonumber(revision_end)
local revisions = {}
local expected = revision_end - revision_start + 1
local requested = expected
local returned = expected
local removed = 0
for revision = revision_start, revision_end do
revisions[revision] = true
end
if status_code == 500 then
local message = html_entities.decode(string.match(html, "<Message>(.-)</Message>"))
local missing = nil
requested, returned, removed, missing = string.match(
message,
"Requested ([0-9]+) returned ([0-9]+) %(test content removed: ([0-9]+)%)%. Missing Revisions: (.+)"
)
requested = tonumber(requested)
returned = tonumber(returned)
removed = tonumber(removed)
for updateid, revision in string.gmatch(missing, "([0-9a-fA-F%-]+)%.([0-9]+)") do
if string.lower(updateid) == item_value then
revisions[tonumber(revision)] = false
end
end
end
local count = 0
for revision = revision_start, revision_end do
if revisions[revision] then
count = count + 1
end
end
if requested ~= expected
or returned ~= count
or removed ~= 0 then
error("Incorrect revision check response.")
end
for revision = revision_start, revision_end do
if revisions[revision] then
discover_item(discovered_updateids, "id:" .. item_value .. ":" .. revision)
end
end
context["revision_checks"] = context["revision_checks"] + 1
return urls
elseif string.match(url, "[%?&]op=") then
return urls
end
html = string.gsub(
html,
"<[0-9a-zA-Z_%-]*:?XmlUpdateBlobCompressed>(.-)</[0-9a-zA-Z_%-]*:?XmlUpdateBlobCompressed>",
function(compressed)
local filename = item_dir .. "/" .. warc_file_base .. "_metadata.cab"
local file = assert(io.open(filename, "wb"))
file:write(base64.decode(compressed))
file:close()
local process = assert(io.popen("cabextract -q -p " .. filename))
local metadata = process:read("*all")
local success = process:close()
os.remove(filename)
if not success or string.len(metadata) == 0 then
error("Could not decompress XmlUpdateBlob.")
end
if string.byte(metadata, 2) == 0 and string.byte(metadata, 4) == 0 then
metadata = assert(iconv.new("UTF-8", "UTF-16LE"):iconv(metadata))
end
for _, escape in ipairs({
{"&", "&"},
{"<", "<"},
{">", ">"}
}) do
metadata = string.gsub(metadata, escape[1], escape[2])
end
return "<XmlUpdateBlob>" .. metadata .. "</XmlUpdateBlob>"
end
)
if string.match(html, "<GetAuthConfigResponse") then
local dss_auth = software_distribution .. "/Server/DssAuthWebService"
check_soap(
"https://sws.update.microsoft.com/DssAuthWebService/DssAuthWebService.asmx",
dss_auth .. "/GetAuthorizationCookie",
"<GetAuthorizationCookie xmlns=\"" .. dss_auth .. "\">"
.. "<accountName>wsus.contoso.com</accountName>"
.. "<accountGuid>" .. uuid() .. "</accountGuid>"
.. "</GetAuthorizationCookie>"
)
elseif string.match(html, "<GetAuthorizationCookieResponse") then
local plugin_id = html_entities.decode(string.match(html, "<PlugInId>(.-)</PlugInId>"))
local cookie_data = html_entities.decode(string.match(html, "<CookieData>(.-)</CookieData>"))
check_soap(
server_sync_url,
software_distribution .. "/GetCookie",
"<GetCookie xmlns=\"" .. software_distribution .. "\">"
.. "<authCookies>"
.. "<AuthorizationCookie>"
.. "<PlugInId>" .. plugin_id .. "</PlugInId>"
.. "<CookieData>" .. cookie_data .. "</CookieData>"
.. "</AuthorizationCookie>"
.. "</authCookies>"
.. "<protocolVersion>" .. protocol_version .. "</protocolVersion>"
.. "</GetCookie>"
)
elseif string.match(html, "<GetCookieResponse") then
local expiration = html_entities.decode(string.match(html, "<Expiration>(.-)</Expiration>"))
local encrypted_data = html_entities.decode(string.match(html, "<EncryptedData>(.-)</EncryptedData>"))
context["wsus_cookie"] = "<cookie>"
.. "<Expiration>" .. expiration .. "</Expiration>"
.. "<EncryptedData>" .. encrypted_data .. "</EncryptedData>"
.. "</cookie>"
check_soap(
server_sync_url,
software_distribution .. "/GetConfigData",
"<GetConfigData xmlns=\"" .. software_distribution .. "\">"
.. context["wsus_cookie"]
.. "</GetConfigData>"
)
elseif string.match(html, "<GetConfigDataResponse") then
if string.match(html, "<ProtocolVersion>(.-)</ProtocolVersion>") ~= protocol_version then
error("Found higher protocol version.")
end
check_soap(
server_sync_url,
software_distribution .. "/GetRelatedRevisionsForUpdates",
"<GetRelatedRevisionsForUpdates xmlns=\"" .. software_distribution .. "\">"
.. context["wsus_cookie"]
.. "<updateIDs>"
.. "<guid>" .. item_value .. "</guid>"
.. "</updateIDs>"
.. "</GetRelatedRevisionsForUpdates>",
"updateid=" .. item_value
)
elseif string.match(html, "<GetRelatedRevisionsForUpdatesResponse") then
for updateid, revision in string.gmatch(
html,
"<UpdateIdentity>%s*"
.. "<UpdateID>([0-9a-fA-F%-]+)</UpdateID>%s*"
.. "<RevisionNumber>([0-9]+)</RevisionNumber>%s*"
.. "</UpdateIdentity>"
) do
updateid = string.lower(updateid)
if updateid == item_value then
if not context["revision_item"] then
context["revision"] = revision
discover_item(discovered_updateids, "id:" .. updateid .. ":" .. revision)
end
elseif string.match(updateid, "^[0-9a-f%-]+$") then
discover_item(discovered_updateids, "id:" .. updateid)
discover_item(discovered_updateids, "id:" .. updateid .. ":" .. revision)
end
end
if context["revision"] then
check_soap(
server_sync_url,
software_distribution .. "/GetUpdateData",
"<GetUpdateData xmlns=\"" .. software_distribution .. "\">"
.. context["wsus_cookie"]
.. "<updateIds>" .. update_identity(context["revision"]) .. "</updateIds>"
.. "</GetUpdateData>",
{
"updateid=" .. item_value .. "&revisionnumber=" .. context["revision"],
"updateid=" .. item_value
}
)
else
check_soap(
server_sync_url,
software_distribution .. "/GetDriverSetData",
"<GetDriverSetData xmlns=\"" .. software_distribution .. "\">"
.. context["wsus_cookie"]
.. "<driverSets>"
.. "<guid>" .. item_value .. "</guid>"
.. "</driverSets>"
.. "</GetDriverSetData>",
"driversetid=" .. item_value
)
end
elseif string.match(html, "<GetDriverSetDataResponse") then
for data in string.gmatch(html, "<ServerSyncDriverSetData>(.-)</ServerSyncDriverSetData>") do
local driver_set_id = string.lower(html_entities.decode(string.match(data, "<DriverSetId>(.-)</DriverSetId>")))
if driver_set_id ~= item_value then
error("Found unexpected driver set.")
end
context["driver_set_seen"] = true
local driver_set = html_entities.decode(string.match(data, "<DriverSetXml>(.-)</DriverSetXml>"))
for _, pattern in ipairs({
"UpdateID%s*=%s*\"([0-9a-fA-F%-]+)\"",
"<UpdateID>%s*([0-9a-fA-F%-]+)%s*</UpdateID>"
}) do
for updateid in string.gmatch(driver_set, pattern) do
discover_item(discovered_updateids, "id:" .. string.lower(updateid))
end
end
end
if not context["driver_set_seen"] and not context["download_found"] then
abort_item()
end
elseif string.match(html, "<GetUpdateDataResponse") then
local is_product = false
local metadata_has_files = false
local secured = false
for data in string.gmatch(html, "<ServerSyncUpdateData>(.-)</ServerSyncUpdateData>") do
local updateid, revision = string.match(
data,
"<Id>%s*"
.. "<UpdateID>([0-9a-fA-F%-]+)</UpdateID>%s*"
.. "<RevisionNumber>([0-9]+)</RevisionNumber>%s*"
.. "</Id>"
)
if updateid then
updateid = string.lower(updateid)
end
if updateid == item_value
and revision == context["revision"] then
context["metadata_seen"] = true
metadata_has_files = string.match(data, "<FileDigestList>") ~= nil
local decoded = html_entities.decode(data)
local bundle = string.gsub(
updateid,
"^([0-9]+%-[0-9]+)%-[0-9]+%-00[36][24]%-([0-9]+)[0-9][0-9]$",
"%1-0001-0000-%200"
)
if bundle ~= updateid then
discover_item(discovered_updateids, "id:" .. bundle)
discover_item(discovered_updateids, "id:" .. bundle .. ":" .. revision)
end
local definition_version = string.match(decoded, "Microsoft Endpoint Protection %- KB2461484.-([0-9]+%.[0-9]+%.[0-9]+%.[0-9]+)")
if definition_version then
check("https://www.microsoft.com/en-us/wdsi/definitions/antimalware-definition-release-notes?requestVersion=" .. definition_version)
end
if string.match(decoded, "UpdateType%s*=%s*\"Category\"") then
local category_type = string.match(decoded, "CategoryType%s*=%s*\"([a-zA-Z]+)\"")
if category_type == "Product" then
is_product = true
end
if category_type == "Product"
or category_type == "ProductFamily"
or category_type == "UpdateClassification"
or category_type == "Company" then
discover_item(discovered_periodic, "id:" .. updateid)
end
if category_type == "UpdateClassification" then
discover_item(discovered_classifications, "id:" .. updateid)
end
end
secured = string.match(decoded, "SecuredFragment%s*=%s*\"FileDecryption\"") ~= nil
or string.match(decoded, "<[0-9a-zA-Z_%-]*:?SecuredFragment>%s*FileDecryption%s*</[0-9a-zA-Z_%-]*:?SecuredFragment>") ~= nil
for attributes in string.gmatch(decoded, "<[0-9a-zA-Z_%-]*:?File%s+([^>]+)>") do
if string.match(attributes, "IsEncrypted%s*=%s*\"true\"") then
local digest = string.match(attributes, "Digest%s*=%s*\"([^\"]+)\"")
if not digest then
error("Did not find digest for encrypted file.")
end
context["encrypted"][digest] = true
end
end
for related in string.gmatch(decoded, "UpdateID%s*=%s*\"([0-9a-fA-F%-]+)\"") do
related = string.lower(related)
if related ~= item_value then
discover_item(discovered_updateids, "id:" .. related)
end
end
elseif updateid then
discover_item(discovered_updateids, "id:" .. updateid)
discover_item(discovered_updateids, "id:" .. updateid .. ":" .. revision)
end
end
if not context["metadata_seen"] then
error("Did not find expected metadata.")
end
if secured and get_count(context["encrypted"]) == 0 then
error("Did not find encrypted files.")
end
local metadata_url_found = false
for data in string.gmatch(html, "<ServerSyncUrlData>(.-)</ServerSyncUrlData>") do
local newurl = string.match(data, "<MUUrl>(.-)</MUUrl>")
if newurl then
local digest = html_entities.decode(string.match(data, "<FileDigest>(.-)</FileDigest>"))
metadata_url_found = true
discover_binary(digest, html_entities.decode(newurl))
end
end
if metadata_has_files and not metadata_url_found then
error("Did not find a metadata file download.")
end
if not context["revision_item"] then
local revision = tonumber(context["revision"])
for revision_start = 0, math.max(200, math.floor(revision / 100) * 100), 100 do
local revision_end = revision_start + 99
if revision_start == 0 then
revision_start = 1
end
local update_ids = ""
for value = revision_start, revision_end do
update_ids = update_ids .. update_identity(value)
end
check_soap(
server_sync_url,
software_distribution .. "/GetUpdateData",
"<GetUpdateData xmlns=\"" .. software_distribution .. "\">"
.. context["wsus_cookie"]
.. "<updateIds>" .. update_ids .. "</updateIds>"
.. "</GetUpdateData>",
"updateid=" .. item_value .. "&revisionstart=" .. revision_start .. "&revisionend=" .. revision_end,
true
)
end
end
if get_count(context["encrypted"]) > 0 then
check_soap(
server_sync_url,