| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/safe_browsing_db/safe_browsing_api_handler_util.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/metrics/histogram_macros.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/values.h" | |
| 16 #include "components/safe_browsing_db/metadata.pb.h" | |
| 17 #include "components/safe_browsing_db/util.h" | |
| 18 | |
| 19 namespace safe_browsing { | |
| 20 namespace { | |
| 21 | |
| 22 // JSON metatdata keys. These are are fixed in the Java-side API. | |
| 23 const char kJsonKeyMatches[] = "matches"; | |
| 24 const char kJsonKeyThreatType[] = "threat_type"; | |
| 25 | |
| 26 // Do not reorder or delete. Make sure changes are reflected in | |
| 27 // SB2RemoteCallThreatSubType. | |
| 28 enum UmaThreatSubType { | |
| 29 UMA_THREAT_SUB_TYPE_NOT_SET = 0, | |
| 30 UMA_THREAT_SUB_TYPE_LANDING = 1, | |
| 31 UMA_THREAT_SUB_TYPE_DISTRIBUTION = 2, | |
| 32 UMA_THREAT_SUB_TYPE_UNKNOWN = 3, | |
| 33 UMA_THREAT_SUB_TYPE_MAX_VALUE | |
| 34 }; | |
| 35 | |
| 36 void ReportUmaThreatSubType(SBThreatType threat_type, | |
| 37 UmaThreatSubType sub_type) { | |
| 38 if (threat_type == SB_THREAT_TYPE_URL_MALWARE) { | |
| 39 UMA_HISTOGRAM_ENUMERATION( | |
| 40 "SB2.RemoteCall.ThreatSubType.PotentiallyHarmfulApp", sub_type, | |
| 41 UMA_THREAT_SUB_TYPE_MAX_VALUE); | |
| 42 } else { | |
| 43 UMA_HISTOGRAM_ENUMERATION("SB2.RemoteCall.ThreatSubType.SocialEngineering", | |
| 44 sub_type, UMA_THREAT_SUB_TYPE_MAX_VALUE); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 // Parse the extra key/value pair(s) added by Safe Browsing backend. To make | |
| 49 // it binary compatible with the Pver3 metadata that UIManager expects to | |
| 50 // deserialize, we convert it to a MalwarePatternType. | |
| 51 // | |
| 52 // TODO(nparker): When chrome desktop is converted to use Pver4, convert this | |
| 53 // to the new proto instead. | |
| 54 const std::string ParseExtraMetadataToPB(const base::DictionaryValue* match, | |
| 55 SBThreatType threat_type) { | |
| 56 std::string pattern_key; | |
| 57 if (threat_type == SB_THREAT_TYPE_URL_MALWARE) { | |
| 58 pattern_key = "pha_pattern_type"; | |
| 59 } else { | |
| 60 DCHECK(threat_type == SB_THREAT_TYPE_URL_PHISHING); | |
| 61 pattern_key = "se_pattern_type"; | |
| 62 } | |
| 63 | |
| 64 std::string pattern_type; | |
| 65 if (!match->GetString(pattern_key, &pattern_type)) { | |
| 66 ReportUmaThreatSubType(threat_type, UMA_THREAT_SUB_TYPE_NOT_SET); | |
| 67 return std::string(); | |
| 68 } | |
| 69 | |
| 70 MalwarePatternType pb; | |
| 71 if (pattern_type == "LANDING") { | |
| 72 pb.set_pattern_type(MalwarePatternType::LANDING); | |
| 73 ReportUmaThreatSubType(threat_type, UMA_THREAT_SUB_TYPE_LANDING); | |
| 74 } else if (pattern_type == "DISTRIBUTION") { | |
| 75 pb.set_pattern_type(MalwarePatternType::DISTRIBUTION); | |
| 76 ReportUmaThreatSubType(threat_type, UMA_THREAT_SUB_TYPE_DISTRIBUTION); | |
| 77 } else { | |
| 78 ReportUmaThreatSubType(threat_type, UMA_THREAT_SUB_TYPE_UNKNOWN); | |
| 79 return std::string(); | |
| 80 } | |
| 81 | |
| 82 return pb.SerializeAsString(); | |
| 83 } | |
| 84 | |
| 85 int GetThreatSeverity(int java_threat_num) { | |
| 86 // Assign higher numbers to more severe threats. | |
| 87 switch (java_threat_num) { | |
| 88 case JAVA_THREAT_TYPE_POTENTIALLY_HARMFUL_APPLICATION: | |
| 89 return 2; | |
| 90 case JAVA_THREAT_TYPE_SOCIAL_ENGINEERING: | |
| 91 return 1; | |
| 92 default: | |
| 93 // Unknown threat type | |
| 94 return -1; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 SBThreatType JavaToSBThreatType(int java_threat_num) { | |
| 99 switch (java_threat_num) { | |
| 100 case JAVA_THREAT_TYPE_POTENTIALLY_HARMFUL_APPLICATION: | |
| 101 return SB_THREAT_TYPE_URL_MALWARE; | |
| 102 case JAVA_THREAT_TYPE_SOCIAL_ENGINEERING: | |
| 103 return SB_THREAT_TYPE_URL_PHISHING; | |
| 104 default: | |
| 105 // Unknown threat type | |
| 106 return SB_THREAT_TYPE_SAFE; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 } // namespace | |
| 111 | |
| 112 // Valid examples: | |
| 113 // {"matches":[{"threat_type":"5"}]} | |
| 114 // or | |
| 115 // {"matches":[{"threat_type":"4"}, | |
| 116 // {"threat_type":"5", "se_pattern_type":"LANDING"}]} | |
| 117 UmaRemoteCallResult ParseJsonToThreatAndPB(const std::string& metadata_str, | |
| 118 SBThreatType* worst_threat, | |
| 119 std::string* metadata_pb_str) { | |
| 120 *worst_threat = SB_THREAT_TYPE_SAFE; // Default to safe. | |
| 121 *metadata_pb_str = std::string(); | |
| 122 | |
| 123 if (metadata_str.empty()) | |
| 124 return UMA_STATUS_JSON_EMPTY; | |
| 125 | |
| 126 // Pick out the "matches" list. | |
| 127 scoped_ptr<base::Value> value = base::JSONReader::Read(metadata_str); | |
| 128 const base::ListValue* matches; | |
| 129 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY) || | |
| 130 !(static_cast<base::DictionaryValue*>(value.get())) | |
| 131 ->GetList(kJsonKeyMatches, &matches)) { | |
| 132 return UMA_STATUS_JSON_FAILED_TO_PARSE; | |
| 133 } | |
| 134 | |
| 135 // Go through each matched threat type and pick the most severe. | |
| 136 int worst_threat_num = -1; | |
| 137 const base::DictionaryValue* worst_match = nullptr; | |
| 138 for (size_t i = 0; i < matches->GetSize(); i++) { | |
| 139 // Get the threat number | |
| 140 const base::DictionaryValue* match; | |
| 141 std::string threat_num_str; | |
| 142 int java_threat_num = -1; | |
| 143 if (!matches->GetDictionary(i, &match) || | |
| 144 !match->GetString(kJsonKeyThreatType, &threat_num_str) || | |
| 145 !base::StringToInt(threat_num_str, &java_threat_num)) { | |
| 146 continue; // Skip malformed list entries | |
| 147 } | |
| 148 | |
| 149 if (GetThreatSeverity(java_threat_num) > | |
| 150 GetThreatSeverity(worst_threat_num)) { | |
| 151 worst_threat_num = java_threat_num; | |
| 152 worst_match = match; | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 *worst_threat = JavaToSBThreatType(worst_threat_num); | |
| 157 if (*worst_threat == SB_THREAT_TYPE_SAFE || !worst_match) | |
| 158 return UMA_STATUS_JSON_UNKNOWN_THREAT; | |
| 159 *metadata_pb_str = ParseExtraMetadataToPB(worst_match, *worst_threat); | |
| 160 | |
| 161 return UMA_STATUS_UNSAFE; // success | |
| 162 } | |
| 163 | |
| 164 } // namespace safe_browsing | |
| OLD | NEW |