|
| 1 | +package org.owasp.webgoat.lessons; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Enumeration; |
| 7 | +import java.util.List; |
| 8 | +import java.util.zip.ZipEntry; |
| 9 | +import java.util.zip.ZipException; |
| 10 | +import java.util.zip.ZipFile; |
| 11 | + |
| 12 | +import org.apache.commons.fileupload.FileItem; |
| 13 | +import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
| 14 | +import org.apache.commons.fileupload.servlet.ServletFileUpload; |
| 15 | +import org.apache.ecs.Element; |
| 16 | +import org.apache.ecs.ElementContainer; |
| 17 | +import org.apache.ecs.html.A; |
| 18 | +import org.apache.ecs.html.Form; |
| 19 | +import org.apache.ecs.html.IMG; |
| 20 | +import org.apache.ecs.html.Input; |
| 21 | +import org.apache.ecs.html.P; |
| 22 | +import org.owasp.webgoat.session.ECSFactory; |
| 23 | +import org.owasp.webgoat.session.WebSession; |
| 24 | + |
| 25 | +/******************************************************************************* |
| 26 | + * |
| 27 | + * |
| 28 | + * This file is part of WebGoat, an Open Web Application Security Project |
| 29 | + * utility. For details, please see http://www.owasp.org/ |
| 30 | + * |
| 31 | + * Copyright (c) 2002 - 20014 Bruce Mayhew |
| 32 | + * |
| 33 | + * This program is free software; you can redistribute it and/or modify it under |
| 34 | + * the terms of the GNU General Public License as published by the Free Software |
| 35 | + * Foundation; either version 2 of the License, or (at your option) any later |
| 36 | + * version. |
| 37 | + * |
| 38 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 39 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 40 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 41 | + * details. |
| 42 | + * |
| 43 | + * You should have received a copy of the GNU General Public License along with |
| 44 | + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 45 | + * Place - Suite 330, Boston, MA 02111-1307, USA. |
| 46 | + * |
| 47 | + * Getting Source ============== |
| 48 | + * |
| 49 | + * Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository |
| 50 | + * for free software projects. |
| 51 | + * |
| 52 | + * For details, please see http://webgoat.github.io |
| 53 | + * |
| 54 | + * @author Jakub Koperwas of <a href="http://www.sages.com.pl">Sages</a> |
| 55 | + * @created October 31, 2014 |
| 56 | + */ |
| 57 | + |
| 58 | +public class ZipBomb extends LessonAdapter { |
| 59 | + public final static A SAGES_LOGO = new A().setHref( |
| 60 | + "http://www.sages.com.pl").addElement( |
| 61 | + new IMG("images/logos/sages.png").setAlt("Sages").setBorder(0) |
| 62 | + .setHspace(0).setVspace(0).setStyle("width:180px; height:60px")); |
| 63 | + |
| 64 | + |
| 65 | + protected Element createContent(WebSession s) { |
| 66 | + |
| 67 | + |
| 68 | + ElementContainer ec = new ElementContainer(); |
| 69 | + |
| 70 | + |
| 71 | + if ("success".equalsIgnoreCase((String)s.get(ZIP_DOS))){ |
| 72 | + System.out.println("final success"); |
| 73 | + makeSuccess(s); |
| 74 | + } |
| 75 | + try { |
| 76 | + |
| 77 | + ec.addElement(new P().addElement("Upload new File")); |
| 78 | + |
| 79 | + Input input = new Input(Input.FILE, "myfile", ""); |
| 80 | + ec.addElement(input); |
| 81 | + |
| 82 | + Element b = ECSFactory.makeButton("Start Upload"); |
| 83 | + ec.addElement(b); |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + } catch (Exception e) { |
| 88 | + s.setMessage("Error generating " + this.getClass().getName()); |
| 89 | + e.printStackTrace(); |
| 90 | + } |
| 91 | + |
| 92 | + return ec; |
| 93 | + } |
| 94 | + |
| 95 | + protected Category getDefaultCategory() { |
| 96 | + return Category.DOS; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + public List<String> getHints(WebSession s) { |
| 101 | + List<String> hints = new ArrayList<String>(); |
| 102 | + |
| 103 | + hints |
| 104 | + .add("You can upload up to 2MB file at once,see what can you insert INTO the file"); |
| 105 | + |
| 106 | + return hints; |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + public String getInstructions(WebSession s) { |
| 111 | + String instructions = ""; |
| 112 | + |
| 113 | + |
| 114 | + instructions = "Server accepts only ZIP files, \n" |
| 115 | + + "extracts them after uploading, does something with them and deletes," |
| 116 | + + "\n it provides 20 MB temporal storage to handle all request \n" |
| 117 | + + "try do perform DOS attack that consume all temporal storage with one request"; |
| 118 | + |
| 119 | + |
| 120 | + return (instructions); |
| 121 | + } |
| 122 | + |
| 123 | + private final static Integer DEFAULT_RANKING = new Integer(10); |
| 124 | + private static final String ZIP_DOS = "ZIP_DOS"; |
| 125 | + |
| 126 | + protected Integer getDefaultRanking() { |
| 127 | + return DEFAULT_RANKING; |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + public String getTitle() { |
| 133 | + return ("ZipBomb"); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + public Element getCredits() { |
| 138 | + return super.getCustomCredits("", SAGES_LOGO); |
| 139 | + } |
| 140 | + |
| 141 | + public void handleRequest(WebSession s) { |
| 142 | + File tmpDir=(File)s.getRequest().getServletContext().getAttribute("javax.servlet.context.tempdir"); |
| 143 | + |
| 144 | + try { |
| 145 | + if (ServletFileUpload.isMultipartContent(s.getRequest())) { |
| 146 | + |
| 147 | + DiskFileItemFactory factory = new DiskFileItemFactory(); |
| 148 | + factory.setSizeThreshold(500000); |
| 149 | + |
| 150 | + ServletFileUpload upload = new ServletFileUpload(factory); |
| 151 | + |
| 152 | + |
| 153 | + List /* FileItem */items = upload.parseRequest(s.getRequest()); |
| 154 | + |
| 155 | + |
| 156 | + java.util.Iterator iter = items.iterator(); |
| 157 | + while (iter.hasNext()) { |
| 158 | + FileItem item = (FileItem) iter.next(); |
| 159 | + |
| 160 | + if (!item.isFormField()) { |
| 161 | + |
| 162 | + File uploadedFile= new File(tmpDir, item.getName()); |
| 163 | + |
| 164 | + if (item.getSize() < 2000 * 1024) { |
| 165 | + if (item.getName().endsWith(".zip")) { |
| 166 | + item.write(uploadedFile); |
| 167 | + |
| 168 | + long total = unzippedSize(uploadedFile); |
| 169 | + s.setMessage("File uploaded"); |
| 170 | + if (total > 20 * 1024 * 1024) { |
| 171 | + s.add(ZIP_DOS, "success"); |
| 172 | + System.out.println("success"); |
| 173 | + makeMessages(s); |
| 174 | + }else{ |
| 175 | + s.setMessage("I still have plenty of free storage on the server..."); |
| 176 | + } |
| 177 | + |
| 178 | + } else { |
| 179 | + s.setMessage("Only ZIP files are accepted"); |
| 180 | + } |
| 181 | + } else { |
| 182 | + s.setMessage("Only up to 2 MB files are accepted"); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + } |
| 188 | + Form form = new Form(getFormAction(), Form.POST).setName("form") |
| 189 | + .setEncType("multipart/form-data"); |
| 190 | + |
| 191 | + form.addElement(createContent(s)); |
| 192 | + |
| 193 | + setContent(form); |
| 194 | + |
| 195 | + } catch (Exception e) { |
| 196 | + e.printStackTrace(System.out); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private long unzippedSize(File uploadedFile) throws ZipException, |
| 201 | + IOException { |
| 202 | + ZipFile zf = new ZipFile(uploadedFile); |
| 203 | + |
| 204 | + long total = 0; |
| 205 | + Enumeration e = zf.entries(); |
| 206 | + while (e.hasMoreElements()) { |
| 207 | + ZipEntry entry = (ZipEntry) e.nextElement(); |
| 208 | + |
| 209 | + total += entry.getSize(); |
| 210 | + |
| 211 | + } |
| 212 | + return total; |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | +} |
0 commit comments