Skip to content

Commit e471816

Browse files
committed
Add oracle delay provided by a command line arugment
1 parent 19f723e commit e471816

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

Program.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
using System.Diagnostics;
1+
using System.Net.Mime;
2+
using System.Linq;
3+
using System.Diagnostics;
24
using System;
35
using System.Text;
6+
using Mono.Options;
47

58
namespace Padding_Oracle_Attack
69
{
710
class PaddingOracleAttack
811
{
912
private static RemoteServerMock server = new RemoteServerMock();
1013

11-
public static void Main()
14+
public static void Main(String[] args)
1215
{
13-
Console.WriteLine("Enter plaintext:");
16+
Console.WriteLine("~~ Padding Oracle Attack Demo ~~");
17+
18+
HandleConfigurationArguments(args);
19+
20+
Console.WriteLine("Oracle response delay set to {0} ms.", server.OracleDelayMilliseconds);
21+
22+
Console.WriteLine("\nEnter plaintext:");
1423
string plaintext = Console.ReadLine();
1524

1625
byte[] encrypted = server.Encrypt(plaintext);
1726
var blocks = ByteUtils.SliceIntoBlocks(encrypted);
1827

1928
Console.WriteLine("\nCiphertext blocks (base64):\n{0}", String.Join("\n", blocks.ConvertAll(block => Convert.ToBase64String(block))));
29+
2030
Console.WriteLine("\nPadding oracle attack results:");
2131
Console.WriteLine("(first block cannot be decrypted)");
2232

@@ -36,12 +46,39 @@ public static void Main()
3646
var decodedBlocksCount = blocks.Count - 1;
3747
Console.WriteLine("\nDecoded {0} blocks.", decodedBlocksCount);
3848

39-
if (decodedBlocksCount > 0) {
49+
if (decodedBlocksCount > 0)
50+
{
4051
var timeElapsed = stopwatch.Elapsed;
41-
Console.WriteLine("Time elapsed: {0}, avg {1:0.0} ms per block", timeElapsed.ToString(), timeElapsed.Divide(decodedBlocksCount).TotalMilliseconds);
52+
Console.WriteLine("Time elapsed: {0}, avg {1:0.000} s per block", timeElapsed.ToString(), timeElapsed.Divide(decodedBlocksCount).TotalMilliseconds / 1000);
4253
}
4354
}
4455

56+
private static void HandleConfigurationArguments(String[] args)
57+
{
58+
OptionSet arguments = new OptionSet();
59+
arguments.Add("d|delay=", "oracle delay in milliseconds for each padding request", (uint d) => server.OracleDelayMilliseconds = d);
60+
arguments.Add("h|help", "displays this message", _ => {
61+
arguments.WriteOptionDescriptions(Console.Out);
62+
Environment.Exit(0);
63+
});
64+
65+
try
66+
{
67+
var rest = arguments.Parse(args);
68+
if (rest.Count == 0) {
69+
return;
70+
}
71+
Console.WriteLine("Unrecognized arguments: {0}", String.Join(",", rest));
72+
}
73+
catch (OptionException e)
74+
{
75+
Console.WriteLine(e.Message);
76+
}
77+
78+
arguments.WriteOptionDescriptions(Console.Out);
79+
Environment.Exit(1);
80+
}
81+
4582
private static string DecryptBlock(byte[] block, byte[] previousBlock)
4683
{
4784
byte[] decrypted = new byte[block.Length];

RemoteServerMock.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Threading;
12
using System.IO;
23
using System.Security.Cryptography;
34

@@ -6,6 +7,7 @@ namespace Padding_Oracle_Attack
67
class RemoteServerMock
78
{
89
private Aes aesAlg = Aes.Create();
10+
public uint OracleDelayMilliseconds { get; set; } = 0;
911

1012
public RemoteServerMock()
1113
{
@@ -37,6 +39,11 @@ public byte[] Encrypt(string plaintext)
3739

3840
public bool IsPaddingCorrect(byte[] ciphertext)
3941
{
42+
if (OracleDelayMilliseconds > 0)
43+
{
44+
Thread.Sleep((int)OracleDelayMilliseconds);
45+
}
46+
4047
try
4148
{
4249
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

padding-oracle-attack.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
<TargetFramework>netcoreapp2.0</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="Mono.Options" Version="5.3.0.1" />
10+
</ItemGroup>
11+
812
</Project>

0 commit comments

Comments
 (0)