Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.37 KB | None | 0 0
  1. public class DPAPI
  2. {
  3. [DllImport("crypt32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  4. private static extern
  5. bool CryptProtectData(ref DATA_BLOB pPlainText, string szDescription, ref DATA_BLOB pEntropy, IntPtr pReserved,
  6. ref CRYPTPROTECT_PROMPTSTRUCT pPrompt, int dwFlags, ref DATA_BLOB pCipherText);
  7.  
  8. [DllImport("crypt32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  9. private static extern
  10. bool CryptUnprotectData(ref DATA_BLOB pCipherText, ref string pszDescription, ref DATA_BLOB pEntropy,
  11. IntPtr pReserved, ref CRYPTPROTECT_PROMPTSTRUCT pPrompt, int dwFlags, ref DATA_BLOB pPlainText);
  12.  
  13. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  14. internal struct DATA_BLOB
  15. {
  16. public int cbData;
  17. public IntPtr pbData;
  18. }
  19.  
  20. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  21. internal struct CRYPTPROTECT_PROMPTSTRUCT
  22. {
  23. public int cbSize;
  24. public int dwPromptFlags;
  25. public IntPtr hwndApp;
  26. public string szPrompt;
  27. }
  28.  
  29. static private IntPtr NullPtr = ((IntPtr)((int)(0)));
  30.  
  31. private const int CRYPTPROTECT_UI_FORBIDDEN = 0x1;
  32. private const int CRYPTPROTECT_LOCAL_MACHINE = 0x4;
  33.  
  34. private static void InitPrompt(ref CRYPTPROTECT_PROMPTSTRUCT ps)
  35. {
  36. ps.cbSize = Marshal.SizeOf(
  37. typeof(CRYPTPROTECT_PROMPTSTRUCT));
  38. ps.dwPromptFlags = 0;
  39. ps.hwndApp = NullPtr;
  40. ps.szPrompt = null;
  41. }
  42.  
  43. private static void InitBLOB(byte[] data, ref DATA_BLOB blob)
  44. {
  45. // Use empty array for null parameter.
  46. if (data == null)
  47. data = new byte[0];
  48.  
  49. // Allocate memory for the BLOB data.
  50. blob.pbData = Marshal.AllocHGlobal(data.Length);
  51.  
  52. // Make sure that memory allocation was successful.
  53. if (blob.pbData == IntPtr.Zero)
  54. throw new Exception(
  55. "Unable to allocate data buffer for BLOB structure.");
  56.  
  57. // Specify number of bytes in the BLOB.
  58. blob.cbData = data.Length;
  59.  
  60. // Copy data from original source to the BLOB structure.
  61. Marshal.Copy(data, 0, blob.pbData, data.Length);
  62. }
  63.  
  64. public enum KeyType { UserKey = 1, MachineKey };
  65.  
  66. private static KeyType defaultKeyType = KeyType.UserKey;
  67.  
  68. public static string Encrypt(string plainText)
  69. {
  70. return Encrypt(defaultKeyType, plainText, String.Empty, String.Empty);
  71. }
  72.  
  73. public static string Encrypt(KeyType keyType, string plainText)
  74. {
  75. return Encrypt(keyType, plainText, String.Empty,
  76. String.Empty);
  77. }
  78.  
  79. public static string Encrypt(KeyType keyType, string plainText, string entropy)
  80. {
  81. return Encrypt(keyType, plainText, entropy, String.Empty);
  82. }
  83.  
  84. public static string Encrypt(KeyType keyType, string plainText, string entropy, string description)
  85. {
  86. // Make sure that parameters are valid.
  87. if (plainText == null) plainText = String.Empty;
  88. if (entropy == null) entropy = String.Empty;
  89.  
  90. // Call encryption routine and convert returned bytes into
  91. // a base64-encoded value.
  92. return Convert.ToBase64String(
  93. Encrypt(keyType,
  94. Encoding.UTF8.GetBytes(plainText),
  95. Encoding.UTF8.GetBytes(entropy),
  96. description));
  97. }
  98.  
  99. public static byte[] Encrypt(KeyType keyType, byte[] plainTextBytes, byte[] entropyBytes, string description)
  100. {
  101. // Make sure that parameters are valid.
  102. if (plainTextBytes == null) plainTextBytes = new byte[0];
  103. if (entropyBytes == null) entropyBytes = new byte[0];
  104. if (description == null) description = String.Empty;
  105.  
  106. // Create BLOBs to hold data.
  107. DATA_BLOB plainTextBlob = new DATA_BLOB();
  108. DATA_BLOB cipherTextBlob = new DATA_BLOB();
  109. DATA_BLOB entropyBlob = new DATA_BLOB();
  110.  
  111. // We only need prompt structure because it is a required
  112. // parameter.
  113. CRYPTPROTECT_PROMPTSTRUCT prompt =
  114. new CRYPTPROTECT_PROMPTSTRUCT();
  115. InitPrompt(ref prompt);
  116.  
  117. try
  118. {
  119. // Convert plaintext bytes into a BLOB structure.
  120. try
  121. {
  122. InitBLOB(plainTextBytes, ref plainTextBlob);
  123. }
  124. catch (Exception ex)
  125. {
  126. throw new Exception(
  127. "Cannot initialize plaintext BLOB.", ex);
  128. }
  129.  
  130. // Convert entropy bytes into a BLOB structure.
  131. try
  132. {
  133. InitBLOB(entropyBytes, ref entropyBlob);
  134. }
  135. catch (Exception ex)
  136. {
  137. throw new Exception(
  138. "Cannot initialize entropy BLOB.", ex);
  139. }
  140.  
  141. // Disable any types of UI.
  142. int flags = CRYPTPROTECT_UI_FORBIDDEN;
  143.  
  144. // When using machine-specific key, set up machine flag.
  145. if (keyType == KeyType.MachineKey)
  146. flags |= CRYPTPROTECT_LOCAL_MACHINE;
  147.  
  148. // Call DPAPI to encrypt data.
  149. bool success = CryptProtectData(ref plainTextBlob,
  150. description,
  151. ref entropyBlob,
  152. IntPtr.Zero,
  153. ref prompt,
  154. flags,
  155. ref cipherTextBlob);
  156. // Check the result.
  157. if (!success)
  158. {
  159. // If operation failed, retrieve last Win32 error.
  160. int errCode = Marshal.GetLastWin32Error();
  161.  
  162. // Win32Exception will contain error message corresponding
  163. // to the Windows error code.
  164. throw new Exception(
  165. "CryptProtectData failed.", new Win32Exception(errCode));
  166. }
  167.  
  168. // Allocate memory to hold ciphertext.
  169. byte[] cipherTextBytes = new byte[cipherTextBlob.cbData];
  170.  
  171. // Copy ciphertext from the BLOB to a byte array.
  172. Marshal.Copy(cipherTextBlob.pbData,
  173. cipherTextBytes,
  174. 0,
  175. cipherTextBlob.cbData);
  176.  
  177. // Return the result.
  178. return cipherTextBytes;
  179. }
  180. catch (Exception ex)
  181. {
  182. throw new Exception("DPAPI was unable to encrypt data.", ex);
  183. }
  184. // Free all memory allocated for BLOBs.
  185. finally
  186. {
  187. if (plainTextBlob.pbData != IntPtr.Zero)
  188. Marshal.FreeHGlobal(plainTextBlob.pbData);
  189.  
  190. if (cipherTextBlob.pbData != IntPtr.Zero)
  191. Marshal.FreeHGlobal(cipherTextBlob.pbData);
  192.  
  193. if (entropyBlob.pbData != IntPtr.Zero)
  194. Marshal.FreeHGlobal(entropyBlob.pbData);
  195. }
  196. }
  197.  
  198. public static string Decrypt(string cipherText)
  199. {
  200. string description;
  201.  
  202. return Decrypt(cipherText, String.Empty, out description);
  203. }
  204.  
  205. public static string Decrypt(string cipherText,out string description)
  206. {
  207. return Decrypt(cipherText, String.Empty, out description);
  208. }
  209.  
  210. public static string Decrypt(string cipherText,string entropy,out string description)
  211. {
  212. // Make sure that parameters are valid.
  213. if (entropy == null) entropy = String.Empty;
  214.  
  215. return Encoding.UTF8.GetString(
  216. Decrypt(Convert.FromBase64String(cipherText),
  217. Encoding.UTF8.GetBytes(entropy),
  218. out description));
  219. }
  220.  
  221. public static byte[] Decrypt(byte[] cipherTextBytes,byte[] entropyBytes,out string description)
  222. {
  223. // Create BLOBs to hold data.
  224. DATA_BLOB plainTextBlob = new DATA_BLOB();
  225. DATA_BLOB cipherTextBlob = new DATA_BLOB();
  226. DATA_BLOB entropyBlob = new DATA_BLOB();
  227.  
  228. // We only need prompt structure because it is a required
  229. // parameter.
  230. CRYPTPROTECT_PROMPTSTRUCT prompt =
  231. new CRYPTPROTECT_PROMPTSTRUCT();
  232. InitPrompt(ref prompt);
  233.  
  234. // Initialize description string.
  235. description = String.Empty;
  236.  
  237. try
  238. {
  239. // Convert ciphertext bytes into a BLOB structure.
  240. try
  241. {
  242. InitBLOB(cipherTextBytes, ref cipherTextBlob);
  243. }
  244. catch (Exception ex)
  245. {
  246. throw new Exception(
  247. "Cannot initialize ciphertext BLOB.", ex);
  248. }
  249.  
  250. // Convert entropy bytes into a BLOB structure.
  251. try
  252. {
  253. InitBLOB(entropyBytes, ref entropyBlob);
  254. }
  255. catch (Exception ex)
  256. {
  257. throw new Exception(
  258. "Cannot initialize entropy BLOB.", ex);
  259. }
  260.  
  261. // Disable any types of UI. CryptUnprotectData does not
  262. // mention CRYPTPROTECT_LOCAL_MACHINE flag in the list of
  263. // supported flags so we will not set it up.
  264. int flags = CRYPTPROTECT_UI_FORBIDDEN;
  265.  
  266. // Call DPAPI to decrypt data.
  267. bool success = CryptUnprotectData(ref cipherTextBlob,
  268. ref description,
  269. ref entropyBlob,
  270. IntPtr.Zero,
  271. ref prompt,
  272. flags,
  273. ref plainTextBlob);
  274.  
  275. // Check the result.
  276. if (!success)
  277. {
  278. // If operation failed, retrieve last Win32 error.
  279. int errCode = Marshal.GetLastWin32Error();
  280.  
  281. // Win32Exception will contain error message corresponding
  282. // to the Windows error code.
  283. throw new Exception(
  284. "CryptUnprotectData failed.", new Win32Exception(errCode));
  285. }
  286.  
  287. // Allocate memory to hold plaintext.
  288. byte[] plainTextBytes = new byte[plainTextBlob.cbData];
  289.  
  290. // Copy ciphertext from the BLOB to a byte array.
  291. Marshal.Copy(plainTextBlob.pbData,
  292. plainTextBytes,
  293. 0,
  294. plainTextBlob.cbData);
  295.  
  296. // Return the result.
  297. return plainTextBytes;
  298. }
  299. catch (Exception ex)
  300. {
  301. throw new Exception("DPAPI was unable to decrypt data.", ex);
  302. }
  303. // Free all memory allocated for BLOBs.
  304. finally
  305. {
  306. if (plainTextBlob.pbData != IntPtr.Zero)
  307. Marshal.FreeHGlobal(plainTextBlob.pbData);
  308.  
  309. if (cipherTextBlob.pbData != IntPtr.Zero)
  310. Marshal.FreeHGlobal(cipherTextBlob.pbData);
  311.  
  312. if (entropyBlob.pbData != IntPtr.Zero)
  313. Marshal.FreeHGlobal(entropyBlob.pbData);
  314. }
  315. }
  316. }
  317.  
  318. static void Main(string[] args)
  319. {
  320. string filename = "my_chrome_passwords.html";
  321. var Writer = new StreamWriter(filename, false, Encoding.UTF8);
  322. string db_way =
  323. Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
  324. + "/Google/Chrome/User Data/Default/Login Data";
  325.  
  326. string db_field = "logins";
  327. byte[] entropy = null;
  328. string description; // Неизвестный параметр
  329.  
  330. string ConnectionString = "data source=" + db_way +
  331. ";New=True;UseUTF16Encoding=True";
  332. var DB = new DataTable();
  333. string sql = string.Format("SELECT * FROM {0} {1} {2}", db_field, "", "");
  334.  
  335. using (var connect = new SQLiteConnection(ConnectionString))
  336. {
  337. var command = new SQLiteCommand(sql, connect);
  338. var adapter = new SQLiteDataAdapter(command);
  339. adapter.Fill(DB);
  340. int rows = DB.Rows.Count;
  341. for (int i = 0; i < rows; i++)
  342. {
  343. byte[] byteArray = (byte[])DB.Rows[i][5];
  344. byte[] decrypted = DPAPI.Decrypt(byteArray, entropy, out description);
  345. // out string description;
  346. string password = new UTF8Encoding(true).GetString(decrypted);
  347. }
  348. }
  349. }
  350.  
  351. public static byte[] DecryptPassword(byte[] reader)
  352. {
  353. try
  354. {
  355. return ProtectedData.Unprotect(reader, null,
  356. DataProtectionScope.CurrentUser);
  357. }
  358. catch (CryptographicException) { return null; }
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement