• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "program.h"
17 
18 #include "utils/logger.h"
19 
20 #include "configs/guard_context.h"
21 #include "graph_analyzer.h"
22 
23 namespace {
24 constexpr std::string_view TAG = "[Program]";
25 }
26 
Create()27 void panda::guard::Program::Create()
28 {
29     LOG(INFO, PANDAGUARD) << TAG << "===== program create start =====";
30 
31     for (const auto &[name, record] : this->prog_->record_table) {
32         if (Node::IsJsonFile(record)) {
33             Node node(this, name, "");
34             node.Create();
35             this->nodeTable_.emplace(name, node);
36         }
37 
38         std::string pkgName;
39         if (Node::FindPkgName(record, pkgName)) {
40             if (GuardContext::GetInstance()->GetGuardOptions()->IsSkippedRemoteHar(pkgName)) {
41                 LOG(INFO, PANDAGUARD) << TAG << "skip record: " << record.name;
42                 continue;
43             }
44 
45             Node node(this, name, pkgName);
46             node.Create();
47             this->nodeTable_.emplace(name, node);
48         }
49     }
50 
51     LOG(INFO, PANDAGUARD) << TAG << "===== program create end =====";
52 }
53 
ForEachFunction(const std::function<FunctionTraver> & callback)54 void panda::guard::Program::ForEachFunction(const std::function<FunctionTraver> &callback)
55 {
56     for (auto &[_, node] : this->nodeTable_) {
57         node.ForEachFunction(callback);
58     }
59 }
60 
RemoveConsoleLog()61 void panda::guard::Program::RemoveConsoleLog()
62 {
63     if (!GuardContext::GetInstance()->GetGuardOptions()->IsRemoveLogObfEnabled()) {
64         return;
65     }
66 
67     this->ForEachFunction([](Function &function) { function.RemoveConsoleLog(); });
68 }
69 
Obfuscate()70 void panda::guard::Program::Obfuscate()
71 {
72     LOG(INFO, PANDAGUARD) << TAG << "===== program obfuscate start =====";
73 
74     for (auto &[name, node] : this->nodeTable_) {
75         node.Obfuscate();
76         node.UpdateScopeNames();
77     }
78 
79     this->UpdateReference();
80 
81     this->RemoveConsoleLog();
82 
83     LOG(INFO, PANDAGUARD) << TAG << "===== program obfuscate end =====";
84 }
85 
UpdateReference()86 void panda::guard::Program::UpdateReference()
87 {
88     this->ForEachFunction([](Function &function) -> void { function.UpdateReference(); });
89     for (auto &[_, node] : this->nodeTable_) {
90         node.UpdateFileNameReferences();
91     }
92 }
93