Advertisement
TrustyJAID

Blockchain Downloader

Nov 26th, 2016
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.22 KB | None | 0 0
  1. #!usr/bin/python2
  2. # This code has been modified from several sources and is free to use
  3. # Install Python 2.7.12
  4. # Install pip on windows
  5. # https://github.com/BurntSushi/nfldb/wiki/Python-&-pip-Windows-installation
  6. # Install pip on linux $ sudo apt-get install python-pip
  7. # During install when it asks for what modules to install
  8. # ensure that the last option install to PATH is enabled
  9. # After installing run Command Prompt/Terminal
  10. # Change directory to where the file is saved
  11. # Type: python blockchain_downloader.py
  12. # You can add the transaction after or it will ask
  13. # for one upon start if one isn't supplied
  14. # You can also add a filename to save it as after the transaction
  15. # The dafault filename is 'file'
  16. # If you have the bitcoin RPC service setup with a
  17. # local blockchain add it to the RPC settings at the top
  18. from __future__ import print_function
  19. import sys
  20. import struct
  21. from binascii import unhexlify, crc32
  22. import urllib2
  23. import imp
  24. import pip
  25. from timeit import default_timer as timer
  26. try:
  27.     imp.find_module('jsonrpclib')
  28. except ImportError:
  29.     pip.main(['install', 'jsonrpclib'])
  30. import jsonrpclib
  31. '''There are some things to fix like the length of the
  32. transactions and adding wallet search but this should
  33. work for any previous file on the blockchain. The list
  34. of transactions must also end with a blank line and
  35. '''
  36.  
  37.  
  38. RPCSERVER = "http://User:Pass@localhost:8332"
  39.  
  40. SERVER = jsonrpclib.Server(RPCSERVER)
  41.  
  42.  
  43. try:
  44.     # Checks for an RPC connection
  45.     SERVER.getinfo()
  46.     LOCAL = True
  47. except Exception, e:
  48.     print("RPC connection not available")
  49.     LOCAL = False
  50.  
  51. def printdataoutlocal(transaction):
  52.     '''Decodes an individual transaction using local blockchain'''
  53.     rawTx = SERVER.getrawtransaction(transaction)
  54.     tx = SERVER.decoderawtransaction(rawTx)
  55.     hexdata = ''
  56.     data = b''
  57.     for txout in tx['vout'][0:-2]:
  58.         for op in txout['scriptPubKey']['asm'].split(' '):
  59.             if not op.startswith('OP_') and len(op) >= 40:
  60.                 hexdata += op.encode('utf8')
  61.                 data += unhexlify(op.encode('utf8'))
  62.  
  63.     #checksum(data)
  64.     length = struct.unpack('<L', data[0:4])[0]
  65.     data = data[8:8+length]
  66.     if checkheader(hexdata) != '':
  67.         print(checkheader(hexdata))
  68.     savefile(data)
  69.  
  70.     return data
  71.  
  72.  
  73. def checksum(data):
  74.  
  75.     checksum = struct.unpack('<L', data[4:8])[0]
  76.     # This was left in from the original script, it doesn't work correctly'
  77.     if checksum != crc32(data):
  78.         print('Checksum mismatch; expected %d but calculated %d' % (checksum, crc32(data)), end='\n\r')
  79.     # sys.exit()
  80.  
  81.  
  82. def printdataoutonline(transaction):
  83.     """This function checks the data
  84.    in the given blockhash using blockchain.info"""
  85.  
  86.     url = ('https://blockchain.info/tx/%s?show_adv=true' % str(transaction))
  87.     dataout = urllib2.urlopen(url)
  88.     data = b''
  89.     hexdata = ''
  90.     atoutput = False
  91.     for line in dataout:
  92.  
  93.         if b'Output Scripts' in line:
  94.             atoutput = True
  95.  
  96.         if b'</table>' in line:
  97.             atoutput = False
  98.  
  99.         if atoutput:
  100.             if len(line) > 100:
  101.                 chunks = line.split(b' ')
  102.                 for c in chunks:
  103.                     if b'O' not in c and b'\n' not in c and b'>' not in c and b'<' not in c:
  104.                         hexdata += c
  105.                         data += unhexlify(c)
  106.  
  107.     #checksum(data)
  108.     length = struct.unpack('<L', data[0:4])[0]
  109.     data = data[8:8+length]
  110.     if checkheader(hexdata) != '':
  111.         print(checkheader(hexdata)) # Will print out in terminal if file header matches
  112.  
  113.     savefile(data)
  114.     # sys.stdout.buffer.write(dataout)
  115.     return data
  116.  
  117.  
  118. def fileofblocks():
  119.     """This function checks the blockchain
  120.    for all files in the FILENAME document"""
  121.  
  122.     with open(BLOCKCHAINADDRESS) as f:
  123.         transaction = f.readlines()
  124.     for i in range(len(transaction)):
  125.         # print(len(transaction))
  126.         blockhash = transaction[i].rstrip('\r\n')
  127.  
  128.         if (blockhash == ''):
  129.             break
  130.  
  131.         if LOCAL:
  132.             printdataoutlocal(blockhash)
  133.         else:
  134.             printdataoutonline(blockhash)
  135.  
  136.  
  137. def savefile(dataout):
  138.     """This saves the data to the chosen
  139.    filename in binary by overwriting the file """
  140.  
  141.     with open(FILENAME, "ab") as output:
  142.         output.write(dataout)
  143.     output.close()
  144.  
  145.  
  146. def checkheader(hexcode):
  147.     #    if "99".lower() in hexcode:
  148.     #        filetype += "GPG Header Found "    # GPG Header| Commented out
  149.     #    if "9901".lower() in hexcode:
  150.     #        filetype += "PKR Header Found "    # PKR Header| to reduce positives
  151.     filetype = ''
  152.     if "D0CF11E0A1B11AE1".lower() in hexcode:
  153.         filetype += "DOC Header Found "         # DOC Header
  154.     if "576F72642E446F63756D656E742E".lower() in hexcode:
  155.         filetype += "DOC Footer Found "         # DOC Footer
  156.     if "D0CF11E0A1B11AE1".lower() in hexcode:
  157.         filetype += "XLS Header Found "         # XLS Header
  158.     if "FEFFFFFF000000000000000057006F0072006B0062006F006F006B00".lower() in hexcode:
  159.         filetype += "XLS Footer Found "         # XLS Footer
  160.     if "D0CF11E0A1B11AE1".lower() in hexcode:
  161.         filetype += "PPT Header Found "         # PPT Header
  162.     if "A0461DF0".lower() in hexcode:
  163.         filetype += "PPT Footer Found "         # PPT Footer
  164.     if "504B030414".lower() in hexcode:
  165.         filetype += "ZIP Header Found "         # ZIP Header
  166.     if "504B050600".lower() in hexcode:
  167.         filetype += "ZIP Footer Found "         # ZIP Footer
  168.     if "504B030414000100630000000000".lower() in hexcode:
  169.         filetype += "ZIPLock Footer Found "     # ZLocked Encrypted
  170.     if "FFD8FFE000104A464946000101".lower() in hexcode:
  171.         filetype += "JPG Header Found "         # JPG Header
  172.     if "474946383961".lower() in hexcode:
  173.         filetype += "GIF Header Found "         # GIF Header
  174.     if "474946383761".lower() in hexcode:
  175.         filetype += "GIF Header Found "         # GIF Header
  176.     if "2100003B00".lower() in hexcode:
  177.         filetype += "GIF Footer Found "         # GIF Footer
  178.     if "25504446".lower() in hexcode:
  179.         filetype += "PDF Header Found "         # PDF Header
  180.     if "2623323035".lower() in hexcode:
  181.         filetype += "PDF Header Found "         # PDF Header
  182.     if "2525454F46".lower() in hexcode:
  183.         filetype += "PDF Footer Found "         # PDF Footer
  184.     if "616E6E6F756E6365".lower() in hexcode:
  185.         filetype += "Torrent Header Found "     # Torrent Header
  186.     if "1F8B08".lower() in hexcode:
  187.         filetype += ".TAR.GZ Header Found "     # TAR/GZ Header
  188.     if "0011AF".lower() in hexcode:
  189.         filetype += "FLI Header Found "         # FLI Header
  190.     if "504B03040A000200".lower() in hexcode:
  191.         filetype += "EPUB Header Found "        # EPUB Header
  192.     if "89504E470D0A1A0A".lower() in hexcode:
  193.         filetype += "PNG Header Found "         # PNG Header
  194.     if "6D51514E42".lower() in hexcode:
  195.         filetype += "8192PGP Header Found "     # 8192 Header
  196.     if "6D51494E4246672F".lower() in hexcode:
  197.         filetype += "4096PGP Header Found "     # 4096 Header
  198.     if "6D51454E424667".lower() in hexcode:
  199.         filetype += "2048PGP Header Found "     # 2048 Header
  200.     if "526172211A0700".lower() in hexcode:
  201.         filetype += "RAR Header Found"          # RAR Header
  202.     if "EFEDFACE".lower() in hexcode:
  203.         filetype += "UTF8 Header Found"         # UTF8 header
  204.     if "4F676753".lower() in hexcode:
  205.         filetype += "OGG Header Found"          # OGG Header
  206.     if "42494646".lower() in hexcode and "57415645".lower() in hexcode:
  207.         filetype += "WAV Header Found"          # WAV Header
  208.     if "42494646".lower() in hexcode and "41564920".lower() in hexcode:
  209.         filetype += "AVI Header Found"          # AVI Header
  210.     if "4D546864".lower() in hexcode:
  211.         filetype += "MIDI Header Found"         # MIDI Header
  212.     if "377ABCAF271C".lower() in hexcode:
  213.         filetype += "7z Header Found"           # 7z Header
  214.     else:
  215.         filetype += ""
  216.     return filetype
  217.  
  218.  
  219. # This is the start of the program
  220.  
  221. if len(sys.argv) == 3:
  222.     # This checks if two arguments were given and
  223.     # utilises them as the transaction then the filename to save
  224.     try:
  225.         BLOCKCHAINADDRESS = str(sys.argv[1])
  226.         FILENAME = str(sys.argv[2])
  227.     except IndexError:
  228.         print("No address of filename")
  229.  
  230. elif len(sys.argv) == 2:
  231.     # This checks if one argument was given
  232.     # and uses it as the transaction and default file name
  233.     try:
  234.         BLOCKCHAINADDRESS = str(sys.argv[1])
  235.         FILENAME = 'File'
  236.     except IndexError:
  237.         print("no address")
  238.  
  239.  
  240. elif len(sys.argv) == 1:
  241.     # This works if no arguments are given to allow the program to function
  242.     BLOCKCHAINADDRESS = raw_input('Enter the blockchain Address or transactions file:')
  243.     FILENAME = raw_input('Enter the file name you would like to save to')
  244.     if FILENAME == '':
  245.         # This gives a default filename
  246.         FILENAME = 'file'
  247.  
  248.  
  249. if BLOCKCHAINADDRESS.endswith(".txt"):
  250.     # This checks if you're giving a list of transactions or just one
  251.     fileofblocks()
  252.  
  253.  
  254. else:
  255.     if LOCAL:
  256.         printdataoutlocal(BLOCKCHAINADDRESS)
  257.  
  258.     else:
  259.         printdataoutonline(BLOCKCHAINADDRESS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement