chromepass.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @File : chromepass.py
  5. @Time : 2019/07/11 07:24:03
  6. @Author : Liuyuqi
  7. @Version : 1.0
  8. @Contact : liuyuqi.gov@msn.cn
  9. @License : (C)Copyright 2019
  10. @Desc : Chrome密码提取
  11. pip install pywin32
  12. python chromepass.py -d
  13. '''
  14. import argparse
  15. import json
  16. import os
  17. import sqlite3
  18. import sys
  19. try:
  20. import win32crypt
  21. except:
  22. pass
  23. def args_parser():
  24. parser = argparse.ArgumentParser(
  25. description="Retrieve Google Chrome Passwords")
  26. parser.add_argument("-o", "--output", choices=['csv', 'json'],
  27. help="Output passwords to [ CSV | JSON ] format.")
  28. parser.add_argument(
  29. "-d", "--dump", help="Dump passwords to stdout. ", action="store_true")
  30. args = parser.parse_args()
  31. res = main()
  32. if args.dump:
  33. for data in res:
  34. print(data)
  35. if args.output == 'csv':
  36. output_csv(res)
  37. return
  38. if args.output == 'json':
  39. output_json(res)
  40. return
  41. else:
  42. parser.print_help()
  43. def main():
  44. info_list = []
  45. path = getpath()
  46. try:
  47. connection = sqlite3.connect(path + "Login Data")
  48. with connection:
  49. cursor = connection.cursor()
  50. v = cursor.execute(
  51. 'SELECT action_url, username_value, password_value FROM logins')
  52. value = v.fetchall()
  53. if (os.name == "posix") and (sys.platform == "darwin"):
  54. print("Mac OSX not supported.")
  55. sys.exit(0)
  56. for origin_url, username, password in value:
  57. if os.name == 'nt':
  58. password = win32crypt.CryptUnprotectData(
  59. password, None, None, None, 0)[1]
  60. if password:
  61. info_list.append({
  62. 'origin_url': origin_url,
  63. 'username': username,
  64. 'password': str(password)
  65. })
  66. except Exception as e:
  67. e = str(e)
  68. if (e == 'database is locked'):
  69. print('[!] Make sure Google Chrome is not running in the background')
  70. elif (e == 'no such table: logins'):
  71. print('[!] Something wrong with the database name')
  72. elif (e == 'unable to open database file'):
  73. print('[!] Something wrong with the database path')
  74. elif (e == "name win32crypt is not defined"):
  75. print("win32crypt error")
  76. else:
  77. print(e)
  78. sys.exit(0)
  79. return info_list
  80. def getpath():
  81. if os.name == "nt":
  82. # This is the Windows Path
  83. PathName = os.getenv('localappdata') + \
  84. '\\Google\\Chrome\\User Data\\Default\\'
  85. elif os.name == "posix":
  86. PathName = os.getenv('HOME')
  87. if sys.platform == "darwin":
  88. # This is the OS X Path
  89. PathName += '/Library/Application Support/Google/Chrome/Default/'
  90. else:
  91. # This is the Linux Path
  92. PathName += '/.config/google-chrome/Default/'
  93. if not os.path.isdir(PathName):
  94. print('[!] Chrome Doesn\'t exists')
  95. sys.exit(0)
  96. return PathName
  97. def output_csv(info):
  98. try:
  99. with open('chromepass-passwords.csv', 'wb') as csv_file:
  100. csv_file.write('origin_url,username,password \n'.encode('utf-8'))
  101. for data in info:
  102. csv_file.write(('%s, %s, %s \n' % (data['origin_url'], data[
  103. 'username'], data['password'])).encode('utf-8'))
  104. print("Data written to chromepass-passwords.csv")
  105. except EnvironmentError:
  106. print('EnvironmentError: cannot write data')
  107. def output_json(info):
  108. try:
  109. with open('chromepass-passwords.json', 'w') as json_file:
  110. json.dump({'password_items':info},json_file)
  111. print("Data written to chromepass-passwords.json")
  112. except EnvironmentError:
  113. print('EnvironmentError: cannot write data')
  114. if __name__ == '__main__':
  115. args_parser()