initial files

This commit is contained in:
Paulo Afonso Pinheiro 2020-04-12 21:58:55 -03:00
parent b9bdea44b5
commit a94c017a18
2 changed files with 105 additions and 2 deletions

View file

@ -1 +0,0 @@
/home/pa/work/ratio-spoof/bencode_parser.py

92
bencode_parser.py Normal file
View file

@ -0,0 +1,92 @@
import socket
import struct
import sys
from enum import Enum
import os
import json
import hashlib
import os
import urllib.parse
class BencodeKeys(Enum):
dic = 'd'
number= 'i'
arr = 'l'
end_of_collecion = 'e'
length_and_value_string_separator = ':'
def string_parse(startIdx, data:bytes):
current = startIdx
while data[current:current +1].decode() != BencodeKeys.length_and_value_string_separator.value:
current= current + 1
size = data[startIdx:current].decode()
string_nextidx = current+1 + int(size)
data_slice = data[current+1:current+1 + int(size)]
return (str(data_slice, 'utf-8', 'replace'), string_nextidx)
def number_parse(startIdx, data:bytes):
current = startIdx
while data[current:current +1].decode() != BencodeKeys.end_of_collecion.value:
current = current +1
number_nextidx = current +1
data_slice = data[startIdx +1:current]
return (int(data_slice), number_nextidx)
def find_parse(startIdx,data:bytes):
c = data[startIdx:startIdx +1].decode()
if(c == BencodeKeys.number.value):
return number_parse(startIdx, data)
elif(c == BencodeKeys.dic.value):
return dic_parse(startIdx,data)
elif(c == BencodeKeys.arr.value):
return list_parse(startIdx,data)
elif(str(c).isdigit()):
return string_parse(startIdx, data)
else:
raise Exception('Error parse')
def list_parse(startIdx, data):
result = []
current = startIdx +1
while current < len(data):
value, nextIdx = find_parse(current, data)
result.append(value)
current = nextIdx
if (data[current: current+1].decode()== BencodeKeys.end_of_collecion.value):
current = current +1
break
list_nextidx = current
return (result, list_nextidx)
def dic_parse(startIdx,data):
dic = {}
initial_dict_idx = startIdx
current = startIdx +1
while current < len(data):
key, nextIdx = find_parse(current, data)
current = nextIdx
value,nextIdx = find_parse(current, data)
dic[key] = value
current = nextIdx
if (data[current: current+1].decode()==BencodeKeys.end_of_collecion.value):
current = current +1
final_dict_idx = current
dic['byte_offsets'] = [initial_dict_idx,final_dict_idx]
break
dic_nextidx = current
return dic, dic_nextidx
def decode(data:bytes):
result,_ = find_parse(0,data)
return result

View file

@ -1 +0,0 @@
/home/pa/work/ratio-spoof/ratio-spoof.py

13
ratio-spoof.py Normal file
View file

@ -0,0 +1,13 @@
import bencode_parser
with open('/home/pa/Downloads/TorrentDB-1586737303638.txt', 'rb') as f:
raw_data = f.read()
result = bencode_parser.decode(raw_data)
print(result)
# offsets =data['info']['byte_offsets']
#info_hash = hashlib.sha1(raw_data[offsets[0]: offsets[1]]).hexdigest()
# sha1_hash =hashlib.sha1(raw_data[offsets[0]: offsets[1]])
#test = hashlib.sha1(raw_data[offsets[0]: offsets[1]]).digest()
#print(data['announce'])
#print(urllib.parse.quote(test).lower())