commit 83c6e7627d18358b29e639184cf2c10680d71aac Author: youys <1272586223@qq.com> Date: Mon Mar 13 17:30:01 2023 +0800 init diff --git a/Buffer Copy Without Checking Size of Input/Buffer Copy Without Checking Size of Input.docx b/Buffer Copy Without Checking Size of Input/Buffer Copy Without Checking Size of Input.docx new file mode 100644 index 0000000..5437044 Binary files /dev/null and b/Buffer Copy Without Checking Size of Input/Buffer Copy Without Checking Size of Input.docx differ diff --git a/Buffer Copy Without Checking Size of Input/code files/mitigated.cpp b/Buffer Copy Without Checking Size of Input/code files/mitigated.cpp new file mode 100644 index 0000000..c2df2d1 --- /dev/null +++ b/Buffer Copy Without Checking Size of Input/code files/mitigated.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() { + + + +char UserType[11]; // need one more than 10 for the end marker + + std::cout << "Please enter your name (less than 10 characters): " << '\n'; + + std::cin.getline(UserType, 11); // limit input to 10 + + std::cout << UserType << '\n'; + + + + +} \ No newline at end of file diff --git a/Buffer Copy Without Checking Size of Input/code files/vulnerable.cpp b/Buffer Copy Without Checking Size of Input/code files/vulnerable.cpp new file mode 100644 index 0000000..dba6583 --- /dev/null +++ b/Buffer Copy Without Checking Size of Input/code files/vulnerable.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() { + char name[10]; + + cout << "Please enter your name: "; + cin >> name; + + + + cout << "Your name is: " << name << endl; + + +} \ No newline at end of file diff --git a/Improper restriction of excessive authentication attemps/Improper restriction of excessive authentication attempts.docx b/Improper restriction of excessive authentication attemps/Improper restriction of excessive authentication attempts.docx new file mode 100644 index 0000000..78f5e2a Binary files /dev/null and b/Improper restriction of excessive authentication attemps/Improper restriction of excessive authentication attempts.docx differ diff --git a/Improper restriction of excessive authentication attemps/Mitigated/LogIn.java b/Improper restriction of excessive authentication attemps/Mitigated/LogIn.java new file mode 100644 index 0000000..8e51feb --- /dev/null +++ b/Improper restriction of excessive authentication attemps/Mitigated/LogIn.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + + +public class LogIn { + + +public static void main(String[] args) { + +//declare variables +String username, password; +int n = 0; + +Scanner scanner = new Scanner (System.in); + + while (n<3) { + + //prompt user for their username + System.out.print("Enter your username: "); + username = scanner.nextLine(); + + //prompt user for their password + System.out.print("Enter your password: "); + password = scanner.nextLine(); + + + if ("Justinfromcharleston@gmail.com".equals(username) && "secret17".equals(password)) + { System.out.println("Welcome " + username + "!!!"); + break; + } + else if(!"secret17".equals(password) || !"Justinfromcharleston@gmail.com".equals(username)) { + System.out.println("Incorrect login. Please try again."); + n++; + + if (n==3){ + + System.out.print("Maximum login attemps exceeded."); + break; + } + } + + + } + } +} \ No newline at end of file diff --git a/Improper restriction of excessive authentication attemps/Vulnerable/LogIn.java b/Improper restriction of excessive authentication attemps/Vulnerable/LogIn.java new file mode 100644 index 0000000..5b979bc --- /dev/null +++ b/Improper restriction of excessive authentication attemps/Vulnerable/LogIn.java @@ -0,0 +1,41 @@ +package homework.pkg5; + +import java.util.Scanner; + + +public class LogIn{ + + +public static void main(String[] args) { + +//declare variables +String username, password; +int n = 0; + +Scanner scanner = new Scanner (System.in); + + while (n<3) { + + //prompt user for their username + System.out.print("Enter your username: "); + username = scanner.nextLine(); + + //prompt user for their password + System.out.print("Enter your password: "); + password = scanner.nextLine(); + + + if ("Justinfromcharleston@gmail.com".equals(username) && "secret17".equals(password)) + { System.out.println("Welcome " + username + "!!!"); + break; + } + else if(!"secret17".equals(password) || !"Justinfromcharleston@gmail.com".equals(username)) { + System.out.println("Incorrect login. Please try again."); + + + } + + + } + } +} \ No newline at end of file diff --git a/Integer Overflow/Integer Overflow.docx b/Integer Overflow/Integer Overflow.docx new file mode 100644 index 0000000..32d46fe Binary files /dev/null and b/Integer Overflow/Integer Overflow.docx differ diff --git a/Integer Overflow/code files/mitigated.java b/Integer Overflow/code files/mitigated.java new file mode 100644 index 0000000..e0847e0 --- /dev/null +++ b/Integer Overflow/code files/mitigated.java @@ -0,0 +1,26 @@ + +package integer.overflow; + +/** + * + * @author justi + */ +public class IntegerOverflow_Mitigated { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + + //int a is equal to the largest int available + int a = Integer.MAX_VALUE; + int b = 1; + + //calculation will result in integer overflow + int c = a + b; + System.out.println("Before: " + a + " + " + b + " = " + c); + System.out.println("After: " + Math.addExact( a, b )); + } + +} diff --git a/Integer Overflow/code files/vulnerable.java b/Integer Overflow/code files/vulnerable.java new file mode 100644 index 0000000..6541513 --- /dev/null +++ b/Integer Overflow/code files/vulnerable.java @@ -0,0 +1,26 @@ + +package integer.overflow; + +/** + * + * @author justi + */ +public class IntegerOverflow { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + + //int a is equal to the largest int available + int a = Integer.MAX_VALUE; + int b = 1; + + //calculation will result in integer overflow + int c = a + b; + System.out.println(a + " + " + b + " = " + c); + + } + +} \ No newline at end of file diff --git a/Missing Encryption of Sensitive Data/Missing Encryption of Sensitive Data.docx b/Missing Encryption of Sensitive Data/Missing Encryption of Sensitive Data.docx new file mode 100644 index 0000000..e7f41f8 Binary files /dev/null and b/Missing Encryption of Sensitive Data/Missing Encryption of Sensitive Data.docx differ diff --git a/Missing Encryption of Sensitive Data/code files/Mitigated/Password.java b/Missing Encryption of Sensitive Data/code files/Mitigated/Password.java new file mode 100644 index 0000000..8c91f8e --- /dev/null +++ b/Missing Encryption of Sensitive Data/code files/Mitigated/Password.java @@ -0,0 +1,29 @@ + +package homework.pkg4; + +import java.security.MessageDigest; +import java.util.Scanner; +import javax.xml.bind.DatatypeConverter; + +public class Password { + public static void main( String[] args ) throws Exception { + Scanner keyboard = new Scanner(System.in); + String un, pw, encrypt_psswrd; + + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + + System.out.print("Please create a username: "); + un = keyboard.nextLine(); + + System.out.print("Please create a password: "); + pw = keyboard.nextLine(); + + digest.update( pw.getBytes("UTF-8") ); + encrypt_psswrd = DatatypeConverter.printHexBinary( digest.digest() ); + + //overwrites user's original input so it is not visible to hackers. + pw = "empty"; + + System.out.println( "Your username is: " + un + ". Your password has been encryped for security purposes: " + encrypt_psswrd ); + } +} diff --git a/Missing Encryption of Sensitive Data/code files/Vulnerable/Password.java b/Missing Encryption of Sensitive Data/code files/Vulnerable/Password.java new file mode 100644 index 0000000..6e4e994 --- /dev/null +++ b/Missing Encryption of Sensitive Data/code files/Vulnerable/Password.java @@ -0,0 +1,22 @@ +package homework.pkg4; + +import java.security.MessageDigest; +import java.util.Scanner; + + +public class Password { + public static void main( String[] args ) throws Exception { + Scanner keyboard = new Scanner(System.in); + String un, pw, encrypt_psswrd; + + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + + System.out.print("Please create a username: "); + un = keyboard.nextLine(); + + System.out.print("Please create a password: "); + pw = keyboard.nextLine(); + + System.out.println( "Your username is: " + un + ". Your password is: " + pw); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..2460036 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Software-Vulnerability-Demonstrations +Software Vulnerability Demonstrations Unique and complete demonstrations of software vulnerabilities found in the CWE/SANS Top 25 vulnerabilities list. Each example contains executable code with both a vulnerable version showing the weakness and a mitigated version showing how eliminate it. Each is well-documented and includes screenshots showing the application executing step by step. These are part of what would be a much larger application. diff --git a/SQL Injection/SQL Injection.docx b/SQL Injection/SQL Injection.docx new file mode 100644 index 0000000..c81beab Binary files /dev/null and b/SQL Injection/SQL Injection.docx differ diff --git a/SQL Injection/code files/Mitigated/SQL_Injection.php b/SQL Injection/code files/Mitigated/SQL_Injection.php new file mode 100644 index 0000000..3e49809 --- /dev/null +++ b/SQL Injection/code files/Mitigated/SQL_Injection.php @@ -0,0 +1,71 @@ + +prepare("INSERT INTO students (first_name, last_name, student_id) VALUES (?, ?, ?)"); + $stmt->bind_param('sss', $_POST['first_name'], $_POST['last_name'], $_POST['student_id']); + $stmt->execute(); + $stmt->close(); + + + + //test if there was a query error + if($stmt){ + //success + echo "Success! Student added to database!"; + }else{ + //failure + die("Database query failed. " . mysqli_error($connection)); + } +?> + + + + + + +SQL Injection + + + + + + + + + + + diff --git a/SQL Injection/code files/Mitigated/index.php b/SQL Injection/code files/Mitigated/index.php new file mode 100644 index 0000000..7733e12 --- /dev/null +++ b/SQL Injection/code files/Mitigated/index.php @@ -0,0 +1,27 @@ + + + + + + +Form + + + + + + +
+
+ First name:
+ Last name:
+ Student ID:
+
+
+
+ + + + + \ No newline at end of file diff --git a/SQL Injection/code files/Vulnerable/SQL_Injection.php b/SQL Injection/code files/Vulnerable/SQL_Injection.php new file mode 100644 index 0000000..71a20e6 --- /dev/null +++ b/SQL Injection/code files/Vulnerable/SQL_Injection.php @@ -0,0 +1,77 @@ + + + + + + + + +SQL Injection + + + + + + + + + + + diff --git a/SQL Injection/code files/Vulnerable/index.php b/SQL Injection/code files/Vulnerable/index.php new file mode 100644 index 0000000..7733e12 --- /dev/null +++ b/SQL Injection/code files/Vulnerable/index.php @@ -0,0 +1,27 @@ + + + + + + +Form + + + + + + +
+
+ First name:
+ Last name:
+ Student ID:
+
+
+
+ + + + + \ No newline at end of file diff --git a/Unrestricted Upload/Unrestriced Upload.docx b/Unrestricted Upload/Unrestriced Upload.docx new file mode 100644 index 0000000..bba9815 Binary files /dev/null and b/Unrestricted Upload/Unrestriced Upload.docx differ diff --git a/Unrestricted Upload/code files/example files to be uploaded/malicious_code.php b/Unrestricted Upload/code files/example files to be uploaded/malicious_code.php new file mode 100644 index 0000000..4805c56 --- /dev/null +++ b/Unrestricted Upload/code files/example files to be uploaded/malicious_code.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/Unrestricted Upload/code files/example files to be uploaded/sdev325notes.txt b/Unrestricted Upload/code files/example files to be uploaded/sdev325notes.txt new file mode 100644 index 0000000..f13499f --- /dev/null +++ b/Unrestricted Upload/code files/example files to be uploaded/sdev325notes.txt @@ -0,0 +1 @@ +blah blah blah diff --git a/Unrestricted Upload/code files/mitigated/upload_picture.php b/Unrestricted Upload/code files/mitigated/upload_picture.php new file mode 100644 index 0000000..f620e76 --- /dev/null +++ b/Unrestricted Upload/code files/mitigated/upload_picture.php @@ -0,0 +1,54 @@ + + + + + + Upload + + + + + + + +
+

+ +
+ + diff --git a/Unrestricted Upload/code files/vulnerable/upload_picture.php b/Unrestricted Upload/code files/vulnerable/upload_picture.php new file mode 100644 index 0000000..5b68ec5 --- /dev/null +++ b/Unrestricted Upload/code files/vulnerable/upload_picture.php @@ -0,0 +1,40 @@ + + + + + + Upload + + + + + +
+

+ +
+ + diff --git a/Use of Hard-Coded Credentials/Use of Hard-Coded Credentials.docx b/Use of Hard-Coded Credentials/Use of Hard-Coded Credentials.docx new file mode 100644 index 0000000..107d52e Binary files /dev/null and b/Use of Hard-Coded Credentials/Use of Hard-Coded Credentials.docx differ diff --git a/Use of Hard-Coded Credentials/code files/Mitigated/SQL_Injection.php b/Use of Hard-Coded Credentials/code files/Mitigated/SQL_Injection.php new file mode 100644 index 0000000..cd94022 --- /dev/null +++ b/Use of Hard-Coded Credentials/code files/Mitigated/SQL_Injection.php @@ -0,0 +1,63 @@ + +prepare("INSERT INTO students (first_name, last_name, student_id) VALUES (?, ?, ?)"); + $stmt->bind_param('sss', $_POST['first_name'], $_POST['last_name'], $_POST['student_id']); + $stmt->execute(); + $stmt->close(); + + + + //test if there was a query error + if($stmt){ + //success + echo "Success! Student added to database!"; + }else{ + //failure + die("Database query failed. " . mysqli_error($connection)); + } +?> + + + + + + +SQL Injection + + + + + + + + + + + diff --git a/Use of Hard-Coded Credentials/code files/Mitigated/db.php b/Use of Hard-Coded Credentials/code files/Mitigated/db.php new file mode 100644 index 0000000..9f83369 --- /dev/null +++ b/Use of Hard-Coded Credentials/code files/Mitigated/db.php @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/Use of Hard-Coded Credentials/code files/Mitigated/index.php b/Use of Hard-Coded Credentials/code files/Mitigated/index.php new file mode 100644 index 0000000..7733e12 --- /dev/null +++ b/Use of Hard-Coded Credentials/code files/Mitigated/index.php @@ -0,0 +1,27 @@ + + + + + + +Form + + + + + + +
+
+ First name:
+ Last name:
+ Student ID:
+
+
+
+ + + + + \ No newline at end of file diff --git a/Use of Hard-Coded Credentials/code files/Vulnerable/SQL_Injection.php b/Use of Hard-Coded Credentials/code files/Vulnerable/SQL_Injection.php new file mode 100644 index 0000000..5168127 --- /dev/null +++ b/Use of Hard-Coded Credentials/code files/Vulnerable/SQL_Injection.php @@ -0,0 +1,66 @@ + +prepare("INSERT INTO students (first_name, last_name, student_id) VALUES (?, ?, ?)"); + $stmt->bind_param('sss', $_POST['first_name'], $_POST['last_name'], $_POST['student_id']); + $stmt->execute(); + $stmt->close(); + + + + //test if there was a query error + if($stmt){ + //success + echo "Success! Student added to database!"; + }else{ + //failure + die("Database query failed. " . mysqli_error($connection)); + } +?> + + + + + + +SQL Injection + + + + + + + + + + + diff --git a/Use of Hard-Coded Credentials/code files/Vulnerable/index.php b/Use of Hard-Coded Credentials/code files/Vulnerable/index.php new file mode 100644 index 0000000..7733e12 --- /dev/null +++ b/Use of Hard-Coded Credentials/code files/Vulnerable/index.php @@ -0,0 +1,27 @@ + + + + + + +Form + + + + + + +
+
+ First name:
+ Last name:
+ Student ID:
+
+
+
+ + + + + \ No newline at end of file diff --git a/Use of a broken or risky Cryptographic algorithm/Mitigated/AES.java b/Use of a broken or risky Cryptographic algorithm/Mitigated/AES.java new file mode 100644 index 0000000..e53cce8 --- /dev/null +++ b/Use of a broken or risky Cryptographic algorithm/Mitigated/AES.java @@ -0,0 +1,66 @@ +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.xml.bind.DatatypeConverter; + + +public class AES { + + + public static void main(String[] args) throws Exception { + + String plainText = "Secret Message"; + SecretKey secKey = getSecretEncryptionKey(); + byte[] cipherText = encryptText(plainText, secKey); + String decryptedText = decryptText(cipherText, secKey); + + + + System.out.println("Original Text:" + plainText); + System.out.println("AES Key (Hex Form):"+bytesToHex(secKey.getEncoded())); + System.out.println("Encrypted Text (Hex Form):"+bytesToHex(cipherText)); + System.out.println("Descrypted Text:"+decryptedText); + } + //End of main class + + + //Gets encryption key. Would normally be stored differently in a real world situation. + public static SecretKey getSecretEncryptionKey() throws Exception{ + + KeyGenerator generator = KeyGenerator.getInstance("AES"); + generator.init(128); // AES key size. More secure than the 56 bit DES + SecretKey secKey = generator.generateKey(); + return secKey; + } + + + //ENCRYPT our text using the secret key to byte array + public static byte[] encryptText(String plainText,SecretKey secKey) throws Exception{ + + Cipher aesCipher = Cipher.getInstance("AES"); + aesCipher.init(Cipher.ENCRYPT_MODE, secKey); + byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes()); + return byteCipherText; + } + + + //DECRYPTS the byte array using the key + public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws Exception { + + Cipher aesCipher = Cipher.getInstance("AES"); + aesCipher.init(Cipher.DECRYPT_MODE, secKey); + byte[] bytePlainText = aesCipher.doFinal(byteCipherText); + return new String(bytePlainText); + + } + + + + //Converts binary byte array into readable hex + private static String bytesToHex(byte[] hash) { + + return DatatypeConverter.printHexBinary(hash); + + } + +} diff --git a/Use of a broken or risky Cryptographic algorithm/Use of a broken or risky cryptographic algorithm.docx b/Use of a broken or risky Cryptographic algorithm/Use of a broken or risky cryptographic algorithm.docx new file mode 100644 index 0000000..415f311 Binary files /dev/null and b/Use of a broken or risky Cryptographic algorithm/Use of a broken or risky cryptographic algorithm.docx differ diff --git a/Use of a broken or risky Cryptographic algorithm/Vulnerable/DES.java b/Use of a broken or risky Cryptographic algorithm/Vulnerable/DES.java new file mode 100644 index 0000000..2495ad8 --- /dev/null +++ b/Use of a broken or risky Cryptographic algorithm/Vulnerable/DES.java @@ -0,0 +1,44 @@ +import java.io.*; +import javax.crypto.*; +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; + + +public class DES { + + + public static void main(String[] args) { + + try{ + + String st = "Hello"; + System.out.println("Real String: " + st); + + byte str[]= st.getBytes(); + Cipher c= Cipher.getInstance("DES"); + KeyGenerator kg =KeyGenerator.getInstance("DES"); + SecretKey sk= kg.generateKey(); + + //encrypt mode + c.init(Cipher.ENCRYPT_MODE, sk); + byte ct[]= c.doFinal(str); + System.out.println("ENCRYPT_MODE DATA : " + new String(ct)); + + //decrypt mode + c.init(Cipher.DECRYPT_MODE, sk); + byte ct1[]= c.doFinal(ct); + System.out.println("DECRYPT_MODE DATA : " + new String(ct1)); + + + + } + catch(Exception e) + { + System.out.println(e); + } + + + } + +}