-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit_windows.vbs
8304 lines (7568 loc) · 404 KB
/
audit_windows.vbs
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
forceCScriptExecution
start_time = Timer
' NOTE - Netstat Ports, Scheduled Tasks and Share Sizes can only be retrieved when running locally.
' below are the default settings
' default to localhost
strcomputer = "."
submit_online = "y"
' create an XML text file of the result in the current directory
create_file = "n"
discovery_id = ""
' submit via a proxy (using the settings of the user running the script)
use_proxy = "n"
' the username (if not using the user running the script)
struser = ""
' the password (if not using the user running the script)
strpass = ""
' If our URL uses https, but the certificate is invalid or unrecognised (self signed), we should submit anyway.
ignore_invalid_ssl = "y"
' optional - assign any PCs audited to this Org - take the OrgId from Open-AudIT web interface
org_id = ""
' optional - query this Active Directory attribute to determine the users work unit
' if attribute #1 produces nothing, then try attribute #2
use_active_directory = "y"
windows_user_work_1 = "physicalDeliveryOfficeName"
windows_user_work_2 = "company"
' should we attempt to query mount points
audit_mount_point = "y"
' should we audit the installed software
audit_software = "y"
' use the win32_product WMI class (not recommended by Microsoft).
' https://support.microsoft.com/kb/974524
' added and set to disabled by default in 1.5.2
audit_win32_product = "n"
' retrieve all DNS names
audit_dns = "y"
' run netstat on the target
' n = no
' y = yes
' s = servers only
audit_netstat = "s"
' if set then delete the audit script upon completion
' useful when starting the script on a remote machine and leaving no trace
self_delete = "n"
' 0 = no debug
' 1 = basic debug
' 2 = verbose debug
' 3 = very verbose debug
debugging = "1"
' Version - NOTE, special formatted so we match the *nix scripts and can do find/replace
version="3.4.0"
' In normal use, DO NOT SET THIS.
' This value is passed in when running the audit_domain script.
' Only set this value if your audit host is on a different domain than audit targets and you are not using audit_domain.vbs - IE, you are running "cscript audit_windows.vbs COMPUTER" where COMPUTER is on a seperate domain than the PC you are running the command on. This would then apply to ALL systems audited like this. This would be the exception rather than the rule. Do not do this unless you know what you are doing :-)
ldap = ""
' set this greater than 0 if you wish to insert systems from AD that have only been seen in the last XX days
ldap_seen_days = "0"
' set this greater than 2000-01-01 if you wish to insert systems from AD that have only been seen since XX date
ldap_seen_date = "2012-06-30"
' attempt to ping a target computer before audit?
ping_target = "n"
' set by the Discovery function - do not normally set this manually
system_id = ""
last_seen_by = "audit"
details_to_lower = "y"
help = "n"
hide_audit_window = "n"
' Should we attempt to audit any connected SAN's
san_audit="y"
' If we detect the san management software, should we run an auto-discover before the query
san_discover = "n"
' DO NOT REMOVE THE LINE BELOW
' Configuration from web UI here
' below we take any command line arguements
' to override the variables above, simply include them on the command line like submit_online=n
' NOTE - argurments are case sensitive
Set objArgs = WScript.Arguments
For Each strArg in objArgs
if instr(strArg, "=") then
argName = lcase(left(strArg,inStr(strArg,"=")-1))
argValue = mid(strArg,inStr(strArg,"=")+1)
select case argName
case "create_file"
create_file = argValue
case "debugging"
debugging = argValue
case "discovery_id"
discovery_id = argValue
case "help"
help = argValue
case "ldap"
ldap = argvalue
case "ignore_invalid_ssl"
ignore_invalid_ssl = argValue
case "org_id"
org_id = argValue
case "ping_target"
ping_target = argValue
case "audit_netstat"
audit_netstat = argValue
case "self_delete"
self_delete = argvalue
case "audit_software"
audit_software = argvalue
case "audit_dns"
audit_dns = argvalue
case "audit_mount_point"
audit_mount_point = argvalue
case "strcomputer"
strcomputer = argvalue
case "struser"
struser = argvalue
case "strpass"
strpass = argvalue
case "submit_online"
submit_online = argValue
case "system_id"
system_id = argValue
case "url"
url = argValue
case "use_active_directory"
use_active_directory = argValue
case "use_proxy"
use_proxy = argValue
case "windows_user_work_1"
windows_user_work_1 = argvalue
case "windows_user_work_2"
windows_user_work_2 = argvalue
case "audit_win32_product"
audit_win32_product = argvalue
case "details_to_lower"
details_to_lower = argvalue
case "hide_audit_window"
hide_audit_window = argvalue
case "san_audit"
san_audit = argvalue
case "san_discover"
san_discover = argvalue
case "last_seen_by"
last_seen_by = argvalue
end select
else
if (strArg = "/help") or (strArg = "/?") then
help = "y"
else
strcomputer = strArg
end if
end if
next
if (help = "y") then
wscript.echo "------------------------------"
wscript.echo "Open-AudIT Windows audit script"
wscript.echo "Version: " & version
wscript.echo "------------------------------"
wscript.echo "This script should be run against a Windows based computer. It audits the target Windows computer and creates a result which can be submitted to the Open-AudIT server or saved as an XML file."
wscript.echo ""
wscript.echo "Valid command line options are below (items containing * are the defaults) and should take the format name=value (eg: debugging=1)."
wscript.echo ""
wscript.echo " create_file"
wscript.echo " y - Create an XML file containing the audit result."
wscript.echo " *n - Do not create an XML result file."
wscript.echo " w - Create an XML file containing the audit result in admin$ share."
wscript.echo ""
wscript.echo " debugging"
wscript.echo " 0 - No output."
wscript.echo " *1 - Minimal Output."
wscript.echo " 2 - Verbose output."
wscript.echo " 3 - Very Verbose output."
wscript.echo ""
wscript.echo " discovery_id"
wscript.echo " * - The Open-AudIT discovery id. This is populated by Open-AudIT when running this script from discovery."
wscript.echo ""
wscript.echo " /? or /help or help=y"
wscript.echo " y - Display this help output."
wscript.echo " *n - Do not display this output."
wscript.echo ""
wscript.echo " ladp"
wscript.echo " - Set by the audit_domain or discover_domain scripts. Do not set."
wscript.echo ""
wscript.echo " local_domain"
wscript.echo " - The domain you wish to audit. Should be in the format LDAP://your.domain.name"
wscript.echo ""
wscript.echo " ignore_invalid_ssl"
wscript.echo " *y - If our URL uses https, but the certificate is invalid or unrecognised (self signed), we should submit anyway."
wscript.echo " n - Do not submit if certificate is invalid (or self signed)."
wscript.echo ""
wscript.echo " org_id"
wscript.echo " - The org_id (an integer) taken from Open-AudIT. If set all devices found will be associated to that Organisation."
wscript.echo ""
wscript.echo " ping_target"
wscript.echo " *n - Attempt to ping the target computer to determine if it is online."
wscript.echo ""
wscript.echo " audit_netstat"
wscript.echo " *s - Run Netstat against 'server' class systems."
wscript.echo " n - Do not run against any computers."
wscript.echo " y - Run against all computers."
wscript.echo ""
wscript.echo " self_delete"
wscript.echo " *n - Do not delete the audit_windows script upon completion."
wscript.echo " y - Delete the audit script file upon script completion."
wscript.echo ""
wscript.echo " skip_*"
wscript.echo " *n - Do not skip detecting attributes in this section of the audit script."
wscript.echo " y - Skip detection of this particular section."
wscript.echo " Valid sections are dns, printer, software, mount_point."
wscript.echo ""
wscript.echo " strcomputer"
wscript.echo " *. - The name (or IP address) of the computer being audited. Default is '.', which means the local system upon which the script is being run."
wscript.echo ""
wscript.echo " struser / strpass"
wscript.echo " - The username (in the format domain/username) and password of the account running the script. Defaults are not set, which means it will run in the context of the account running the script."
wscript.echo ""
wscript.echo " submit_online"
wscript.echo " *y - Submit the audit result to the Open-AudIT server."
wscript.echo " n - Don't submit the audit result."
wscript.echo ""
wscript.echo " use_proxy"
wscript.echo " *n - Do not use the system or user level proxy to submit the audit result."
wscript.echo " y - Use the proxy to submit the result."
wscript.echo ""
wscript.echo " use_active_directory"
wscript.echo " *y - Query Active Directory for additional information."
wscript.echo " n - Never query Active Directory."
wscript.echo ""
wscript.echo " windows_user_work_1"
wscript.echo " physicalDeliveryOfficeName - The Active Directory field to assign the computer to (first preference)."
wscript.echo ""
wscript.echo " windows_user_work_2"
wscript.echo " company - The Active Directory field to assign the computer to (second preference)."
wscript.echo ""
wscript.echo " audit_win32_product"
wscript.echo " *n - Tells the audit script to NOT query the win32_product class. It is recommended by Microsoft not to use this class as is causes Windows to check the integrity of all installed packages (resulting in 1035 events in the log) and can cause performance issues."
wscript.echo " y - Do query win32_product anyway and use the result to add to the list of installed software."
wscript.echo ""
wscript.echo " details_to_lower"
wscript.echo " *y - Convert the hostname to lower."
wscript.echo " n - Keep the hostname as per retrieved."
wscript.echo ""
wscript.echo " hide_audit_window"
wscript.echo " *n - Do not hide the audit script window when executing."
wscript.echo " y - Hide the audit script window when executing."
wscript.echo ""
wscript.echo " san_audit"
wscript.echo " *y - Should we audit a SAN if it is detected"
wscript.echo " n - Do not attempt to audit any attached SANs"
wscript.echo ""
wscript.echo " san_discover"
wscript.echo " *n - Do not run smcli -A if we detect the SAN management software"
wscript.echo " Y - Run the command and update the list of any connected SANs"
wscript.echo ""
wscript.echo " url"
wscript.echo " *http://localhost/open-audit/index.php/discovery/process_subnet - The http url of the Open-AudIT Server used to submit the result to."
wscript.echo ""
wscript.echo "The name of the resulting XML file will be in the format HOSTNAME-YYMMDDHHIISS.xml, as in the hostname of the machine the complete timestamp the audit was started."
wscript.quit
end if
if hide_audit_window = "y" then
hiddenExecution
end if
' start the script
if debugging > "0" then wscript.echo "starting audit - " & strcomputer end if
if debugging > "0" then
wscript.echo "----------------------------"
wscript.echo "Open-AudIT Windows audit script"
wscript.echo "Version: " & version
wscript.echo "----------------------------"
wscript.echo "audit_dns " & audit_dns
wscript.echo "audit_mount_point " & audit_mount_point
wscript.echo "audit_netstat " & audit_netstat
wscript.echo "audit_software " & audit_software
wscript.echo "create_file " & create_file
wscript.echo "debugging " & debugging
wscript.echo "details_to_lower " & details_to_lower
wscript.echo "discovery_id " & discovery_id
wscript.echo "hide_audit_window " & hide_audit_window
wscript.echo "ldap " & ldap
wscript.echo "org_id " & org_id
wscript.echo "ping_target " & ping_target
wscript.echo "self_delete " & self_delete
wscript.echo "strcomputer " & strcomputer
wscript.echo "strpass " & strpass
wscript.echo "struser " & struser
wscript.echo "submit_online " & submit_online
wscript.echo "system_id " & system_id
wscript.echo "url " & url
wscript.echo "use_proxy " & use_proxy
wscript.echo "use_active_directory " & use_active_directory
wscript.echo "windows_user_work_1 " & windows_user_work_1
wscript.echo "windows_user_work_2 " & windows_user_work_2
wscript.echo "-------------------"
end if
system_timestamp = Year(Now()) & "-" & Right("0" & Month(Now()),2) & "-" & Right("0" & Day(Now()),2) & " " & Right("0" & Hour(Now()),2) & ":" & Right("0" & Minute(Now()),2) & ":" & Right("0" & Second(Now()),2)
''''''''''''''''''''''''''''''''''''''''
' Don't change the settings below here '
''''''''''''''''''''''''''''''''''''''''
const HKEY_CLASSES_ROOT = &H80000000
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
const HKEY_USERS = &H80000003
const FOR_READING = 1
const FOR_WRITING = 2
const FOR_APPENDING = 8
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_SECURE_AUTHENTICATION = 1
Const ADS_USE_ENCRYPTION = 2
Const ADS_SCOPE_SUBTREE = 2
Const wbemConnectFlagUseMaxWait = 128
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const TristateTrue = -1
dim objTrans, objDomain
dim dt : dt = Now()
set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")
set wshNetwork = WScript.CreateObject("WScript.Network")
dim local_hostname : local_hostname = wshNetwork.ComputerName
audit_wmi_fails = ""
set result = CreateObject("ADODB.Stream")
result.Type = 2
result.Open
result.Position = 0
result.Charset = "UTF-8"
' some local items
set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " local (Win32_OperatingSystem)" end if
for each objItem in colItems
local_windows_build_number = objItem.BuildNumber
next
set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration " _
& "WHERE IPEnabled = True or (ServiceName<>'' AND ServiceName<>'AsyncMac' " _
& "AND ServiceName<>'VMnetx' AND ServiceName<>'VMnetadapter' " _
& "AND ServiceName<>'Rasl2tp' AND ServiceName<>'msloop' " _
& "AND ServiceName<>'PptpMiniport' AND ServiceName<>'Raspti' " _
& "AND ServiceName<>'NDISWan' AND ServiceName<>'NdisWan4' AND ServiceName<>'RasPppoe' " _
& "AND ServiceName<>'NdisIP' AND Description<>'PPP Adapter.') " _
& "AND MACAddress is not NULL" ,,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " local (Win32_NetworkAdapterConfiguration) 1" end if
local_dns_server = ""
for each objItem in colItems
if (not isnull(objItem.DNSServerSearchOrder)) then
local_dns_server = objItem.DNSServerSearchOrder(0)
end if
next
' enumerate all the ip addresses so we can determine if we are running against localhost
local_net = ""
set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = True ",,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " local (Win32_NetworkAdapterConfiguration) 2" end if
for each objItem in colItems
net_mac_address = objItem.MACAddress
if net_mac_address > "" then
for i = LBound(objItem.IPAddress) to UBound(objItem.IPAddress)
local_net = local_net & " " & objItem.IPAddress(i) & " "
next
end if
next
local_net = " " & local_net & " " & local_hostname & " " & " . "
if debugging > "1" then
wscript.echo "LocalNet: " & local_net
wscript.echo "Target: " & strcomputer
if (instr(lcase(local_net), " " & lcase(strcomputer) & " ") <> 0) then
wscript.echo "Match: Auditing localhost."
if (strcomputer <> ".") then
strcomputer = "."
if debugging > "0" then
wscript.echo "Changed strcomputer from " & strcomputer & " to . because we're auditing this local machine."
end if
end if
else
wscript.echo "No Match: Auditing remote host."
end if
end if
' If auditing the local machine, disregard and supplied credentials
if (strcomputer = "." and (struser <> "" or strpass <> "")) then
if debugging > "0" then
wscript.echo "Disregarding username / password as WMI does not support connecting to localhost with credentials."
end if
struser = ""
strpass = ""
end if
' Ping Test
pc_alive = 0
if ping_target = "y" then
if (strcomputer = ".") then
pc_alive = 1
if debugging > "0" then
wscript.echo "Disregarding ping_target because we're auditing localhost."
end if
else
if (cint(local_windows_build_number) > 2222 and not local_windows_build_number = "3000") then
On Error Resume Next
set ping = objWMIService.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Timeout = 200 and Address = '" & strcomputer & "'")
on error goto 0
for each item in ping
if (IsNull(item.StatusCode) or (item.Statuscode <> 0)) then
' it is not switched on
pc_alive = 0
if debugging > "0" then wscript.echo "PC " & strcomputer & " not responding to ping" end if
else
pc_alive = 1
if debugging > "0" then wscript.echo "PC " & strcomputer & " responding to ping" end if
end if
next
else
pc_alive = 1
if debugging > "0" then wscript.echo "Auditing PC is older than Win2000, Cannot ping target PC." end if
end if
end if
else
if (strcomputer = ".") then
pc_alive = 1
if debugging > "0" then wscript.echo "Not pinging target because we're auditing localhost." end if
else
if debugging > "0" then wscript.echo "Not pinging target (override with ping_target=y)." end if
end if
end if
error_returned = 0
if (struser <> "" and instr(lcase(local_net), " " & lcase(strcomputer) & " ") = 0) then
' credentials passed and not localhost
if ((pc_alive = 1) or (ping_target = "n")) then
if (error_returned = 0) then
On Error Resume Next
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem creating WBEM object (0) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
Set wmiNameSpace = wmiLocator.ConnectServer(strcomputer, "\root\default", struser, strpass, "", "", wbemConnectFlagUseMaxWait)
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (1) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
wmiNameSpace.Security_.ImpersonationLevel = 3
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (2) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
Set oReg = wmiNameSpace.Get("StdRegProv")
Set objWMIService = wmiLocator.ConnectServer(strcomputer, "\root\cimv2",struser,strpass, "", "", wbemConnectFlagUseMaxWait)
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (3) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
objWMIService.Security_.ImpersonationLevel = 3
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (4) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
Set objWMIService2 = wmiLocator.ConnectServer(strcomputer, "\root\WMI",struser,strpass, "", "", wbemConnectFlagUseMaxWait)
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (5) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
objWMIService2.Security_.ImpersonationLevel = 3
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (6) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
Set objWMIService3 = wmiLocator.ConnectServer(strcomputer, "\root\rsop\computer",struser,strpass, "", "", wbemConnectFlagUseMaxWait)
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (6.1) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
end if ' pc_alive or ping_target = n
else
' localhost or no credentials passed, therefore auditing as the user running this script
if ((pc_alive = 1) or (ping_target = "n")) then
if (error_returned = 0) then
On Error Resume Next
set objWMIService = GetObject("winmgmts:\\" & strcomputer & "\root\cimv2")
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (7) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
set objWMIService2 = GetObject("winmgmts:\\" & strcomputer & "\root\WMI")
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (8) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strcomputer & "\root\default:StdRegProv")
error_returned = Err.Number
error_description = Err.Description
if error_description = "" then error_description = "Access Denied. Check Firewall and user security permissions."
on error goto 0
if (error_returned <> 0) then
if debugging > "1" then wscript.echo "Problem authenticating (9) to " & strcomputer end if
if debugging > "1" then wscript.echo "Error Number:" & error_returned end if
if debugging > "1" then wscript.echo "Error Description:" & error_description end if
end if
end if
if (error_returned = 0) then
On Error Resume Next
Set objWMIService3 = GetObject("winmgmts:\\" & strComputer & "\root\rsop\computer")
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem authenticating (10) to " & strcomputer end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
if debugging > "0" then wscript.echo "Warning - Will not be able to retrieve Group Policy details" end if
error_returned = 0
end if
end if
end if ' pc_alive or ping_target = n
end if
if ((error_returned <> 0) or ((pc_alive = 0) and (ping_target = "y"))) then
if ((pc_alive = 0) and (ping_target = "y")) then
if debugging > "1" then wscript.echo "Computer " & strcomputer & " is not responding to ping." end if
end if
if (error_returned <> 0) then
if debugging > "1" then wscript.echo "Cannot connect to " & strcomputer end if
end if
if use_active_directory = "y" then
if debugging > "1" then wscript.echo "Attempting Active Directory data retrieval." end if
if ldap = "" then
if debugging > "1" then wscript.echo "No default LDAP provided, using local settings." end if
set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " (Win32_ComputerSystem)" : audit_wmi_fails = audit_wmi_fails & "Win32_ComputerSystem " : end if
for each objItem in colItems
ldap = "LDAP://" & LCase(objItem.Domain)
next
end if
if ldap > "" then
if debugging > "1" then wscript.echo "LDAP domain: " & ldap end if
' retrieve what details we can from AD
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, pwdLastSet, lastlogon, dnshostname, distinguishedName, operatingsystem from '" & ldap & "' Where objectClass='computer' and name = '" & strcomputer & "'"
objCommand.Properties("Page Size") = 10000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
if objRecordSet.RecordCount > 0 then
if debugging > "1" then wscript.echo "LDAP details retrieved." end if
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
system_hostname = ""
ip_address = ""
computer_dns = ""
family = ""
os_group = ""
os_name = ""
computer_ou = ""
last_seen = ""
last_logon = ""
dns_hostname = ""
on error resume next
set objLastLogon = objRecordSet.Fields("lastlogon").Value
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
thistime = intLastLogonTime + #1/1/1601#
last_logon = datepart("yyyy", thistime) & "-" & right("00" & datepart("m", thistime), 2) & "-" & right("00" & datepart("d", thistime), 2)
on error goto 0
last_pass = ""
on error resume next
set objLastLogon = objRecordSet.Fields("pwdLastSet").Value
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
thistime = intLastLogonTime + #1/1/1601#
last_pass = datepart("yyyy", thistime) & "-" & right("00" & datepart("m", thistime), 2) & "-" & right("00" & datepart("d", thistime), 2)
on error goto 0
if (last_logon > last_pass) then
last_seen = last_logon
else
last_seen = last_pass
end if
if instr(lcase(objRecordSet.Fields("operatingsystem").Value), "windows") then
os_group = "Windows"
else
os_group = ""
end if
system_hostname = objRecordSet.Fields("Name").Value
dns_hostname = objRecordSet.Fields("dnshostname").Value
if details_to_lower = "y" then
system_hostname = lcase(system_hostname)
dns_hostname = lcase(dns_hostname)
end if
strParams = "%comspec% /c NSlookup " & dns_hostname
if debugging > "1" then wscript.echo "Looking up DNS entry for " & dns_hostname end if
Set objExecObj = objShell.exec(strParams)
Do While Not objExecObj.StdOut.AtEndOfStream
strText = objExecObj.StdOut.Readline()
if instr(strText, "Server") then
strServer = trim(replace(strText,"Server:",""))
Elseif instr (strText, "Name") then
strhost = trim(replace(strText,"Name:",""))
Elseif instr (strText, "Address") then
man_ip_address = trim(replace(strText,"Address:","")) : stradd = stradd + 1
End if
Loop
if stradd = 1 then man_ip_address = "0.0.0.0" end if
computer_ou = lcase(ou(objRecordSet.Fields("distinguishedName").Value))
i = split(computer_ou, ",")
for j = 1 to ubound(i)
if instr(i(j), "dc=") then
computer_dns = computer_dns & "." & i(j)
end if
next
computer_dns = replace(computer_dns, ".dc=", ".")
computer_dns = mid(computer_dns, 2)
' i = split(dns_hostname, ".")
' for j = 1 to ubound(i)
' computer_dns = computer_dns & "." & i(j)
' next
if details_to_lower = "y" then
computer_dns = lcase(computer_dns)
end if
os_name = "Microsoft " & objRecordSet.Fields("operatingsystem").Value
family = os_family(objRecordSet.Fields("operatingsystem").Value)
if os_group = "Windows" then
result.WriteText "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbcrlf
result.WriteText "<system>" & vbcrlf
result.WriteText " <sys>" & vbcrlf
result.WriteText " <script_version>" & version & "</script_version>" & vbcrlf
result.WriteText " <id>" & system_id & "</id>" & vbcrlf
result.WriteText " <hostname>" & escape_xml(system_hostname) & "</hostname>" & vbcrlf
result.WriteText " <ip>" & escape_xml(man_ip_address) & "</ip>" & vbcrlf
result.WriteText " <domain>" & escape_xml(computer_dns) & "</domain>" & vbcrlf
result.WriteText " <type>computer</type>" & vbcrlf
result.WriteText " <icon>windows</icon>" & vbcrlf
result.WriteText " <os_group>" & escape_xml(os_group) & "</os_group>" & vbcrlf
result.WriteText " <os_family>" & escape_xml(family) & "</os_family>" & vbcrlf
result.WriteText " <os_name>" & escape_xml(os_name) & "</os_name>" & vbcrlf
result.WriteText " <active_directory_ou>" & escape_xml(computer_ou) & "</active_directory_ou>" & vbcrlf
result.WriteText " <last_seen>" & escape_xml(last_seen) & "</last_seen>" & vbcrlf
result.WriteText " <last_seen_by>active directory</last_seen_by>" & vbcrlf
result.WriteText " <discovery_id>" & escape_xml(discovery_id) & "</discovery_id>" & vbcrlf
result.WriteText " </sys>" & vbcrlf
end if
objRecordSet.MoveNext
Loop
result.WriteText "</system>" & vbcrlf
if debugging > "1" then
result.position = 0
result_text = result.readtext
wscript.echo result_text
end if
if debugging > "1" then wscript.echo "LDAP Seen Days: " & ldap_seen_days end if
if debugging > "1" then wscript.echo "LDAP Actual Days: " & datediff("d", last_seen, Now) end if
if debugging > "1" then wscript.echo "LDAP Seen Date: " & ldap_seen_date end if
if debugging > "1" then wscript.echo "LDAP Actual Date: " & last_seen end if
hit = 0
if cint(ldap_seen_days) > cint(datediff("d", last_seen, Now)) then
' hit
if debugging > "0" then
wscript.echo "PC not able to be audited but seen by Active Directory " & datediff("d", last_seen, Now) & " days ago." & vbcrlf & "As this is less than " & ldap_seen_days & ", using AD details for audit."
end if
hit = 1
elseif cdate(ldap_seen_date) < cdate(last_seen) then
' hit
if debugging > "0" then
wscript.echo "PC not able to be audited but seen in Active Directory on " & last_seen & "." & vbcrlf & "As this is after " & ldap_seen_date & ", using AD details for audit."
end if
hit = 1
else
if debugging > "0" then
wscript.echo "PC not able to be audited and last seen by Active Directory on " & last_seen & "." & vbcrlf & "As this is before " & ldap_seen_date & ", no audit recorded."
end if
hit = 0
end if
if (submit_online = "y" and hit = 1) then
if debugging > "0" then wscript.echo "Submitting audit online" end if
Err.clear
XmlObj = "ServerXMLHTTP"
Set objHTTP = WScript.CreateObject("MSXML2.ServerXMLHTTP.3.0")
objHTTP.setTimeouts 5000, 5000, 5000, 120000
objHTTP.SetOption 2, 13056 ' Ignore all SSL errors
objHTTP.Open "POST", url, False
objHTTP.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
result.position = 0
objHTTP.Send "data=" + urlEncode(result.ReadText()) + vbcrlf
if (objHTTP.ResponseText > "" and debugging > "2") then
wscript.echo
wscript.echo
wscript.echo "Response"
wscript.echo "--------"
wscript.echo objHTTP.ResponseText
if (inStr(objHTTP.ResponseText, "error")) then
wscript.sleep 50000
end if
end if
if debugging > "0" then wscript.echo "Audit Submitted" end if
end if
if (create_file = "y" and hit = 1) then
if debugging > "0" then wscript.echo "Creating output File" end if
' Write the results to a file
file_timestamp = Year(dt) & Right("0" & Month(dt),2) & Right("0" & Day(dt),2) & Right("0" & Hour(dt),2) & Right("0" & Minute(dt),2) & Right("0" & Second(dt),2)
OutputFile = system_hostname & "-" & file_timestamp & ".xml"
if debugging > "0" then wscript.echo "Output file: " & OutputFile end if
Err.clear
on error resume next
result.position = 0
result.SaveToFile OutputFile, 2 ' Overwrites the file with the data from the currently open Stream object, if the file already exists
error_returned = Err.Number
error_description = Err.Description
on error goto 0
if (error_returned <> 0) then
if debugging > "0" then wscript.echo "Problem writing to file." end if
if debugging > "0" then wscript.echo "Error Number:" & error_returned end if
if debugging > "0" then wscript.echo "Error Description:" & error_description end if
else
if debugging > "0" then wscript.echo "Output file created." end if
end if
end if
else ' count > 0
if debugging > "0" then
wscript.echo "PC not able to be audited and not found in Active Directory."
wscript.echo "Active Directory used for search was: " & ldap
wscript.echo "No audit recorded."
end if
end if ' count > 0
end if ' ldap > ""
end if ' use_active_directory = "y"
if debugging > "2" then wscript.sleep 10000 end if
wscript.quit
end if
if ((strcomputer = ".") or (strcomputer = "127.0.0.1") or (lcase(strcomputer) = lcase(local_hostname))) then
audit_location = "local"
else
audit_location = "remote"
end if
sScriptName = wscript.scriptName
nPID = "unknown"
if (cint(local_windows_build_number) > 2222 and not local_windows_build_number = "3000") then
for each oProc in getObject( "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").instancesOf("Win32_Process")
if lcase(oProc.name) = "wscript.exe" _
or lcase(oProc.name) = "cscript.exe" then
sCmdLine = oProc.commandLine
if instr(1, sCmdLine, "\" & sScriptName, vbTextCompare) > 0 _
or instr(1, sCmdLine, " " & sScriptName, vbTextCompare) > 0 _
or instr(1, sCmdLine, """" & sScriptName, vbTextCompare) > 0 then
nPID = oProc.processId
end if
end if
next
end if
if debugging > "0" then wscript.echo "My PID is " & nPID end if
if debugging > "0" then wscript.echo "Audit Start Time " & system_timestamp end if
if debugging > "0" then wscript.echo "Audit Location " & audit_location end if
if debugging > "0" then wscript.echo "-------------------" end if
if debugging > "0" then wscript.echo "system info" end if
set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " (Win32_OperatingSystem)" : audit_wmi_fails = audit_wmi_fails & "Win32_OperatingSystem " : end if
for each objItem in colItems
system_os_version = objItem.Version
windows_build_number = objItem.BuildNumber
windows_service_pack = objItem.ServicePackMajorVersion
system_os_family = os_family(objItem.Caption)
system_os_name = objItem.Caption
system_os_name = replace(system_os_name, "(R)", "")
system_description = objItem.Description
if details_to_lower = "y" then system_description = lcase(system_description) end if
OSInstall = objItem.InstallDate
OSInstall = Left(OSInstall, 8)
OSInstallYear = Left(OSInstall, 4)
OSInstallMonth = Mid(OSInstall, 5, 2)
OSInstallDay = Right(OSInstall, 2)
system_pc_date_os_installation = OSInstallYear & "-" & OSInstallMonth & "-" & OSInstallDay
windows_boot_device = objItem.BootDevice
windows_country_code = WMIOSCountry(objItem.CountryCode)
windows_organisation = objItem.Organization
windows_language = WMIOSLanguage(objItem.OSLanguage)
windows_registered_user = objItem.RegisteredUser
windows_install_directory_split = split(objItem.Name, "|")
windows_install_directory = windows_install_directory_split(1)
next
mem_size = 0
system_pc_memory = 0
set colItems = objWMIService.ExecQuery("Select Capacity,DeviceLocator,FormFactor,MemoryType,TypeDetail,Speed,Tag FROM Win32_PhysicalMemory",,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " (Win32_PhysicalMemory)" : audit_wmi_fails = audit_wmi_fails & "Win32_PhysicalMemory " : end if
for each objItem in colItems
mem_size = int(objItem.Capacity /1024)
system_pc_memory = system_pc_memory + mem_size
next
' set this ahead of time.
system_pc_num_processor = 0
processor_count = 0
processor_logical = 0
' Only get this value if later OS than XP/2003
if (cint(windows_build_number) > 3790) then
set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " (Win32_Processor)" : audit_wmi_fails = audit_wmi_fails & "Win32_Processor " : end if
for each objItem in colItems
system_pc_num_processor = system_pc_num_processor + int(objItem.NumberOfCores)
processor_count = processor_count + 1
processor_logical = processor_logical + int(objItem.NumberOfLogicalProcessors)
next
end if
set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,32)
error_returned = Err.Number : if (error_returned <> 0 and debugging > "0") then wscript.echo check_wbem_error(error_returned) & " (Win32_ComputerSystem)" : audit_wmi_fails = audit_wmi_fails & "Win32_ComputerSystem " : end if
for each objItem in colItems
' this is no longer used because it is actually the NetBIOS name, not the hostname
' we grab it to a temp variable to use below in a last resort situation
netbiosname = objItem.Name
'This is not used because it is not available on Win2000 or WinXP
'system_hostname = LCase(objItem.DNSHostName)
system_domain = objItem.Domain
if details_to_lower = "y" then
system_domain = lcase(system_domain)
end if