Slide 1

Slide 1 text

M a s h i n g U p Q A a n d S e c u r i t y Craig Stuntz Improving https://speakerdeck.com/craigstuntz https://github.com/CraigStuntz/Fizil

Slide 2

Slide 2 text

https://www.flickr.com/photos/futureshape/566200801

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

https://what-if.xkcd.com/49/

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

S o f t w a r e C o r r e c t n e s s

Slide 9

Slide 9 text

M a n u a l A n a l y s i s

Slide 10

Slide 10 text

U n d e f i n e d B e h a v i o r

Slide 11

Slide 11 text

I m p l e m e n t i n g T h i s S t u f f

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

20 TB SWF files from Google index https://security.googleblog.com/2011/08/fuzzing-at-scale.html

Slide 15

Slide 15 text

1 week run time on 2000 cores to find minimal set of 20000 SWF files https://security.googleblog.com/2011/08/fuzzing-at-scale.html

Slide 16

Slide 16 text

3 weeks run time on 2000 cores with mutated inputs https://security.googleblog.com/2011/08/fuzzing-at-scale.html

Slide 17

Slide 17 text

㱺 400 unique crash signatures https://security.googleblog.com/2011/08/fuzzing-at-scale.html

Slide 18

Slide 18 text

㱺 106 distinct security bugs https://security.googleblog.com/2011/08/fuzzing-at-scale.html

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

https://www.flickr.com/photos/sloth_rider/392367929

Slide 22

Slide 22 text

https://commons.wikimedia.org/wiki/File:ACT_recycling_truck.jpg

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Fizil AFL Runs on Windows ✅ There’s a fork Runs on Unix ❌ ✅ Fast ❌ ✅ Bunnies! ❌ Process models In Process, Out of Process Fork Server, Out of Process Instrumentation guided Soon? ✅ Automatic instrumentation .NET Assemblies Clang, GCC, Python Rich suite of fuzzing strategies Getting there! ✅ Automatically disables crash reporting ✅ ❌ Rich tooling ❌ ✅ Proven track record ❌ ✅ Stable ❌ ✅ License Apache 2.0 Apache 2.0

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

https://unsplash.com/search/bug?photo=emTCWiq2txk

Slide 30

Slide 30 text

F u z z i n g https://commons.wikimedia.org/wiki/File:Rabbit_american_fuzzy_lop_buck_white.jpg

Slide 31

Slide 31 text

{ "a" : "bc" }

Slide 32

Slide 32 text

A B D C E

Slide 33

Slide 33 text

A B D C E

Slide 34

Slide 34 text

{ "a" : "bc" } ✅

Slide 35

Slide 35 text

{ "a" : "bc" }

Slide 36

Slide 36 text

{ "a" : "bc" } |

Slide 37

Slide 37 text

A B D C E

Slide 38

Slide 38 text

A B D C E

Slide 39

Slide 39 text

| "a" : "bc" } ❌

Slide 40

Slide 40 text

https://www.flickr.com/photos/29278394@N00/696701369

Slide 41

Slide 41 text

I m p o s s i b l e ? Or just really, amazingly difficult? https://commons.wikimedia.org/wiki/File:Impossible_cube_illusion_angle.svg

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

https://xkcd.com/1316/

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

E x p l o r a t o r y https://dojo.ministryoftesting.com/lessons/exploratory-testing-an-api

Slide 48

Slide 48 text

S e c u r i t y ⊇ Q A ?

Slide 49

Slide 49 text

Behavior

Slide 50

Slide 50 text

Behavior Specification

Slide 51

Slide 51 text

Behavior Specification

Slide 52

Slide 52 text

Behavior Specification

Slide 53

Slide 53 text

Behavior Specification

Slide 54

Slide 54 text

Behavior Specification

Slide 55

Slide 55 text

P e o p l e https://www.flickr.com/photos/wocintechchat/25677176162/

Slide 56

