在python中自动获取哈希Tor密码
我用python编写了一个脚本,它允许我使用stem更改公共IP。 脚本没有问题,但问题是脚本需要/etc/tor/torrc中的散列密码进行身份验证。 我希望所有其他人都使用我的脚本,但他们需要手动将哈希密码放入脚本中。 那么,是否有一个python脚本可以自动获取哈希密码 请不要使用tor-hash密码my_password,因为密码也必须存储在TORC中 我们将非常感谢您的帮助,谢谢 我制作了这个脚本,它使用tor-hash password和stem.process.launch\u tor\u with_config来使用哈希密码在python中自动获取哈希Tor密码,python,hash,tor,stem,Python,Hash,Tor,Stem,我用python编写了一个脚本,它允许我使用stem更改公共IP。 脚本没有问题,但问题是脚本需要/etc/tor/torrc中的散列密码进行身份验证。 我希望所有其他人都使用我的脚本,但他们需要手动将哈希密码放入脚本中。 那么,是否有一个python脚本可以自动获取哈希密码 请不要使用tor-hash密码my_password,因为密码也必须存储在TORC中 我们将非常感谢您的帮助,谢谢 我制作了这个脚本,它使用tor-hash password和stem.process.launch\u t
from stem.process import launch_tor_with_config
from stem.control import Controller
from subprocess import Popen, PIPE
import logging
def genTorPassHash(password):
""" Launches a subprocess of tor to generate a hashed <password> """
logging.info("Generating a hashed password")
torP = Popen(
['tor', '--hush', '--hash-password', str(password)],
stdout=PIPE,
bufsize=1
)
try:
with torP.stdout:
for line in iter(torP.stdout.readline, b''):
line = line.strip('\n')
if "16:" not in line:
logging.debug(line)
else:
passhash = line
torP.wait()
logging.info("Got hashed password")
return passhash
except Exception:
raise
def startTor(controlPass, config):
"""
Starts tor subprocess using a custom <config>,
returns Popen and connected controller.
"""
try:
# start tor
logging.info("Starting tor subprocess")
process = launch_tor_with_config(
config=config,
tor_cmd='tor',
completion_percent=50,
timeout=60,
take_ownership=True
)
logging.info("Connecting controller")
# create controller
control = Controller.from_port(
address="127.0.0.1",
port=int(config['ControlPort'])
)
# auth controller
control.authenticate(password=controlPass)
logging.info("Connected to tor process")
return process, control
except Exception as e:
logging.exception(e)
raise e
if __name__ == "__main__":
logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt="%H:%M:%S", level=logging.DEBUG)
password = raw_input('password: ')
password_hash = genTorPassHash(password)
config = {
'ClientOnly': '1',
'ControlPort': '9051',
'DataDirectory': '~/.tor/temp',
'Log': ['DEBUG stdout', 'ERR stderr' ],
'HashedControlPassword' : password_hash }
torProcess, torControl = startTor(password, config)
以下是如何在不使用tor的情况下实现这一点:
从操作系统导入urandom
从binascii导入b2a_hex
从hashlib导入sha1
def getTorPassHashsecret='password':
'
https://gist.github.com/jamesacampbell/2f170fc17a328a638322078f42e04cbc
'
静态“计数”值后来被引用为c
指示器=chr96
生成salt并附加指示符值,以便
盐=%s%s%8铀,指示剂
c=盐[8]
生成一个偶数,可在后续部分中进行划分。谢谢你,罗曼
EXPBIAS=6
计数=16+c&15>4+E
d=sha1
取盐并附加密码
tmp=盐[:8]+秘密
散列咸密码
slen=lentmp
计数时:
如果计数>slen:
d、 updatetmp
计数-=slen
其他:
d、 updatetmp[:计数]
计数=0
hash=d.digest
将所有内容合并为专有的Tor格式。
返回'16:%s%s%s'%b2a\u六角盐[:8]。上限,
b2a_六角指示器,
b2a_hexashed.upper
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
密码=原始输入密码:
password\u hash=getTorPassHashpassword
配置={
“ClientOnly”:“1”,
“控制端口”:“9051”,
“数据目录”:“~/.tor/temp”,
'Log':['DEBUG stdout','ERR stderr'],
'HashedControlPassword':密码\u hash}
torProcess,torControl=startTorpassword,配置
在Python 2 os.random中返回一个str,在Python 3 os.random中返回一个bytes, 代码需要稍微修改一下
import os, hashlib, binascii, codecs
def getTorPassHashv03(sectretPassword='passw0rd'):
#python v3 working
# static 'count' value later referenced as "c"
indicator = chr(96)
# generate salt and append indicator value so that it
salt = "%s%s" % (os.urandom(8), indicator) #this will be working
c = ord(indicator)
#salt = "%s%s" % (codecs.encode(os.urandom(4), 'hex').decode(), indicator) #this will be working
#c = ord(salt[8])
#salt = "%s%s" % (codecs.encode(os.urandom(8), 'hex').decode(), indicator) #this will be working
#c = ord(salt[16])
# generate an even number that can be divided in subsequent sections. (Thanks Roman)
EXPBIAS = 6
count = (16+(c&15)) << ((c>>4) + EXPBIAS)
d = hashlib.sha1()
# take the salt and append the password
tmp = salt[:8] + sectretPassword
# hash the salty password
slen = len(tmp)
while count:
if count > slen:
d.update(tmp.encode('utf-8'))
count -= slen
else:
d.update(tmp[:count].encode('utf-8'))
count = 0
hashed = d.digest()
saltRes = binascii.b2a_hex(salt[:8].encode('utf-8')).upper().decode()
indicatorRes = binascii.b2a_hex(indicator.encode('utf-8')).upper().decode()
torhashRes = binascii.b2a_hex(hashed).upper().decode()
hashedControlPassword = '16:' + str(saltRes) + str(indicatorRes) + str(torhashRes)
return hashedControlPassword
另外,通过在windows上生成Tor二进制文件的方法获得哈希,代码需要这样更改它
import subprocess, re
def genTorPassHashV00(sectretPassword='passw0rd'):
""" Launches a subprocess of tor to generate a hashed <password>"""
print('Generating a hashed password')
torP = subprocess.Popen(['tor.exe', '--hash-password', sectretPassword], stdout=subprocess.PIPE)
out, err = torP.communicate()
resultString = str(out)
match = re.search(r'(\\r\\n16:.{58})', resultString)
hashedControlPassword = re.sub(r'(\\r\\n)', "", match.group(0))
return hashedControlPassword
没有人吗我很绝望…用Cookie身份验证代替?散列密码是一种单向散列,因此在不知道或破坏它的情况下无法获取散列密码。那么,有没有一种方法可以让它在没有散列密码的情况下工作?它是什么?或者有没有一种方法可以让脚本进入torrc文件并读取散列密码?我认为这是可能的,但我不知道怎么做。