/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */ private void GoodB2G2(HttpRequest req, HttpResponse resp) { string data = null; using (SecureString securePwd = new SecureString()) { using (SecureString secureUser = new SecureString()) { for (int i = 0; i < "AP@ssw0rd".Length; i++) { /* INCIDENTAL: CWE-798 Use of Hard-coded Credentials */ securePwd.AppendChar("AP@ssw0rd"[i]); } for (int i = 0; i < "user".Length; i++) { /* INCIDENTAL: CWE-798 Use of Hard-coded Credentials */ securePwd.AppendChar("user"[i]); } /* POTENTIAL FLAW: Set data to credentials (without hashing or encryption) */ data = secureUser.ToString() + ":" + securePwd.ToString(); } } goodB2G2PublicStatic = true; CWE315_Cleartext_Storage_in_Cookie__Web_22b.GoodB2G2Sink(data, req, resp); }
/* goodG2B() - use goodsource and badsink */ private void GoodG2B(HttpRequest req, HttpResponse resp) { string data = null; using (SecureString securePwd = new SecureString()) { using (SecureString secureUser = new SecureString()) { for (int i = 0; i < "AP@ssw0rd".Length; i++) { /* INCIDENTAL: CWE-798 Use of Hard-coded Credentials */ securePwd.AppendChar("AP@ssw0rd"[i]); } for (int i = 0; i < "user".Length; i++) { /* INCIDENTAL: CWE-798 Use of Hard-coded Credentials */ securePwd.AppendChar("user"[i]); } /* FIX: Set data to a hash of credentials */ { string salt = "ThisIsMySalt"; using (SHA512CryptoServiceProvider sha512 = new SHA512CryptoServiceProvider()) { string credentialsToHash = secureUser.ToString() + ":" + securePwd.ToString(); byte[] buffer = Encoding.UTF8.GetBytes(string.Concat(salt, credentialsToHash)); byte[] hashedCredsAsBytes = sha512.ComputeHash(buffer); data = IO.ToHex(hashedCredsAsBytes); } } } } goodG2BPublicStatic = true; CWE315_Cleartext_Storage_in_Cookie__Web_22b.GoodG2BSink(data, req, resp); }