-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
system_report_component.cc
926 lines (807 loc) · 35.2 KB
/
system_report_component.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/components/system_report_component.h"
#include <windows.h>
#include <psapi.h>
#include <stdint.h>
#include <winhttp.h>
#include <wrl/client.h>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/base_paths_win.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/process/process_iterator.h"
#include "base/stl_util.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "base/win/registry.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_handle.h"
#include "base/win/scoped_variant.h"
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "chrome/chrome_cleaner/chrome_utils/chrome_util.h"
#include "chrome/chrome_cleaner/chrome_utils/extension_file_logger.h"
#include "chrome/chrome_cleaner/chrome_utils/extensions_util.h"
#include "chrome/chrome_cleaner/constants/chrome_cleaner_switches.h"
#include "chrome/chrome_cleaner/constants/common_registry_names.h"
#include "chrome/chrome_cleaner/logging/logging_service_api.h"
#include "chrome/chrome_cleaner/logging/proto/shared_data.pb.h"
#include "chrome/chrome_cleaner/logging/scoped_timed_task_logger.h"
#include "chrome/chrome_cleaner/os/disk_util.h"
#include "chrome/chrome_cleaner/os/file_path_sanitization.h"
#include "chrome/chrome_cleaner/os/file_path_set.h"
#include "chrome/chrome_cleaner/os/layered_service_provider_wrapper.h"
#include "chrome/chrome_cleaner/os/nt_internals.h"
#include "chrome/chrome_cleaner/os/pre_fetched_paths.h"
#include "chrome/chrome_cleaner/os/process.h"
#include "chrome/chrome_cleaner/os/registry_util.h"
#include "chrome/chrome_cleaner/os/scoped_disable_wow64_redirection.h"
#include "chrome/chrome_cleaner/os/scoped_service_handle.h"
#include "chrome/chrome_cleaner/os/system_util.h"
#include "chrome/chrome_cleaner/os/system_util_cleaner.h"
#include "chrome/chrome_cleaner/os/task_scheduler.h"
#include "chrome/chrome_cleaner/parsers/json_parser/json_parser_api.h"
#include "chrome/chrome_cleaner/parsers/parser_utils/command_line_arguments_sanitizer.h"
#include "chrome/chrome_cleaner/parsers/shortcut_parser/broker/sandboxed_shortcut_parser.h"
#include "components/chrome_cleaner/public/constants/constants.h"
using base::WaitableEvent;
namespace chrome_cleaner {
namespace {
const wchar_t kServicesKeyPath[] = L"system\\currentcontrolset\\services\\";
const wchar_t kAbsolutePrefix[] = L"\\??\\";
const wchar_t kSystemRootPrefix[] = L"\\systemroot\\";
const wchar_t kNameServerPath[] =
L"system\\currentcontrolset\\services\\tcpip\\parameters\\interfaces";
const wchar_t kNameServer[] = L"nameserver";
const bool kHasFileInformation = true;
const bool kNoFileInformation = false;
struct RegistryKey {
HKEY hkey;
const wchar_t* key_path;
};
struct RegistryValue {
HKEY hkey;
const wchar_t* key_path;
const wchar_t* value_name;
};
const RegistryKey startup_registry_keys[] = {
{HKEY_LOCAL_MACHINE, L"software\\microsoft\\windows\\currentversion\\run"},
{HKEY_CURRENT_USER, L"software\\microsoft\\windows\\currentversion\\run"},
{HKEY_LOCAL_MACHINE,
L"software\\microsoft\\windows\\currentversion\\runonce"},
{HKEY_CURRENT_USER,
L"software\\microsoft\\windows\\currentversion\\runonce"},
{HKEY_LOCAL_MACHINE,
L"software\\microsoft\\windows\\currentversion\\runonceex"},
{HKEY_CURRENT_USER,
L"software\\microsoft\\windows\\currentversion\\runonceex"},
{HKEY_LOCAL_MACHINE,
L"software\\microsoft\\windows\\currentversion\\runservices"},
{HKEY_CURRENT_USER,
L"software\\microsoft\\windows\\currentversion\\runservices"},
{HKEY_LOCAL_MACHINE,
L"software\\microsoft\\windows\\currentversion\\runservicesonce"},
{HKEY_CURRENT_USER,
L"software\\microsoft\\windows\\currentversion\\runservicesonce"}};
const RegistryValue system_path_values[] = {
{HKEY_LOCAL_MACHINE, L"system\\currentcontrolset\\control\\session manager",
L"bootexecute"},
{HKEY_LOCAL_MACHINE, L"system\\currentcontrolset\\control\\session manager",
L"setupexecute"},
{HKEY_LOCAL_MACHINE, kAppInitDllsKeyPath, kAppInitDllsValueName},
{HKEY_LOCAL_MACHINE, L"system\\currentcontrolset\\control\\session manager",
L"appcertdlls"}};
const RegistryValue system_values[] = {
{HKEY_LOCAL_MACHINE,
L"software\\policies\\microsoft\\windows\\windowsupdate",
L"disablewindowsupdateaccess"}};
const RegistryKey extension_policy_keys[] = {
{HKEY_LOCAL_MACHINE, kChromePoliciesAllowlistKeyPath},
{HKEY_LOCAL_MACHINE, kChromePoliciesWhitelistKeyPathDeprecated},
{HKEY_CURRENT_USER, kChromePoliciesAllowlistKeyPath},
{HKEY_CURRENT_USER, kChromePoliciesWhitelistKeyPathDeprecated},
{HKEY_LOCAL_MACHINE, kChromePoliciesForcelistKeyPath},
{HKEY_CURRENT_USER, kChromePoliciesForcelistKeyPath},
{HKEY_LOCAL_MACHINE, kChromiumPoliciesAllowlistKeyPath},
{HKEY_LOCAL_MACHINE, kChromiumPoliciesWhitelistKeyPathDeprecated},
{HKEY_CURRENT_USER, kChromiumPoliciesAllowlistKeyPath},
{HKEY_CURRENT_USER, kChromiumPoliciesWhitelistKeyPathDeprecated},
{HKEY_LOCAL_MACHINE, kChromiumPoliciesForcelistKeyPath},
{HKEY_CURRENT_USER, kChromiumPoliciesForcelistKeyPath}};
// Expand an executable path as if the launch process directory was the
// windows folder. This is used to resolve kernel module path.
bool ExpandToAbsolutePathFromWindowsFolder(const base::FilePath& path,
base::FilePath* output) {
DCHECK(output);
if (path.IsAbsolute()) {
*output = path;
return true;
}
// Retrieve the system folder path.
base::FilePath system_folder =
PreFetchedPaths::GetInstance()->GetWindowsFolder();
base::FilePath absolute_path = system_folder.Append(path);
if (base::PathExists(absolute_path)) {
*output = absolute_path;
return true;
}
LOG(ERROR) << "Cannot determine absolute path: file does not exist.";
return false;
}
void RetrieveDetailedFileInformationFromCommandLine(
const std::wstring& content,
internal::FileInformation* file_information,
bool* white_listed) {
// Handle the case where |content| contains only an executable path.
base::FilePath expanded_path(
ExtractExecutablePathFromRegistryContent(content));
if (base::PathExists(expanded_path)) {
RetrieveDetailedFileInformation(expanded_path, file_information,
white_listed);
return;
}
// Fallback using the original path.
RetrieveDetailedFileInformation(base::FilePath(content), file_information,
white_listed);
}
void ReportRegistryValue(const RegKeyPath& key_path,
const wchar_t* name,
const std::wstring& content,
bool has_file_information) {
DCHECK(name);
if (content.empty())
return;
internal::FileInformation file_information;
if (has_file_information) {
bool white_listed = false;
RetrieveDetailedFileInformationFromCommandLine(content, &file_information,
&white_listed);
if (white_listed)
return;
}
internal::RegistryValue registry_value = {};
std::vector<internal::FileInformation> file_informations;
registry_value.key_path = key_path.FullPath();
registry_value.value_name = name;
registry_value.data = content;
if (has_file_information)
file_informations.push_back(file_information);
LoggingServiceAPI::GetInstance()->AddRegistryValue(registry_value,
file_informations);
}
void ReportRegistryValues(const RegistryValue* report_data,
size_t report_data_length,
REGSAM access_mask,
bool has_file_information) {
DCHECK(report_data);
for (size_t offset = 0; offset < report_data_length; ++offset) {
// Retrieve the content of the registry value.
std::wstring content;
const RegKeyPath key_path(report_data[offset].hkey,
report_data[offset].key_path, access_mask);
RegistryError error;
if (!ReadRegistryValue(key_path, report_data[offset].value_name, &content,
nullptr, &error)) {
LOG_IF(WARNING, error != RegistryError::VALUE_NOT_FOUND)
<< "Failed to read string registry value: '" << key_path.FullPath()
<< "\\" << report_data[offset].value_name << "', error: '"
<< static_cast<int>(error) << "'.";
continue;
}
ReportRegistryValue(key_path, report_data[offset].value_name, content,
has_file_information);
}
}
void ReportRegistryKeys(const RegistryKey* report_data,
size_t length,
REGSAM access_mask,
bool has_file_information) {
DCHECK(report_data);
for (size_t offset = 0; offset < length; ++offset) {
// Enumerate values from the current registry key.
base::win::RegistryValueIterator values_it(
report_data[offset].hkey, report_data[offset].key_path, access_mask);
for (; values_it.Valid(); ++values_it) {
std::wstring content;
GetRegistryValueAsString(values_it.Value(), values_it.ValueSize(),
values_it.Type(), &content);
RegKeyPath key_path(report_data[offset].hkey,
report_data[offset].key_path);
ReportRegistryValue(
key_path, values_it.Name(), content,
// File information should only be text.
has_file_information && (values_it.Type() == REG_SZ ||
values_it.Type() == REG_EXPAND_SZ ||
values_it.Type() == REG_MULTI_SZ));
}
}
}
// Reports nameservers i.e.
// system\currentcontrolset\services\tcpip\parameters\interfaces\*\nameserver
void ReportNameServer(REGSAM access_mask) {
base::win::RegistryKeyIterator keys_it(HKEY_LOCAL_MACHINE, kNameServerPath,
access_mask);
// Check each key at this path.
for (; keys_it.Valid(); ++keys_it) {
const std::wstring full_path =
base::StrCat({kNameServerPath, L"\\", keys_it.Name()});
std::wstring content;
uint32_t content_type = REG_NONE;
const RegKeyPath nameserver_key_path(HKEY_LOCAL_MACHINE, full_path.c_str(),
access_mask);
// Check to see if this key has a nameserver value who's content is non
// empty.
if (!ReadRegistryValue(nameserver_key_path, kNameServer, &content,
&content_type, nullptr) ||
content_type != REG_SZ || content.empty()) {
continue;
}
ReportRegistryValue(nameserver_key_path, kNameServer, content,
kNoFileInformation);
}
}
void ReportAppInitDllsTargets(REGSAM access_mask) {
std::wstring content;
uint32_t content_type = REG_NONE;
const RegKeyPath appinit_dlls_key_path(HKEY_LOCAL_MACHINE,
kAppInitDllsKeyPath, access_mask);
if (!ReadRegistryValue(appinit_dlls_key_path, kAppInitDllsValueName, &content,
&content_type, nullptr) ||
content_type != REG_SZ) {
return;
}
std::wstring delimiters(PUPData::kCommonDelimiters,
PUPData::kCommonDelimitersLength);
std::vector<std::wstring> entries = base::SplitString(
content, delimiters, base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
bool white_listed = false;
internal::RegistryValue registry_value = {};
std::vector<internal::FileInformation> file_informations;
registry_value.key_path = appinit_dlls_key_path.FullPath();
registry_value.value_name = kAppInitDllsValueName;
registry_value.data = content;
for (const auto& entry : entries) {
std::wstring long_path;
ConvertToLongPath(entry, &long_path);
base::FilePath expanded_path(
ExpandEnvPathAndWow64Path(base::FilePath(long_path)));
internal::FileInformation file_information;
RetrieveDetailedFileInformation(expanded_path, &file_information,
&white_listed);
if (!white_listed)
file_informations.push_back(file_information);
}
LoggingServiceAPI::GetInstance()->AddRegistryValue(registry_value,
file_informations);
}
void ReportRegistry(REGSAM access_mask) {
// Report on startup keys/values.
ReportRegistryKeys(startup_registry_keys, base::size(startup_registry_keys),
access_mask, kHasFileInformation);
ReportRegistryValues(system_path_values, base::size(system_path_values),
access_mask, kHasFileInformation);
ReportRegistryValues(system_values, base::size(system_values), access_mask,
kNoFileInformation);
// Report on extension policy keys.
ReportRegistryKeys(extension_policy_keys, base::size(extension_policy_keys),
access_mask, kNoFileInformation);
ReportAppInitDllsTargets(access_mask);
ReportNameServer(access_mask);
}
void ReportRegistry() {
ReportRegistry(KEY_WOW64_32KEY);
if (IsX64Architecture())
ReportRegistry(KEY_WOW64_64KEY);
}
void ReportFoldersUnderPath(const base::FilePath& path, const wchar_t* prefix) {
base::FileEnumerator folder_enum(path, false,
base::FileEnumerator::DIRECTORIES);
for (base::FilePath folder = folder_enum.Next(); !folder.empty();
folder = folder_enum.Next()) {
LoggingServiceAPI::GetInstance()->AddInstalledProgram(folder);
}
}
void ReportInstalledPrograms() {
static unsigned int install_paths[] = {
base::DIR_PROGRAM_FILES, base::DIR_PROGRAM_FILESX86,
base::DIR_PROGRAM_FILES6432, base::DIR_APP_DATA,
base::DIR_LOCAL_APP_DATA, base::DIR_COMMON_APP_DATA,
};
std::set<base::FilePath> path_processed;
for (size_t path = 0; path < base::size(install_paths); ++path) {
base::FilePath install_path;
bool success = base::PathService::Get(install_paths[path], &install_path);
if (!success) {
LOG(ERROR) << "Can't get path from PathService.";
continue;
}
if (path_processed.insert(install_path).second)
ReportFoldersUnderPath(install_path, L"Program:");
}
}
void ReportRunningProcesses() {
base::ProcessIterator iter(nullptr);
while (const base::ProcessEntry* entry = iter.NextProcessEntry()) {
base::win::ScopedHandle handle(
::OpenProcess(PROCESS_QUERY_INFORMATION, false, entry->pid()));
std::wstring exec_path;
if (handle.IsValid() &&
GetProcessExecutablePath(handle.Get(), &exec_path)) {
internal::FileInformation file_information;
bool white_listed = false;
RetrieveDetailedFileInformation(base::FilePath(exec_path),
&file_information, &white_listed);
if (!white_listed) {
LoggingServiceAPI::GetInstance()->AddProcess(entry->exe_file(),
file_information);
}
} else {
LOG(WARNING) << "Unable to query process: '" << entry->exe_file() << "'";
}
}
}
bool RetrieveExecutablePathFromPid(DWORD pid, base::FilePath* path) {
DCHECK(path);
base::win::ScopedHandle process_handle(
::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
std::wstring exe_path;
if (!GetProcessExecutablePath(process_handle.Get(), &exe_path))
return false;
*path = base::FilePath(exe_path);
return true;
}
bool RetrieveExecutablePathFromServiceName(const std::wstring& service_name,
base::FilePath* service_path) {
DCHECK(service_path);
std::wstring subkey_path(kServicesKeyPath);
subkey_path += service_name;
std::wstring content;
if (!ReadRegistryValue(RegKeyPath(HKEY_LOCAL_MACHINE, subkey_path.c_str()),
L"imagepath", &content, nullptr, nullptr)) {
return false;
}
base::FilePath file_path = base::FilePath(content);
// Convert a NT native form ("\Device\HarddiskVolumeXX\...") to the
// equivalent path that starts with a drive letter ("C:\...").
base::FilePath dos_file_path;
if (base::DevicePathToDriveLetterPath(file_path, &dos_file_path)) {
file_path = dos_file_path;
} else if (base::StartsWith(file_path.value(), kAbsolutePrefix,
base::CompareCase::INSENSITIVE_ASCII)) {
// Remove the prefix "\??\" in front of the path.
file_path = base::FilePath(
file_path.value().substr(base::size(kAbsolutePrefix) - 1));
} else if (base::StartsWith(file_path.value(), kSystemRootPrefix,
base::CompareCase::INSENSITIVE_ASCII)) {
// Remove the prefix "\systemroot\" in front of the path. The path
// will be resolved from the system folder.
file_path = base::FilePath(
file_path.value().substr(base::size(kSystemRootPrefix) - 1));
}
// For relative path, resolve them from %systemroot%.
base::FilePath absolute_file_path;
if (ExpandToAbsolutePathFromWindowsFolder(file_path, &absolute_file_path))
file_path = absolute_file_path;
*service_path = file_path;
return true;
}
void ReportRunningServices(DWORD services_type) {
ScopedScHandle service_manager;
service_manager.Set(::OpenSCManager(
nullptr, nullptr, SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_CONNECT));
if (!service_manager.IsValid()) {
PLOG(ERROR) << "Cannot open service manager.";
return;
}
std::vector<ServiceStatus> services;
if (!EnumerateServices(service_manager, services_type, SERVICE_ACTIVE,
&services)) {
return;
}
for (const ServiceStatus& service : services) {
base::FilePath service_path;
bool service_found = false;
if (service.service_status_process.dwProcessId != 0) {
service_found = RetrieveExecutablePathFromPid(
service.service_status_process.dwProcessId, &service_path);
} else {
service_found = RetrieveExecutablePathFromServiceName(
service.service_name, &service_path);
}
if (service_found) {
internal::FileInformation file_information;
bool ignored_reporting = false;
RetrieveDetailedFileInformation(service_path, &file_information,
&ignored_reporting);
if (!ignored_reporting) {
LoggingServiceAPI::GetInstance()->AddService(
service.display_name, service.service_name, file_information);
}
}
}
}
void ReportScheduledTasks() {
std::unique_ptr<TaskScheduler> task_scheduler(
TaskScheduler::CreateInstance());
std::vector<std::wstring> task_names;
if (!task_scheduler->GetTaskNameList(&task_names)) {
LOG(ERROR) << "Failed to enumerate scheduled tasks.";
return;
}
for (const auto& task_name : task_names) {
TaskScheduler::TaskInfo task_info;
if (!task_scheduler->GetTaskInfo(task_name.c_str(), &task_info)) {
LOG(ERROR) << "Failed to get info for task '" << task_name << "'";
continue;
}
std::vector<internal::FileInformation> actions;
for (size_t action = 0; action < task_info.exec_actions.size(); ++action) {
bool white_listed = false;
internal::FileInformation file_information;
RetrieveDetailedFileInformation(
task_info.exec_actions[action].application_path, &file_information,
&white_listed);
if (!white_listed)
actions.push_back(file_information);
}
LoggingServiceAPI::GetInstance()->AddScheduledTask(
task_name, task_info.description, actions);
}
}
// Report the file paths of all DLLs loaded into the process with |pid|. The
// |prefix| is prepended to the log line to qualify the running process.
// |paths_reported| contains already reported paths to avoid duplication.
// Reported paths are added to |paths_reported|.
void ReportLoadedModules(DWORD pid,
const wchar_t* prefix,
ModuleHost host,
std::set<base::FilePath>* paths_reported) {
DCHECK(prefix);
DCHECK(paths_reported);
base::win::ScopedHandle snapshot(
::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid));
if (!snapshot.Get())
return;
MODULEENTRY32 module = {sizeof(module)};
if (!::Module32First(snapshot.Get(), &module))
return;
do {
base::FilePath file_path(module.szExePath);
if (!paths_reported->insert(file_path).second) {
// File details are already reported.
continue;
}
internal::FileInformation file_information;
bool white_listed = false;
RetrieveDetailedFileInformation(file_path, &file_information,
&white_listed);
if (!white_listed) {
LoggingServiceAPI::GetInstance()->AddLoadedModule(module.szModule, host,
file_information);
}
} while (::Module32Next(snapshot.Get(), &module));
}
void ReportLoadedModulesOfCurrentProcess() {
std::set<base::FilePath> paths_reported;
ReportLoadedModules(::GetCurrentProcessId(), L"CCT",
ModuleHost::CHROME_CLEANUP_TOOL, &paths_reported);
}
// Report the DLLs for every running process |executable|. DLLs are reported
// only once.
void ReportLoadedModulesOfRunningProcesses(const wchar_t* executable,
ModuleHost host) {
DCHECK(executable);
// |paths_reported| is used to avoid duplicate report of the same path for
// different running chrome processes.
std::set<base::FilePath> paths_reported;
base::NamedProcessIterator iter(executable, nullptr);
while (const base::ProcessEntry* entry = iter.NextProcessEntry())
ReportLoadedModules(entry->pid(), executable, host, &paths_reported);
}
void ReportLayeredServiceProviders() {
LSPPathToGUIDs providers;
GetLayeredServiceProviders(LayeredServiceProviderWrapper(), &providers);
for (LSPPathToGUIDs::const_iterator provider = providers.begin();
provider != providers.end(); ++provider) {
const base::FilePath& path = provider->first;
internal::FileInformation file_information;
bool white_listed = false;
RetrieveDetailedFileInformationFromCommandLine(
path.value(), &file_information, &white_listed);
if (!white_listed) {
std::vector<std::wstring> logged_guids;
const std::set<GUID, GUIDLess>& guids = provider->second;
for (std::set<GUID, GUIDLess>::const_iterator guid = guids.begin();
guid != guids.end(); ++guid) {
logged_guids.push_back(base::win::WStringFromGUID(*guid));
}
LoggingServiceAPI::GetInstance()->AddLayeredServiceProvider(
logged_guids, file_information);
}
}
}
void ReportProxySettingsInformation() {
// Retrieve the default Internet Explorer proxy configuration for the current
// user.
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_proxy_info;
if (::WinHttpGetIEProxyConfigForCurrentUser(&ie_proxy_info)) {
// Report proxy information when it's not the default configuration.
if (ie_proxy_info.lpszProxy || ie_proxy_info.lpszProxyBypass ||
ie_proxy_info.lpszAutoConfigUrl || ie_proxy_info.fAutoDetect) {
std::wstring config =
(ie_proxy_info.lpszProxy ? ie_proxy_info.lpszProxy : L"");
std::wstring bypass =
(ie_proxy_info.lpszProxyBypass ? ie_proxy_info.lpszProxyBypass : L"");
std::wstring autoconfig =
(ie_proxy_info.lpszAutoConfigUrl ? ie_proxy_info.lpszAutoConfigUrl
: L"");
LoggingServiceAPI::GetInstance()->SetWinInetProxySettings(
config, bypass, autoconfig, ie_proxy_info.fAutoDetect != FALSE);
}
if (ie_proxy_info.lpszProxy)
::GlobalFree(ie_proxy_info.lpszProxy);
if (ie_proxy_info.lpszProxyBypass)
::GlobalFree(ie_proxy_info.lpszProxyBypass);
if (ie_proxy_info.lpszAutoConfigUrl)
::GlobalFree(ie_proxy_info.lpszAutoConfigUrl);
}
// Retrieve the default WinHTTP proxy configuration from the registry.
WINHTTP_PROXY_INFO proxy_info;
if (::WinHttpGetDefaultProxyConfiguration(&proxy_info)) {
const char* access_type = nullptr;
switch (proxy_info.dwAccessType) {
case WINHTTP_ACCESS_TYPE_NO_PROXY:
access_type = "no proxy";
break;
case WINHTTP_ACCESS_TYPE_DEFAULT_PROXY:
access_type = "default proxy";
break;
case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
access_type = "named proxy";
break;
default:
access_type = "unknown";
break;
}
// Report proxy information when it's not the default configuration.
if (proxy_info.dwAccessType != WINHTTP_ACCESS_TYPE_NO_PROXY ||
proxy_info.lpszProxy || proxy_info.lpszProxyBypass) {
std::wstring config = (proxy_info.lpszProxy ? proxy_info.lpszProxy : L"");
std::wstring bypass =
(proxy_info.lpszProxyBypass ? proxy_info.lpszProxyBypass : L"");
LoggingServiceAPI::GetInstance()->SetWinHttpProxySettings(config, bypass);
}
if (proxy_info.lpszProxy)
::GlobalFree(proxy_info.lpszProxy);
if (proxy_info.lpszProxyBypass)
::GlobalFree(proxy_info.lpszProxyBypass);
}
}
void ReportForcelistExtensions(ExtensionFileLogger* extension_file_logger) {
std::vector<ExtensionPolicyRegistryEntry> policies;
GetExtensionForcelistRegistryPolicies(&policies);
for (const ExtensionPolicyRegistryEntry& policy : policies) {
std::vector<internal::FileInformation> extension_files;
extension_file_logger->GetExtensionFiles(policy.extension_id,
&extension_files);
LoggingServiceAPI::GetInstance()->AddInstalledExtension(
policy.extension_id, ExtensionInstallMethod::POLICY_EXTENSION_FORCELIST,
extension_files);
}
}
void ReportInstalledExtensions(JsonParserAPI* json_parser,
ExtensionFileLogger* extension_file_logger) {
DCHECK(json_parser);
ReportForcelistExtensions(extension_file_logger);
std::vector<ExtensionPolicyRegistryEntry> extension_settings_policies;
WaitableEvent extension_settings_done(
WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
GetExtensionSettingsForceInstalledExtensions(
json_parser, &extension_settings_policies, &extension_settings_done);
std::vector<ExtensionPolicyFile> master_preferences_policies;
WaitableEvent master_preferences_done(
WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
GetMasterPreferencesExtensions(json_parser, &master_preferences_policies,
&master_preferences_done);
std::vector<ExtensionPolicyFile> default_extension_policies;
WaitableEvent default_extensions_done(
WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
GetNonWhitelistedDefaultExtensions(json_parser, &default_extension_policies,
&default_extensions_done);
// Wait for all asynchronous parsing to be done with a single timeout for all
// phases combined.
const base::TimeTicks end_time =
base::TimeTicks::Now() +
base::TimeDelta::FromMilliseconds(kParseAttemptTimeoutMilliseconds);
extension_settings_done.TimedWait(end_time - base::TimeTicks::Now());
master_preferences_done.TimedWait(end_time - base::TimeTicks::Now());
default_extensions_done.TimedWait(end_time - base::TimeTicks::Now());
// Log extensions that were found
for (const ExtensionPolicyRegistryEntry& policy :
extension_settings_policies) {
std::vector<internal::FileInformation> extension_files;
extension_file_logger->GetExtensionFiles(policy.extension_id,
&extension_files);
LoggingServiceAPI::GetInstance()->AddInstalledExtension(
policy.extension_id, ExtensionInstallMethod::POLICY_EXTENSION_SETTINGS,
extension_files);
}
for (const ExtensionPolicyFile& policy : master_preferences_policies) {
std::vector<internal::FileInformation> extension_files;
extension_file_logger->GetExtensionFiles(policy.extension_id,
&extension_files);
LoggingServiceAPI::GetInstance()->AddInstalledExtension(
policy.extension_id, ExtensionInstallMethod::POLICY_MASTER_PREFERENCES,
extension_files);
}
for (const ExtensionPolicyFile& policy : default_extension_policies) {
std::vector<internal::FileInformation> extension_files;
extension_file_logger->GetExtensionFiles(policy.extension_id,
&extension_files);
LoggingServiceAPI::GetInstance()->AddInstalledExtension(
policy.extension_id, ExtensionInstallMethod::DEFAULT_APPS_EXTENSION,
extension_files);
}
}
void ReportShortcutModifications(ShortcutParserAPI* shortcut_parser) {
// A return here means that lnk shortcut analysis is not enabled on the
// command line.
if (!shortcut_parser)
return;
std::vector<int> keys_of_paths_to_explore = {
base::DIR_USER_DESKTOP, base::DIR_COMMON_DESKTOP,
base::DIR_USER_QUICK_LAUNCH, base::DIR_START_MENU,
base::DIR_COMMON_START_MENU, base::DIR_TASKBAR_PINS};
std::vector<base::FilePath> paths_to_explore;
for (int path_key : keys_of_paths_to_explore) {
base::FilePath path;
if (base::PathService::Get(path_key, &path))
paths_to_explore.push_back(path);
}
std::vector<ShortcutInformation> shortcuts_found;
base::WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
std::set<base::FilePath> chrome_exe_directories;
ListChromeExeDirectories(&chrome_exe_directories);
const std::wstring kChromeExecutableName = L"chrome.exe";
FilePathSet chrome_exe_file_path_set;
for (const auto& path : chrome_exe_directories)
chrome_exe_file_path_set.Insert(path.Append(kChromeExecutableName));
shortcut_parser->FindAndParseChromeShortcutsInFoldersAsync(
paths_to_explore, chrome_exe_file_path_set,
base::BindOnce(
[](base::WaitableEvent* event,
std::vector<ShortcutInformation>* shortcuts_found,
std::vector<ShortcutInformation> parsed_shortcuts) {
*shortcuts_found = parsed_shortcuts;
event->Signal();
},
&event, &shortcuts_found));
event.Wait();
InitializeFilePathSanitization();
for (const ShortcutInformation& shortcut : shortcuts_found) {
base::FilePath target_path(shortcut.target_path);
// All of the shortcuts returned by
// FindAndParseChromeShortcutsInFoldersAsync are guaranteed to be named
// Google Chrome.lnk, if the shortcut points to a file different than
// chrome.exe or if it has arguments we catalog it as interesting to be
// logged.
if (target_path.BaseName().value() != kChromeExecutableName ||
!shortcut.command_line_arguments.empty()) {
std::wstring sanitized_target_path = SanitizePath(target_path);
std::wstring sanitized_lnk_path = SanitizePath(shortcut.lnk_path);
std::string target_digest;
if (PathExists(target_path) &&
!ComputeSHA256DigestOfPath(target_path, &target_digest)) {
LOG(ERROR) << "Cannot compute the sha digest of the target path";
target_digest = "";
}
std::vector<std::wstring> sanitized_command_line_arguments =
SanitizeArguments(shortcut.command_line_arguments);
LoggingServiceAPI::GetInstance()->AddShortcutData(
sanitized_lnk_path, sanitized_target_path, target_digest,
sanitized_command_line_arguments);
}
}
}
} // namespace
SystemReportComponent::SystemReportComponent(JsonParserAPI* json_parser,
ShortcutParserAPI* shortcut_parser)
: created_report_(false),
json_parser_(json_parser),
shortcut_parser_(shortcut_parser),
user_data_path_(
PreFetchedPaths::GetInstance()->GetLocalAppDataFolder().Append(
L"Google\\Chrome\\User Data")) {}
void SystemReportComponent::PreScan() {}
void SystemReportComponent::PostScan(const std::vector<UwSId>& found_pups) {
// If no removable UwS was found, we should collect the detailed system report
// now since there won't be a post-cleanup called.
if (found_pups.size() == 0 ||
!PUPData::HasFlaggedPUP(found_pups, &PUPData::HasRemovalFlag))
CreateFullSystemReport();
}
void SystemReportComponent::PreCleanup() {}
void SystemReportComponent::PostCleanup(ResultCode result_code,
RebooterAPI* rebooter) {
// If the user cancels the cleanup, don't collect system information since the
// old UI quits without giving the user a chance to opt out of uploading this.
if (result_code == RESULT_CODE_CANCELED)
return;
CreateFullSystemReport();
}
void SystemReportComponent::PostValidation(ResultCode result_code) {}
void SystemReportComponent::OnClose(ResultCode result_code) {}
void SystemReportComponent::CreateFullSystemReport() {
LoggingServiceAPI* logging_service = LoggingServiceAPI::GetInstance();
// Don't collect a system report if we've already collected one
if (created_report_)
return;
ExtensionFileLogger extension_file_logger(user_data_path_);
// Don't collect a system report if logs won't be uploaded.
if (!logging_service->uploads_enabled()) {
return;
}
logging_service->SetDetailedSystemReport(true);
ScopedTimedTaskLogger scoped_timed_task_logger(
"Logging detailed system report");
// Make sure this process has the debug privilege to allow opening more
// running processes. If we have to obtain it, be sure to drop it again to
// leave the system in the state we found it. Some unit tests depend on not
// having debug privileges.
base::ScopedClosureRunner release_debug_rights;
if (!HasDebugRightsPrivileges() && AcquireDebugRightsPrivileges()) {
release_debug_rights.ReplaceClosure(
base::BindOnce(base::IgnoreResult(&ReleaseDebugRightsPrivileges)));
}
ReportLoadedModulesOfCurrentProcess();
ReportLoadedModulesOfRunningProcesses(L"chrome.exe", ModuleHost::CHROME);
ReportRunningProcesses();
ReportRunningServices(SERVICE_WIN32);
{
// The wow64 redirection must be disabled because path are reported as
// viewed by the kernel.
ScopedDisableWow64Redirection wow64_disabled;
ReportRunningServices(SERVICE_DRIVER);
}
ReportRegistry();
ReportScheduledTasks();
ReportInstalledPrograms();
ReportLayeredServiceProviders();
ReportProxySettingsInformation();
{
// ReportInstalledExtensions and ReportShortcutModifications use
// WaitableEvent. This is acceptable since the Chrome Cleaner doesn't have a
// UI and the system report is collected at the end of the process.
base::ScopedAllowBaseSyncPrimitives allow_sync;
ReportInstalledExtensions(json_parser_, &extension_file_logger);
ReportShortcutModifications(shortcut_parser_);
}
created_report_ = true;
}
void SystemReportComponent::SetUserDataPathForTesting(
const base::FilePath& test_user_data_path) {
user_data_path_ = test_user_data_path;
}
} // namespace chrome_cleaner