Slide 56 text

http://amanda.secured.org/in-securities-comic/

Slide 57

Slide 57 text

https://www.quora.com/What-qualities-make-a-good-QA-engineer —Thomas Peham

Slide 58

Slide 58 text

T o o l s https://commons.wikimedia.org/wiki/ File:Tools,_arsenical_copper,_Naxos,_2700%E2%80%932200_BC,_BM,_GR_1969.12-31,_142703.jpg

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

S i m p l e T e s t i n g https://www.usenix.org/system/files/conference/osdi14/osdi14-paper-yuan.pdf

Slide 62

Slide 62 text

https://laurent22.github.io/so-injections/

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

https://en.wikipedia.org/wiki/File:Row_hammer.svg

Slide 65

Slide 65 text

S p e c i f i c a t i o n s https://lorinhochstein.wordpress.com/2014/06/04/crossing-the-river-with-tla/

Slide 66

Slide 66 text

[] let testReadDoubleWithExponent() = let actual = parseString "10.0e1" actual |> shouldEqual (Parsed (JsonNumber "10.0e1"))

Slide 67

Slide 67 text

let toHexString (bytes: byte[]) : string = //...

Slide 68

Slide 68 text

http://d3s.mff.cuni.cz/research/seminar/download/2010-02-23-Tobies-HypervisorVerification.pdf

Slide 69

Slide 69 text

Best Known Example Released Informal Spec Formal Spec Execute QuickCheck 1999 “Reversing a list twice should result in the same list” prop_RevRev xs = reverse (reverse xs) == xs where types = xs::[Int] Main> quickCheck prop_RevRev OK, passed 100 tests.

Slide 70

Slide 70 text

Best Known Example Released Informal Spec Formal Spec Execute QuickCheck 1999 “Reversing a list twice should result in the same list” prop_RevRev xs = reverse (reverse xs) == xs where types = xs::[Int] Main> quickCheck prop_RevRev OK, passed 100 tests.

Slide 71

Slide 71 text

Best Known Example Released Informal Spec Formal Spec Execute QuickCheck 1999 “Reversing a list twice should result in the same list” prop_RevRev xs = reverse (reverse xs) == xs where types = xs::[Int] Main> quickCheck prop_RevRev OK, passed 100 tests. AFL 2007 “System under test shouldn’t crash no matter what I pass to it” if (WIFSIGNALED(status) #$ !stop_soon) { kill_signal = WTERMSIG(status); return FAULT_CRASH; } ./afl-fuzz -i testcase_dir -o findings_dir -- \ /path/to/tested/program [&&.program's cmdline&&.]

Slide 72

Slide 72 text

Best Known Example Released Informal Spec Formal Spec Execute QuickCheck 1999 “Reversing a list twice should result in the same list” prop_RevRev xs = reverse (reverse xs) == xs where types = xs::[Int] Main> quickCheck prop_RevRev OK, passed 100 tests. AFL 2007 “System under test shouldn’t crash no matter what I pass to it” if (WIFSIGNALED(status) #$ !stop_soon) { kill_signal = WTERMSIG(status); return FAULT_CRASH; } ./afl-fuzz -i testcase_dir -o findings_dir -- \ /path/to/tested/program [&&.program's cmdline&&.]

Slide 73

Slide 73 text

https://www.flickr.com/photos/x1brett/2279939232

Slide 74

Slide 74 text

https://medium.com/@betable/tifu-by-using-math-random-f1c308c4fd9d

Slide 75

Slide 75 text

Thought Experiment: W h a t I f A u t o m a t e d T e s t s W e r e P e r f e c t ?

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

W h a t I f S e c u r i t y A n a l y s i s T o o l s W e r e P e r f e c t ?

Slide 79

Slide 79 text

–DNI James Clapper “Something like 90 percent of cyber intrusions start with phishing… Somebody always falls for it.” https://twitter.com/ODNIgov/status/776070411482193920

Slide 80

Slide 80 text

https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45366.pdf

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Manual Testing Examples Exploratory testing, Binary analysis Effort Very high Killer App Finding cases where code technically correct but fails at human- computer interaction Major Disadvantage Often misused

Slide 83

Slide 83 text

Dynamic Analysis Examples QuickCheck, AFL, sqlmap Effort Low Killer App More like an app killer, amiright? Major Disadvantage Tends to find a few specific (though important!) bugs

Slide 84

Slide 84 text

Static Analysis Examples FxCop, FindBugs, Coverity, Veracode Effort Very low Killer App Cheaper than air. Just do it. Major Disadvantage Limited to finding a few hundred important kinds of bugs

Slide 85

Slide 85 text

Formal Verification / Symbolic Execution Examples VCC, TLA+, Cryptol Effort High effort but correspondingly high return Killer App MiTLS, Hyper-V Memory Manager Major Disadvantage Hard to find people with skill set

Slide 86

Slide 86 text

Program Synthesis Examples Nothing off the shelf, really, but Agda and Z3 help Effort PhD-level research Killer App Elimination of incidental complexity Major Disadvantage Doesn’t really exist in general form

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

How Amazon Web Services Uses Formal Methods “Formal methods are a big success at AWS, helping us prevent subtle but serious bugs from reaching production, bugs we would not have found through any other technique. They have helped us devise aggressive optimizations to complex algorithms without sacrificing quality.” http://research.microsoft.com/en-us/um/people/lamport/tla/amazon.html

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

“Finding and Understanding Bugs in C Compilers,” Yang et al. https://www.flux.utah.edu/paper/yang-pldi11

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

=================================== Technical "whitepaper" for afl-fuzz =================================== This document provides a quick overview of the guts of American Fuzzy Lop. See README for the general instruction manual; and for a discussion of motivations and design goals behind AFL, see historical_notes.txt. 0) Design statement ------------------- American Fuzzy Lop does its best not to focus on any singular principle of operation and not be a proof-of-concept for any specific theory. The tool can be thought of as a collection of hacks that have been tested in practice, found to be surprisingly effective, and have been implemented in the simplest, most robust way I could think of at the time. Many of the resulting features are made possible thanks to the availability of lightweight instrumentation that served as a foundation for the tool, but this mechanism should be thought of merely as a means to an end. The only true governing principles are speed, reliability, and ease of use. 1) Coverage measurements ------------------------ The instrumentation injected into compiled programs captures branch (edge) coverage, along with coarse branch-taken hit counts. The code injected at branch points is essentially equivalent to: cur_location = ; shared_mem[cur_location ^ prev_location]++; http://lcamtuf.coredump.cx/afl/technical_details.txt

Slide 95

Slide 95 text

M e m o r y

Slide 96

Slide 96 text

{ "a" : "bc" }

Slide 97

Slide 97 text

