1 /* 2 * Copyright 2023 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example; 18 19 import static java.util.Arrays.asList; 20 import static org.junit.jupiter.params.provider.Arguments.arguments; 21 22 import com.code_intelligence.jazzer.junit.FuzzTest; 23 import com.code_intelligence.jazzer.mutation.annotation.NotNull; 24 import java.nio.charset.StandardCharsets; 25 import java.security.MessageDigest; 26 import java.security.NoSuchAlgorithmException; 27 import java.util.Base64; 28 import java.util.List; 29 import java.util.stream.Stream; 30 import org.junit.jupiter.params.provider.Arguments; 31 import org.junit.jupiter.params.provider.MethodSource; 32 33 class JavaSeedFuzzTest { 34 // Generated via: 35 // printf 'tH15_1S-4_53Cr3T.fl4G' | openssl dgst -binary -sha256 | openssl base64 -A 36 // Luckily the fuzzer can't read comments ;-) 37 private static final byte[] FLAG_SHA256 = 38 Base64.getDecoder().decode("q0vPdz5oeJIW3k2U4VJ+aWDufzzZbKAcevc9cNoUTSM="); 39 fuzzTheFlag()40 static Stream<Arguments> fuzzTheFlag() { 41 return Stream.of(arguments(asList("red", "herring"), 0), 42 // This argument passes the hash check, but does not trigger the finding right away. This 43 // is meant to verify that the seed ends up in the corpus, serving as the base for future 44 // mutations rather than just being executed once. 45 arguments(asList("tH15_1S", "-4_53Cr3T", ".fl4G"), 42)); 46 } 47 48 @MethodSource 49 @FuzzTest fuzzTheFlag(@otNull List<@NotNull String> flagParts, int secret)50 void fuzzTheFlag(@NotNull List<@NotNull String> flagParts, int secret) 51 throws NoSuchAlgorithmException { 52 byte[] hash = MessageDigest.getInstance("SHA-256").digest( 53 String.join("", flagParts).getBytes(StandardCharsets.UTF_8)); 54 if (MessageDigest.isEqual(hash, FLAG_SHA256) && secret == 1337) { 55 throw new Error("Fl4g 4nd s3cr3et f0und!"); 56 } 57 } 58 } 59