Search code examples
python-3.xtor

Instagram login with tor


i am trying to login to instagram through tor (i am using python3 on a linux machine if this helps!) here is the code:

import json
import requests
import os
from colorama import Fore
from stem import Signal
from stem.control import Controller

def tor_session():
session = requests.session()
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
return session

def login(username, password):
# params:
# [string]username- the username of the instagram account to log in to
# [string]password- the password to use in the log in process

# description:
# logs in to the account with the specified username and with the specified password

# session setup
sess = tor_session()
sess.cookies.update({
    'sessionid': '',
    'mid': '',
    'ig_pr': '1',
    'ig_vw': '1920',
    'csrftoken': '',
    's_network': '',
    'ds_user_id': ''
})
sess.headers.update({
    'UserAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
    'x-instagram-ajax': '1',
    'X-Requested-With': 'XMLHttpRequest',
    'origin': 'https://www.instagram.com',
    'ContentType': 'application/x-www-form-urlencoded',
    'Connection': 'keep-alive',
    'Accept': '*/*',
    'Referer': 'https://www.instagram.com',
    'authority': 'www.instagram.com',
    'Host': 'www.instagram.com',
    'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
    'Accept-Encoding': 'gzip, deflate'
})

# get csrftoken and the instagram main page
r = sess.get('https://www.instagram.com/')
sess.headers.update({'X-CSRFToken': r.cookies.get_dict()['csrftoken']})

# log in
data = {'username': username, 'password': password}
r = sess.post('https://www.instagram.com/accounts/login/ajax/', data=data, allow_redirects=True)
token = r.cookies.get_dict()['csrftoken']
sess.headers.update({'X-CSRFToken': token})

# parse the response from the log in
data = json.loads(r.text)
print(data)
if data['status'] == 'fail':
    return None
if data['authenticated']:
    return True
else:
    return False

login("username", "password")

the problem is that almost every time i have tried to run this it didnt work and threw an exception:

Traceback (most recent call last):
File "main.py", line 156, in <module>
main()
File "main.py", line 152, in main
brute_force(username, pass_file_path)
File "main.py", line 114, in brute_force
logged_in = login(username, password)
File "main.py", line 81, in login
sess.headers.update({'X-CSRFToken': r.cookies.get_dict()['csrftoken']})
KeyError: 'csrftoken'

and sometimes it threw this exception:

File "main.py", line 94, in login
if data['authenticated']:
KeyError: 'authenticated'

how can i fix this? i tried restarting tor changing its configs but nothing works please help if you can!


Solution

  • It appears that Instagram doesn't set cookies for tor users:

    >>> s = your_setup_code_for_session()
    >>> r = s.get('https://www.instagram.com')
    >>> r.cookies.get_dict()
    {}
    

    I also tested this using the tor browser and got the same results:

    enter image description here

    It looks like you'll need to use a vpn or a Tor + vpn combination.