let jsonNetResult = try JsonConvert.DeserializeObject(str) |> ignore Success with | :? JsonReaderException as jre -> jre.Message |> Error | :? JsonSerializationException as jse -> jse.Message |> Error | :? System.FormatException as fe -> if fe.Message.StartsWith("Invalid hex character”) // hard coded in Json.NET then fe.Message |> Error else reraise() ⃪ T est ⬑ Special case error stuff

Slide 98

Slide 98 text

use proc = new Process() proc.StartInfo.FileName <- executablePath inputMethod.BeforeStart proc testCase.Data proc.StartInfo.UseShellExecute <- false proc.StartInfo.RedirectStandardOutput <- true proc.StartInfo.RedirectStandardError <- true proc.StartInfo.EnvironmentVariables.Add(SharedMemory.environmentVariableName, sharedMemoryName) let output = new System.Text.StringBuilder() let err = new System.Text.StringBuilder() proc.OutputDataReceived.Add(fun args -> output.Append(args.Data) |> ignore) proc.ErrorDataReceived.Add (fun args -> err.Append(args.Data) |> ignore) proc.Start() |> ignore inputMethod.AfterStart proc testCase.Data proc.BeginOutputReadLine() proc.BeginErrorReadLine() proc.WaitForExit() let exitCode = proc.ExitCode let crashed = exitCode = WinApi.ClrUnhandledExceptionCode ⃪ Set up ⃪ Read results ⃪ Important bit

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

/// An ordered list of functions to use when starting with a single piece of /// example data and producing new examples to try let private allStrategies = [ bitFlip 1 bitFlip 2 bitFlip 4 byteFlip 1 byteFlip 2 byteFlip 4 arith8 arith16 arith32 interest8 interest16 ]

Slide 101

Slide 101 text

let totalBits = bytes.Length * 8 let testCases = seq { for bit = 0 to totalBits - flipBits do let newBytes = Array.copy bytes let firstByte = bit / 8 let firstByteMask, secondByteMask = bitMasks(bit, flipBits) let newFirstByte = bytes.[firstByte] ^^^ firstByteMask newBytes.[firstByte] <- newFirstByte let secondByte = firstByte + 1 if secondByteMask <> 0uy && secondByte < bytes.Length then let newSecondByte = bytes.[secondByte] ^^^ secondByteMask newBytes.[secondByte] <- newSecondByte yield newBytes } Fuzz one byte → ^^^ means xor ↓

Slide 102

Slide 102 text

https://commons.wikimedia.org/wiki/File:CPT-Recursion-Factorial-Code.svg

Slide 103

Slide 103 text

private static void F(string arg) { Console.WriteLine("f"); Console.Error.WriteLine("Error!"); Environment.Exit(1); }

Slide 104

Slide 104 text

private static void F(string arg) { instrument.Trace(29875); Console.WriteLine("f"); Console.Error.WriteLine("Error!"); Environment.Exit(1); } ← Random number

Slide 105

Slide 105 text

private static void F(string arg) { #if MANUAL_INSTRUMENTATION instrument.Trace(29875); #endif Console.WriteLine("f"); Console.Error.WriteLine("Error!"); Environment.Exit(1); }

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

let stringify (ob: obj) : string = JsonConvert.SerializeObject(ob)

Slide 109

Slide 109 text

let stringify (ob: obj) : string = JsonConvert.SerializeObject(ob) // Method: System.String\u0020Program::stringify(System.Object) .body stringify { arg_02_0 [generated] arg_07_0 [generated] nop() arg_02_0 = ldloc(ob) arg_07_0 = call(JsonConvert::SerializeObject, arg_02_0) ret(arg_07_0) }

Slide 110

Slide 110 text

let stringify (ob: obj) : string = JsonConvert.SerializeObject(ob) // Method: System.String\u0020Program::stringify(System.Object) .body stringify { arg_02_0 [generated] arg_07_0 [generated] nop() arg_02_0 = ldloc(ob) arg_07_0 = call(JsonConvert::SerializeObject, arg_02_0) ret(arg_07_0) } // Method: System.String\u0020Program::stringify(System.Object) .body stringify { arg_05_0 [generated] arg_0C_0 [generated] arg_11_0 [generated] arg_05_0 = ldc.i4(23831) call(Instrument::Trace, arg_05_0) nop() arg_0C_0 = ldloc(ob) arg_11_0 = call(JsonConvert::SerializeObject, arg_0C_0) ret(arg_11_0) }

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

http://www.json.org/

Slide 114

Slide 114 text

https://tools.ietf.org/html/rfc4627

Slide 115

Slide 115 text

http://www.ecma-international.org/ecma-262/5.1/#sec-15.12

Slide 116

Slide 116 text

http://www.ecma-international.org/publications/standards/Ecma-404.htm

Slide 117

Slide 117 text

https://tools.ietf.org/html/rfc7158

Slide 118

Slide 118 text

https://tools.ietf.org/html/rfc7159

Slide 119

Slide 119 text

https://github.com/nst/STJSON

Slide 120

Slide 120 text

https://github.com/CraigStuntz/Fizil/blob/master/StJson/StJsonParser.fs

Slide 121

Slide 121 text

Standard Accepts, Json.NET Rejects Value 88888888888888888888888888888888888888888888888888 88888888888888888888888888888888888888888888888888 88888888888888888888888888888888888888888888888888 88888888888888888888888888888888888888888888888888 88888888888888888888888888888888888888888888888888 Standard Says No limit Json.NET MaximumJavascriptIntegerCharacterLength = 380;

Slide 122

Slide 122 text

Standard Rejects, Json.NET Accepts Value [,,,] Standard Says A JSON value MUST be an object, array, number, or string, or one of the following three literal names: false null true Json.NET [null, null, null, null]

Slide 123

Slide 123 text

I m p l e m e n t a t i o n D e t a i l s

Slide 124

Slide 124 text

let private insertTraceInstruction(ilProcessor: ILProcessor, before: Instruction, state) = let compileTimeRandom = state.Random.Next(0, UInt16.MaxValue |> Convert.ToInt32) let ldArg = ilProcessor.Create(OpCodes.Ldc_I4, compileTimeRandom) let callTrace = ilProcessor.Create(OpCodes.Call, state.Trace) ilProcessor.InsertBefore(before, ldArg) ilProcessor.InsertAfter (ldArg, callTrace) This margin is too narrow to contain a try/finally example, so see: https://goo.gl/W4y7JH

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

let private removeStrongName (assemblyDefinition : AssemblyDefinition) = let name = assemblyDefinition.Name; name.HasPublicKey <- false; name.PublicKey <- Array.empty; assemblyDefinition.Modules |> Seq.iter ( fun moduleDefinition -> moduleDefinition.Attributes <- moduleDefinition.Attributes &&& ~~~ModuleAttributes.StrongNameSigned) let aptca = assemblyDefinition.CustomAttributes.FirstOrDefault( fun attr -> attr.AttributeType.FullName = typeof.FullName) assemblyDefinition.CustomAttributes.Remove aptca |> ignore assembly.MainModule.AssemblyReferences |> Seq.filter (fun reference -> Set.contains reference.Name assembliesToInstrument) |> Seq.iter (fun reference -> reference.PublicKeyToken <- null )

Slide 127

Slide 127 text

I n / O u t o f P r o c e s s

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

–ECMA-335, Common Language Infrastructure (CLI), Partition I “If marked BeforeFieldInit then the type’s initializer method is executed at, or sometime before, first access to any static field defined for that type.”

Slide 131

Slide 131 text

f ( x ) = f ( x ) t i m e ( f ( x ) ) ! = t i m e ( f ( x ) ) ✅ ❌

Slide 132

Slide 132 text

U n i c o d e Original JSON { "a": "bc" } ASCII Bytes 7B 20 22 61 22 20 3A 20 22 62 63 22 20 7D UTF-8 with Byte Order Mark EF BB BF 7B 20 22 61 22 20 3A 20 22 62 63 22 20 7D UTF-16 BE with BOM FE FF 00 7B 00 20 00 22 00 61 00 22 00 20 00 3A 00 20 00 22 00 62 00 63 00 22 00 20 00 7D

Slide 133

Slide 133 text

T h a n k Y o u ! Presentation Review Cassandra Faris Chad James Damian Synadinos Doug Mair Tommy Graves Source Code Inspiration Michał Zalewski Nicolas Seriot Everyone Who Works on dnSpy & Mono.Cecil

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

C r a i g S t u n t z @craigstuntz [email protected] http://www.craigstuntz.com http://www.meetup.com/Papers-We-Love-Columbus/ https://speakerdeck.com/craigstuntz