-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> | ||
<%@ page import="inc.insecure.*" %> | ||
<%@ page import="insecure.inc.Constants" %> | ||
<%@ page import="insecure.inc.Util" %> | ||
<% | ||
String alertVisibility = "hidden"; | ||
String query = request.getParameter("query"); | ||
if (query != null) { | ||
query = query.trim(); | ||
alertVisibility = ""; | ||
} | ||
String result = Util.executeJavascript(query); | ||
if (result == "solved") { | ||
session.setAttribute(Constants.CHALLENGE_ID,"cwe94"); | ||
response.sendRedirect(Constants.SECRET_PAGE); | ||
} | ||
%> | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<title>Improper Control of Generation of Code ('Code Injection')</title> | ||
<link rel="stylesheet" href="public/bootstrap/css/bootstrap.min.css"> | ||
<script src="public/jquery.min.js"></script> | ||
<script src="public/bootstrap/js/bootstrap.min.js"></script> | ||
|
||
</head> | ||
<body> | ||
<nav class="navbar navbar-inverse"> | ||
<div class="container-fluid"> | ||
<div class="navbar-header"> | ||
<a class="navbar-brand" href="index.jsp">Insecure Inc.</a> | ||
</div> | ||
<ul class="nav navbar-nav"> | ||
<li class="active"><a href="#">Improper Control of Generation of Code ('Code Injection')</a></li> | ||
</ul> | ||
</div> | ||
</nav> | ||
<div class="container"> | ||
<h1>Welcome to CWE94 - Improper Control of Generation of Code ('Code Injection')!</h1> | ||
<p>Please enter your mathematical operation and the backend JavaScript engine will provide the result.</p> | ||
<form action="cwe94.jsp" autocomplete="off" method="POST"> | ||
<div class="form-group"> | ||
<label for="expression">Mathematical Expression:</label> | ||
<input type="text" class="form-control" id="expression" name="query"> | ||
</div> | ||
<input type="submit" id="submit" class="btn" value="Submit"> | ||
<br><br> | ||
<div class="alert alert-danger <%=alertVisibility%>"> | ||
Result: <%=result%> | ||
</div> | ||
</form> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<p> | ||
The purpose of this challenge is to demonstrate the MITRE Top 25 programming flaw: 'Improper Control of Generation of Code ('Code Injection')'. | ||
<br><br> | ||
|
||
<blockquote> | ||
<p> | ||
<i>The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.</i> | ||
</p> | ||
<footer>From MITRE <a target="_blank" rel="noopener noreferrer" href="https://cwe.mitre.org/data/definitions/94.html">CWE 94</a></footer> | ||
</blockquote> | ||
<p> | ||
The developer of this part of the site has implemented a server side calculator that expects a mathematical expression from the user to calculate. | ||
The issue is that the server side code runs the user input through eval(), which means that any input is treated as executable code. | ||
</p> | ||
<p> | ||
Below is a portion of the application code. You will see the developers are also loading some other unrelated backend utility code into the runtime environment. See if you can determine a way to exploit this. | ||
</p> | ||
<p> | ||
Your task is to invoke the existing `deleteHistory` utility loaded into the runtime environment, likely causing issues to data stored somewhere on the server side. | ||
</p> | ||
<pre class="pre-scrollable"> | ||
public static String calculate(String mathematicalExpression) { | ||
ScriptEngineManager manager = new ScriptEngineManager(); | ||
ScriptEngine engine = manager.getEngineByName("JavaScript"); | ||
String result = ""; | ||
|
||
engine.eval("var password = '" + PASSWORD + "';"); | ||
engine.eval("function deleteHistory(password) { if (authenticate(password)) { clearDatabase(); } else { return 'wrong password'; } }"); | ||
Object outcome = engine.eval(mathematicalExpression); | ||
|
||
if (outcome != null) { | ||
result = command + " = " + outcome.toString(); | ||
} | ||
|
||
return result; | ||
} | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
### Solution for "Improper Control of Generation of Code ('Code Injection')" challenge | ||
|
||
When a system inserts external input into an engine that is capable of executing code, the input can exploit this fact by executing code that will accomplish some goal unintended by the system creator. | ||
External input should never be trusted as it may actually be executable code. | ||
Always apply server side input validation to external input to help protect against code injection. | ||
|
||
To pass this challenge: | ||
|
||
- Become familiar with the Insecure Inc. Calculator and study the code snippet in the challenge description. | ||
- Obtain the value for the password that is loaded into the runtime environment. | ||
- Invoke the `deleteHistory` utility, providing the correct password. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters