Skip to content

Commit

Permalink
Hook up random lesson. A little too hard on the tutorial. Might need …
Browse files Browse the repository at this point in the history
…to ease up on the RNG.
  • Loading branch information
fervidnerd committed Aug 14, 2012
1 parent 5c51d78 commit ef4b578
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 17 deletions.
27 changes: 19 additions & 8 deletions WebGoat/App_Code/WeakRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@ namespace OWASP.WebGoat.NET.App_Code
public class WeakRandom
{
private uint _seed = 7;


public WeakRandom() {}

public WeakRandom(uint seed)
{
//Get a first high number so it looks random.
if (seed < 100)
_seed = _seed ^ 3;
else
_seed = seed;
_seed = seed;
}

public uint Next(uint min, uint max)
{
if (min >= max)
throw new Exception("Min must be smaller than max");

unchecked
unchecked //Just use next number from overflow
{
_seed = _seed ^ 2;
_seed = _seed * _seed + _seed;
}

return _seed % (max - min) + min;
}

public uint Peek(uint min, uint max)
{
if (min >= max)
throw new Exception("Min must be smaller than max");

unchecked //Just use next number from overflow
{
var seed = _seed * _seed + _seed;

return seed % (max - min) + min;
}
}
}
}
15 changes: 14 additions & 1 deletion WebGoat/Content/Random.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,21 @@
such as password salts, SSL handshakes etc.</p>

<p>In the following example, try to predict the next number in the sequence:</p>

<p><asp:Label ID="lblSequence" runat="server" /></p>

<table>
<tr>
<td><asp:Button ID="btnOneMore" runat="server" onclick="btnOneMore_Click" Text="Generate number!" /></td>
<td><asp:Button ID="btnReset" Text="Reset" onclick="btnReset_Click" runat="server" /></td>
</tr>
</table>

<p>The next number is: <asp:TextBox ID="txtNextNumber" runat="server" />
<asp:Button ID="btnGo" Text="Go!" runat="server" onclick="btnGo_Click" /></p>

<p><asp:Label ID="lblResult" runat="server" /></p>

<p>-- Sequence is created here...</p>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HelpContentPlaceholder" runat="server">
Expand Down
76 changes: 71 additions & 5 deletions WebGoat/Content/Random.aspx.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,78 @@

using System;
using System.Web;
using System.Web.UI;
using OWASP.WebGoat.NET.App_Code;
using System.Collections.Generic;
using System.Text;

namespace OWASP.WebGoat.NET.Content
{
public partial class Random : System.Web.UI.Page
{
}
}
private const uint MIN = 1;
private const uint MAX = 1000;
private const int INIT_NUMBERS = 5;

public void Page_Load(object sender, EventArgs args)
{
if (Session["Random"] == null)
Reset();

IList<uint> numbers = (IList<uint>) Session["Numbers"];
lblSequence.Text = "Sequence: " + Print(numbers);
}

public void btnOneMore_Click(object sender, EventArgs args)
{
WeakRandom rnd = (WeakRandom) Session["Random"];
IList<uint> numbers = (IList<uint>) Session["Numbers"];

numbers.Add(rnd.Next(MIN, MAX));

lblSequence.Text = "Sequence: " + Print(numbers);
}

public void btnGo_Click(object sender, EventArgs args)
{
WeakRandom rnd = (WeakRandom) Session["Random"];
IList<uint> numbers = (IList<uint>) Session["Numbers"];

uint next = rnd.Peek(MIN, MAX);

if (txtNextNumber.Text == next.ToString())
lblResult.Text = "You found it!";
else
lblResult.Text = "Sorry please try again.";
}

public void btnReset_Click(object sender, EventArgs args)
{
Reset();

IList<uint> numbers = (IList<uint>) Session["Numbers"];
lblSequence.Text = "Sequence: " + Print(numbers);
}

private string Print(IList<uint> numbers)
{
StringBuilder strBuilder = new StringBuilder();

foreach(uint n in numbers)
strBuilder.AppendFormat("{0}, ", n);

return strBuilder.ToString();
}

public void Reset()
{
Session["Random"] = new WeakRandom();

var rnd = (WeakRandom) Session["Random"];

IList<uint> numbers = new List<uint>();

for(int i=0; i<INIT_NUMBERS; i++)
numbers.Add(rnd.Next(MIN, MAX));

Session["Numbers"] = numbers;
}
}
}
10 changes: 7 additions & 3 deletions WebGoat/Content/Random.aspx.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef4b578

Please sign in to comment.