|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 5 | + * |
| 6 | + * Copyright 2025 The ZAP Development Team |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package org.zaproxy.zap.extension.pscanrules; |
| 21 | + |
| 22 | +import java.time.LocalDate; |
| 23 | +import java.time.Period; |
| 24 | +import java.time.YearMonth; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Set; |
| 28 | +import org.apache.commons.lang3.reflect.MethodUtils; |
| 29 | +import org.apache.logging.log4j.LogManager; |
| 30 | +import org.apache.logging.log4j.Logger; |
| 31 | +import org.parosproxy.paros.Constant; |
| 32 | +import org.parosproxy.paros.control.Control; |
| 33 | +import org.parosproxy.paros.core.scanner.Alert; |
| 34 | +import org.parosproxy.paros.network.HttpMessage; |
| 35 | +import org.zaproxy.zap.Version; |
| 36 | +import org.zaproxy.zap.extension.autoupdate.ExtensionAutoUpdate; |
| 37 | +import org.zaproxy.zap.extension.pscan.PluginPassiveScanner; |
| 38 | + |
| 39 | +public class ZapVersionScanRule extends PluginPassiveScanner implements CommonPassiveScanRuleInfo { |
| 40 | + |
| 41 | + private static final String MESSAGE_PREFIX = "pscanrules.zapversion."; |
| 42 | + |
| 43 | + private static final int PLUGIN_ID = 10116; |
| 44 | + |
| 45 | + private static final Logger LOGGER = LogManager.getLogger(ZapVersionScanRule.class); |
| 46 | + |
| 47 | + /** Used to make sure we only raise one alert per host. */ |
| 48 | + private static Set<String> hosts = new HashSet<>(); |
| 49 | + |
| 50 | + private static String latestVersionStr; |
| 51 | + private static Integer risk; |
| 52 | + |
| 53 | + @Override |
| 54 | + public void scanHttpRequestSend(HttpMessage msg, int id) { |
| 55 | + try { |
| 56 | + int risk = getRisk(); |
| 57 | + if (risk <= 0) { |
| 58 | + return; |
| 59 | + } |
| 60 | + String host = msg.getRequestHeader().getURI().getHost(); |
| 61 | + synchronized (hosts) { |
| 62 | + if (hosts.contains(host)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + hosts.add(host); |
| 66 | + } |
| 67 | + buildAlert(risk, getLatestVersionStr()).raise(); |
| 68 | + } catch (Exception e) { |
| 69 | + LOGGER.debug(e.getMessage(), e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private int getRisk() { |
| 74 | + if (risk != null) { |
| 75 | + return risk; |
| 76 | + } |
| 77 | + if (Constant.isDevBuild()) { |
| 78 | + // Assume the user is keeping this up to date |
| 79 | + risk = -1; |
| 80 | + return risk; |
| 81 | + } |
| 82 | + |
| 83 | + if (Constant.isDailyBuild()) { |
| 84 | + risk = getDateRisk(Constant.PROGRAM_VERSION, LocalDate.now()); |
| 85 | + return risk; |
| 86 | + } |
| 87 | + |
| 88 | + String latestVersion = getLatestVersionStr(); |
| 89 | + if (latestVersion == null) { |
| 90 | + // No CFU req yet, might be done next time? |
| 91 | + return -1; |
| 92 | + } |
| 93 | + risk = getVersionRisk(Constant.PROGRAM_VERSION, latestVersion); |
| 94 | + return risk; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns the risk based on 2 semantic version strings like "2.16.1" |
| 99 | + * |
| 100 | + * @param currentVer the version of ZAP being run |
| 101 | + * @param latestVer the latest ZAP version based on the Check For Updates call |
| 102 | + * @return the risk, where <= 0: no risk, 1: low, 2: medium, 3: high |
| 103 | + */ |
| 104 | + protected static int getVersionRisk(String currentVer, String latestVer) { |
| 105 | + try { |
| 106 | + Version cv = new Version(currentVer); |
| 107 | + Version lv = new Version(latestVer); |
| 108 | + |
| 109 | + if (cv.getMajorVersion() == lv.getMajorVersion()) { |
| 110 | + // Easy case, no major version difference |
| 111 | + return intToRisk(lv.getMinorVersion() - cv.getMinorVersion(), 0); |
| 112 | + } |
| 113 | + if (lv.getMajorVersion() - cv.getMajorVersion() > 1) { |
| 114 | + // 2+ major version differences |
| 115 | + return Alert.RISK_HIGH; |
| 116 | + } |
| 117 | + // 1 major version difference, always min of medium risk |
| 118 | + return intToRisk(lv.getMinorVersion(), Alert.RISK_MEDIUM); |
| 119 | + } catch (Exception e) { |
| 120 | + LOGGER.debug(e.getMessage(), e); |
| 121 | + return -1; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static int intToRisk(int i, int minRisk) { |
| 126 | + if (i < minRisk) { |
| 127 | + return minRisk; |
| 128 | + } |
| 129 | + if (i > Alert.RISK_HIGH) { |
| 130 | + return Alert.RISK_HIGH; |
| 131 | + } |
| 132 | + return i; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns the risk based on 1 date-stamped version string like "D-2025-06-30" |
| 137 | + * |
| 138 | + * @param currentVer the date-stamped version of ZAP being run |
| 139 | + * @param today todays date (makes testing so much easier) |
| 140 | + * @return the risk, where <= 0: no risk, 1: low, 2: medium, 3: high |
| 141 | + */ |
| 142 | + protected static int getDateRisk(String currentVer, LocalDate today) { |
| 143 | + if (currentVer == null || !currentVer.startsWith("D-")) { |
| 144 | + return -1; |
| 145 | + } |
| 146 | + YearMonth yearMonth = YearMonth.parse(currentVer.substring(2, 9)); |
| 147 | + LocalDate pastDate = yearMonth.atDay(1); |
| 148 | + return diffToRisk(Period.between(pastDate, today).getYears()); |
| 149 | + } |
| 150 | + |
| 151 | + private static int diffToRisk(int diff) { |
| 152 | + if (diff > Alert.RISK_HIGH) { |
| 153 | + return Alert.RISK_HIGH; |
| 154 | + } |
| 155 | + return diff; |
| 156 | + } |
| 157 | + |
| 158 | + private static String getLatestVersionStr() { |
| 159 | + if (latestVersionStr == null) { |
| 160 | + ExtensionAutoUpdate ext = |
| 161 | + Control.getSingleton() |
| 162 | + .getExtensionLoader() |
| 163 | + .getExtension(ExtensionAutoUpdate.class); |
| 164 | + |
| 165 | + if (ext != null) { |
| 166 | + try { |
| 167 | + Object ver = MethodUtils.invokeMethod(ext, true, "getLatestVersionNumber"); |
| 168 | + |
| 169 | + latestVersionStr = (String) ver; |
| 170 | + } catch (Exception e) { |
| 171 | + LOGGER.debug(e.getMessage(), e); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + return latestVersionStr; |
| 176 | + } |
| 177 | + |
| 178 | + private AlertBuilder buildAlert(int risk, String latest) { |
| 179 | + AlertBuilder ab = |
| 180 | + newAlert() |
| 181 | + .setRisk(risk) |
| 182 | + .setConfidence(Alert.CONFIDENCE_HIGH) |
| 183 | + .setDescription(Constant.messages.getString(MESSAGE_PREFIX + "desc")) |
| 184 | + .setSolution(Constant.messages.getString(MESSAGE_PREFIX + "soln")) |
| 185 | + .setReference(Constant.messages.getString(MESSAGE_PREFIX + "refs")) |
| 186 | + .setCweId(1104) // CWE-1104: Use of Unmaintained Third Party Components |
| 187 | + .setWascId(45); // WASC-45: Application Misconfiguration |
| 188 | + if (latest != null) { |
| 189 | + ab.setOtherInfo(Constant.messages.getString(MESSAGE_PREFIX + "otherinfo", latest)); |
| 190 | + } |
| 191 | + |
| 192 | + return ab; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public int getPluginId() { |
| 197 | + return PLUGIN_ID; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public String getName() { |
| 202 | + return Constant.messages.getString(MESSAGE_PREFIX + "name"); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public List<Alert> getExampleAlerts() { |
| 207 | + return List.of(buildAlert(Alert.RISK_MEDIUM, null).build()); |
| 208 | + } |
| 209 | + |
| 210 | + protected static void clear() { |
| 211 | + hosts.clear(); |
| 212 | + } |
| 213 | +} |
0 commit comments