Consolidated ASP Classic MVC framework from best components
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

265 líneas
9.8KB

  1. <%
  2. '=======================================================================================================================
  3. ' Password hashing
  4. '
  5. ' Pure VBScript SHA-256 - no external process, no shell-out, no COM crypto
  6. ' dependency. The previous implementation shelled out to a PowerShell script
  7. ' (hash_sha256.ps1, which did not exist anywhere in this repo) via
  8. ' WScript.Shell.Exec with the raw password concatenated directly into the
  9. ' command line. That was both non-functional (missing script) and a command
  10. ' injection vulnerability (a password containing ", `, ; etc. could execute
  11. ' arbitrary PowerShell, and the password was visible in the process command
  12. ' line to anything that could list processes).
  13. '
  14. ' NOTE: This hashes without a per-user salt, matching the single-argument
  15. ' HashPassword(password) contract these functions already had. Unsalted
  16. ' hashes are vulnerable to precomputed/rainbow-table attacks. If a real
  17. ' Users table is introduced, prefer storing a random per-user salt alongside
  18. ' the hash and hashing (salt & password) - that is a schema change, so it is
  19. ' intentionally not done here.
  20. '=======================================================================================================================
  21. Private Function Sha256_U32(n)
  22. If n < 0 Then
  23. Sha256_U32 = n + 4294967296.0
  24. Else
  25. Sha256_U32 = CDbl(n)
  26. End If
  27. End Function
  28. Private Function Sha256_S32(d)
  29. Do While d >= 4294967296.0
  30. d = d - 4294967296.0
  31. Loop
  32. Do While d < 0
  33. d = d + 4294967296.0
  34. Loop
  35. If d >= 2147483648.0 Then
  36. Sha256_S32 = CLng(d - 4294967296.0)
  37. Else
  38. Sha256_S32 = CLng(d)
  39. End If
  40. End Function
  41. Private Function Sha256_Mod(x, y)
  42. Sha256_Mod = x - Int(x / y) * y
  43. End Function
  44. Private Function Sha256_Add32(a, b)
  45. Sha256_Add32 = Sha256_S32(Sha256_U32(a) + Sha256_U32(b))
  46. End Function
  47. Private Function Sha256_Add32_5(a, b, c, d, e)
  48. Sha256_Add32_5 = Sha256_S32(Sha256_U32(a) + Sha256_U32(b) + Sha256_U32(c) + Sha256_U32(d) + Sha256_U32(e))
  49. End Function
  50. Private Function Sha256_RotR32(x, n)
  51. Dim u, partA, keepBits, partB
  52. If n = 0 Then
  53. Sha256_RotR32 = x
  54. Exit Function
  55. End If
  56. u = Sha256_U32(x)
  57. partA = Int(u / (2 ^ n))
  58. keepBits = Sha256_Mod(u, 2 ^ n)
  59. partB = keepBits * (2 ^ (32 - n))
  60. Sha256_RotR32 = Sha256_S32(partA + partB)
  61. End Function
  62. Private Function Sha256_ShR32(x, n)
  63. Dim u
  64. u = Sha256_U32(x)
  65. Sha256_ShR32 = Sha256_S32(Int(u / (2 ^ n)))
  66. End Function
  67. Private Function Sha256_BytesToWord(b0, b1, b2, b3)
  68. Sha256_BytesToWord = Sha256_S32(b0 * 16777216.0 + b1 * 65536.0 + b2 * 256.0 + b3)
  69. End Function
  70. Private Function Sha256_HexWord(x)
  71. Dim u, h
  72. u = Sha256_U32(x)
  73. h = ""
  74. Do While u > 0
  75. h = Mid("0123456789abcdef", Sha256_Mod(u, 16) + 1, 1) & h
  76. u = Int(u / 16)
  77. Loop
  78. Sha256_HexWord = Right("00000000" & h, 8)
  79. End Function
  80. ' Converts a VBScript string (UTF-16) to a UTF-8 byte array (Long values 0-255).
  81. ' Pure VBScript - avoids ADODB.Stream, which returns a Byte SafeArray that is
  82. ' not reliably indexable from VBScript on every 32/64-bit IIS configuration.
  83. Private Function Sha256_StringToUtf8Bytes(s)
  84. Dim arr(), count, i, code, lenS
  85. lenS = Len(s)
  86. If lenS = 0 Then
  87. ReDim arr(-1)
  88. Sha256_StringToUtf8Bytes = arr
  89. Exit Function
  90. End If
  91. ReDim arr(lenS * 3)
  92. count = -1
  93. For i = 1 To lenS
  94. code = AscW(Mid(s, i, 1))
  95. If code < 0 Then code = code + 65536
  96. If code <= 127 Then
  97. count = count + 1 : arr(count) = code
  98. ElseIf code <= 2047 Then
  99. count = count + 1 : arr(count) = 192 Or Int(code / 64)
  100. count = count + 1 : arr(count) = 128 Or (code And 63)
  101. Else
  102. count = count + 1 : arr(count) = 224 Or Int(code / 4096)
  103. count = count + 1 : arr(count) = 128 Or (Int(code / 64) And 63)
  104. count = count + 1 : arr(count) = 128 Or (code And 63)
  105. End If
  106. Next
  107. ReDim Preserve arr(count)
  108. Sha256_StringToUtf8Bytes = arr
  109. End Function
  110. ' Returns the SHA-256 hash of inputString (UTF-8 encoded) as a 64-character
  111. ' lowercase hex string. Verified against NIST/RFC test vectors (empty string,
  112. ' "abc", the 56-byte two-block vector, and non-ASCII input) plus .NET's
  113. ' System.Security.Cryptography.SHA256 as a cross-check.
  114. Function Sha256Hex(inputString)
  115. Dim msg, msgLen, bitLen, i, t
  116. Dim numBlocks, blockIdx, base
  117. Dim hs(7)
  118. Dim k(63)
  119. Dim w(63)
  120. Dim a, b, c, d, e, f, g, h
  121. Dim s0, s1, ch, maj, temp1, temp2, bigS0, bigS1
  122. Dim padded()
  123. Dim padLen, totalLen, lenPos, hi32, lo32
  124. Dim kHex, result
  125. hs(0) = &h6a09e667 : hs(1) = &hbb67ae85 : hs(2) = &h3c6ef372 : hs(3) = &ha54ff53a
  126. hs(4) = &h510e527f : hs(5) = &h9b05688c : hs(6) = &h1f83d9ab : hs(7) = &h5be0cd19
  127. kHex = Array( _
  128. "428a2f98","71374491","b5c0fbcf","e9b5dba5","3956c25b","59f111f1","923f82a4","ab1c5ed5", _
  129. "d807aa98","12835b01","243185be","550c7dc3","72be5d74","80deb1fe","9bdc06a7","c19bf174", _
  130. "e49b69c1","efbe4786","0fc19dc6","240ca1cc","2de92c6f","4a7484aa","5cb0a9dc","76f988da", _
  131. "983e5152","a831c66d","b00327c8","bf597fc7","c6e00bf3","d5a79147","06ca6351","14292967", _
  132. "27b70a85","2e1b2138","4d2c6dfc","53380d13","650a7354","766a0abb","81c2c92e","92722c85", _
  133. "a2bfe8a1","a81a664b","c24b8b70","c76c51a3","d192e819","d6990624","f40e3585","106aa070", _
  134. "19a4c116","1e376c08","2748774c","34b0bcb5","391c0cb3","4ed8aa4a","5b9cca4f","682e6ff3", _
  135. "748f82ee","78a5636f","84c87814","8cc70208","90befffa","a4506ceb","bef9a3f7","c67178f2")
  136. For i = 0 To 63
  137. k(i) = Sha256_S32(CDbl("&h" & kHex(i)))
  138. Next
  139. msg = Sha256_StringToUtf8Bytes(inputString)
  140. If UBound(msg) < LBound(msg) Then
  141. msgLen = 0
  142. Else
  143. msgLen = UBound(msg) - LBound(msg) + 1
  144. End If
  145. bitLen = msgLen * 8.0
  146. ' Padding: msg + 0x80 + zero bytes so length % 64 = 56, then an 8-byte
  147. ' big-endian bit length.
  148. padLen = 56 - Sha256_Mod(msgLen + 1, 64)
  149. If padLen < 0 Then padLen = padLen + 64
  150. totalLen = msgLen + 1 + padLen + 8
  151. ReDim padded(totalLen - 1)
  152. For i = 0 To msgLen - 1
  153. padded(i) = msg(i)
  154. Next
  155. padded(msgLen) = 128
  156. For i = msgLen + 1 To msgLen + padLen
  157. padded(i) = 0
  158. Next
  159. lenPos = msgLen + 1 + padLen
  160. hi32 = Int(bitLen / 4294967296.0)
  161. lo32 = bitLen - hi32 * 4294967296.0
  162. padded(lenPos + 0) = Sha256_Mod(Int(hi32 / 16777216), 256)
  163. padded(lenPos + 1) = Sha256_Mod(Int(hi32 / 65536), 256)
  164. padded(lenPos + 2) = Sha256_Mod(Int(hi32 / 256), 256)
  165. padded(lenPos + 3) = Sha256_Mod(hi32, 256)
  166. padded(lenPos + 4) = Int(lo32 / 16777216)
  167. padded(lenPos + 5) = Sha256_Mod(Int(lo32 / 65536), 256)
  168. padded(lenPos + 6) = Sha256_Mod(Int(lo32 / 256), 256)
  169. padded(lenPos + 7) = Sha256_Mod(lo32, 256)
  170. numBlocks = totalLen / 64
  171. For blockIdx = 0 To numBlocks - 1
  172. base = blockIdx * 64
  173. For t = 0 To 15
  174. w(t) = Sha256_BytesToWord(padded(base + t*4), padded(base + t*4 + 1), padded(base + t*4 + 2), padded(base + t*4 + 3))
  175. Next
  176. For t = 16 To 63
  177. s0 = Sha256_RotR32(w(t-15), 7) Xor Sha256_RotR32(w(t-15), 18) Xor Sha256_ShR32(w(t-15), 3)
  178. s1 = Sha256_RotR32(w(t-2), 17) Xor Sha256_RotR32(w(t-2), 19) Xor Sha256_ShR32(w(t-2), 10)
  179. w(t) = Sha256_Add32_5(w(t-16), s0, w(t-7), s1, 0)
  180. Next
  181. a = hs(0) : b = hs(1) : c = hs(2) : d = hs(3)
  182. e = hs(4) : f = hs(5) : g = hs(6) : h = hs(7)
  183. For t = 0 To 63
  184. bigS1 = Sha256_RotR32(e,6) Xor Sha256_RotR32(e,11) Xor Sha256_RotR32(e,25)
  185. ch = (e And f) Xor ((Not e) And g)
  186. temp1 = Sha256_Add32_5(h, bigS1, ch, k(t), w(t))
  187. bigS0 = Sha256_RotR32(a,2) Xor Sha256_RotR32(a,13) Xor Sha256_RotR32(a,22)
  188. maj = (a And b) Xor (a And c) Xor (b And c)
  189. temp2 = Sha256_Add32(bigS0, maj)
  190. h = g
  191. g = f
  192. f = e
  193. e = Sha256_Add32(d, temp1)
  194. d = c
  195. c = b
  196. b = a
  197. a = Sha256_Add32(temp1, temp2)
  198. Next
  199. hs(0) = Sha256_Add32(hs(0), a) : hs(1) = Sha256_Add32(hs(1), b) : hs(2) = Sha256_Add32(hs(2), c) : hs(3) = Sha256_Add32(hs(3), d)
  200. hs(4) = Sha256_Add32(hs(4), e) : hs(5) = Sha256_Add32(hs(5), f) : hs(6) = Sha256_Add32(hs(6), g) : hs(7) = Sha256_Add32(hs(7), h)
  201. Next
  202. result = ""
  203. For i = 0 To 7
  204. result = result & Sha256_HexWord(hs(i))
  205. Next
  206. Sha256Hex = result
  207. End Function
  208. Function HashPassword(password)
  209. HashPassword = Sha256Hex(password)
  210. End Function
  211. '=======================================================================================================================
  212. ' CheckPassword
  213. '
  214. ' The original implementation called CreateRepository(conn, "Users", "UserId")
  215. ' and Array("UserName", user) - "conn" and "CreateRepository" are not defined
  216. ' anywhere in this framework (no global "conn", no CreateRepository factory),
  217. ' and it searched using the not-yet-assigned "user" variable instead of the
  218. ' "username" parameter, so this function has never been callable. Nothing in
  219. ' this codebase calls it yet, and there is no Users table/migration either -
  220. ' this remains example/scaffold code. Rewritten here to use the framework's
  221. ' actual data-access primitive (DAL().Query with a parameterized command) and
  222. ' the correct lookup value, so it will work once a Users table exists with
  223. ' UserName and PasswordHash columns.
  224. '=======================================================================================================================
  225. Function CheckPassword(username, password)
  226. Dim rs
  227. Set rs = DAL().Query("SELECT PasswordHash FROM Users WHERE UserName = ?", Array(username))
  228. If rs.EOF Then
  229. CheckPassword = False
  230. Else
  231. CheckPassword = (HashPassword(password) = rs("PasswordHash"))
  232. End If
  233. rs.Close
  234. Set rs = Nothing
  235. End Function
  236. %>

Powered by TurnKey Linux.