Created
October 19, 2016 23:34
-
-
Save badstreff/9f4fce31151f3b5004a2efab032eaacf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/opt/python3/bin/python3 | |
# Requirements: bencoder & requests | |
# - can be gotten via 'pip install bencoder requests' | |
from bencoder import encode, decode | |
import hashlib | |
import requests | |
import socket | |
from struct import unpack | |
#GLOBALS: | |
torrent_path = 'X-Men_ Apocalypse (2016) [1080p] [YTS.AG].torrent' | |
# Open torrent and read data into data variable | |
data = None | |
with open(torrent_path, 'rb') as f: | |
read_data = f.read() | |
data = decode(read_data) | |
# Generate hash of info field | |
info_hash = hashlib.sha1(encode(data[b'info'])).digest() | |
# Generate static peer_id, just for PoC | |
peer_id = bytes(range(20)) | |
# Calculate length of all torrent files, assume these are all multi-torrent files | |
length = 0 | |
for file in data[b'info'][b'files']: | |
length += file[b'length'] | |
# Make requests to a bunch of trackers | |
for tracker in data[b'announce-list']: | |
payload = {'info_hash': info_hash, 'peer_id': peer_id.decode("utf-8"), 'length': length, | |
'port': 6881, 'uploaded':0, 'downloaded':0, 'compact':1} | |
try: | |
r = requests.get('http://' + tracker[0].decode("utf-8")[6:], params=payload, timeout=3) | |
if(r.status_code == 200): | |
print('http://' + tracker[0].decode("utf-8")[6:]) | |
# Grab the peers and split them into manageable chunks | |
peers = decode(r.content)[b'peers'] | |
peers = [peers[i:i+6] for i in range(0, len(peers), 6)] | |
# For each peer: print the ip and unpack the last 2 bytes as an int for the port | |
for p in peers: | |
print(str(socket.inet_ntoa(p[:4])) + ':' + str(unpack(">H", p[4:])[0])) | |
except Exception as e: | |
print(e) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment