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