Skip to content

Commit 39d6dba

Browse files
committed
Fix security check for some phone numbers
1 parent 3a77831 commit 39d6dba

3 files changed

Lines changed: 71 additions & 61 deletions

File tree

vk_api/utils.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
@author: Kirill Python
5+
@contact: https://vk.com/python273
6+
@license Apache License, Version 2.0, see LICENSE file
7+
8+
Copyright (C) 2016
9+
"""
10+
11+
12+
def doc(method=None):
13+
""" Открывает документацию на метод или список всех методов
14+
15+
:param method: метод
16+
"""
17+
18+
if not method:
19+
method = 'methods'
20+
21+
url = 'https://vk.com/dev/{}'.format(method)
22+
23+
import webbrowser
24+
webbrowser.open(url)
25+
26+
27+
def search_re(reg, string):
28+
""" Поиск по регулярке """
29+
s = reg.search(string)
30+
31+
if s:
32+
groups = s.groups()
33+
return groups[0]
34+
35+
36+
def clean_string(s):
37+
if s:
38+
return s.strip().replace(' ', '')
39+
40+
41+
def code_from_number(phone_prefix, phone_postfix, number):
42+
prefix_len = len(phone_prefix)
43+
postfix_len = len(phone_postfix)
44+
45+
if number[0] == '+':
46+
number = number[1:]
47+
48+
if (prefix_len + postfix_len) >= len(number):
49+
return
50+
51+
# Сравниваем начало номера
52+
if not number[:prefix_len] == phone_prefix:
53+
return
54+
55+
# Сравниваем конец номера
56+
if not number[-postfix_len:] == phone_postfix:
57+
return
58+
59+
return number[prefix_len:-postfix_len]

vk_api/vk_api.py

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
"""
1010

1111
import re
12-
import time
1312
import threading
13+
import time
1414

1515
import requests
1616

1717
import jconfig
18+
from .utils import doc, code_from_number, search_re, clean_string
1819

1920
DELAY = 0.34 # ~3 requests per second
2021
TOO_MANY_RPS_CODE = 6
@@ -29,8 +30,7 @@
2930
RE_AUTH_HASH = re.compile(r"hash: '([a-z_0-9]+)'")
3031
RE_TOKEN_URL = re.compile(r'location\.href = "(.*?)"\+addr;')
3132

32-
RE_PHONE_PREFIX = re.compile(r'phone_number">(.*?)<')
33-
RE_PHONE_PREFIX_2 = re.compile(r'label ta_r">\+(\d+)')
33+
RE_PHONE_PREFIX = re.compile(r'label ta_r">\+(.*?)<')
3434
RE_PHONE_POSTFIX = re.compile(r'phone_postfix">.*?(\d+).*?<')
3535

3636

@@ -134,6 +134,7 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
134134
response = self.http.get('https://vk.com/')
135135

136136
values = {
137+
'act': 'login',
137138
'role': 'al_frame',
138139
'_origin': 'https://vk.com',
139140
'utf8': '1',
@@ -148,18 +149,16 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
148149
'captcha_key': captcha_key
149150
})
150151

151-
response = self.http.post('https://login.vk.com/?act=login', values)
152-
153-
remixsid = None
152+
response = self.http.post('https://login.vk.com/', values)
154153

155154
if 'act=authcheck' in response.url: # TODO: test/fix
156155
code, remember_device = self.error_handlers[TWOFACTOR_CODE]()
157156
response = self.twofactor(response, code, remember_device)
158157

159-
if 'remixsid' in self.http.cookies:
160-
remixsid = self.http.cookies['remixsid']
161-
elif 'remixsid6' in self.http.cookies: # ipv6?
162-
remixsid = self.http.cookies['remixsid6']
158+
remixsid = (
159+
self.http.cookies.get('remixsid') or
160+
self.http.cookies.get('remixsid6')
161+
)
163162

164163
if remixsid:
165164
self.settings.remixsid = remixsid
@@ -228,11 +227,8 @@ def security_check(self, response=None):
228227
if 'security_check' not in response.url:
229228
return
230229

231-
phone_prefix = search_re(RE_PHONE_PREFIX, response.text)
232-
if not phone_prefix:
233-
phone_prefix = search_re(RE_PHONE_PREFIX_2, response.text)
234-
235-
phone_postfix = search_re(RE_PHONE_POSTFIX, response.text)
230+
phone_prefix = clean_string(search_re(RE_PHONE_PREFIX, response.text))
231+
phone_postfix = clean_string(search_re(RE_PHONE_POSTFIX, response.text))
236232

237233
code = None
238234
if self.sec_number:
@@ -461,51 +457,6 @@ def get_doc(self):
461457
doc(self._method)
462458

463459

464-
def doc(method=None):
465-
""" Открывает документацию на метод или список всех методов
466-
467-
:param method: метод
468-
"""
469-
470-
if not method:
471-
method = 'methods'
472-
473-
url = 'https://vk.com/dev/{}'.format(method)
474-
475-
import webbrowser
476-
webbrowser.open(url)
477-
478-
479-
def search_re(reg, string):
480-
""" Поиск по регулярке """
481-
s = reg.search(string)
482-
483-
if s:
484-
groups = s.groups()
485-
return groups[0]
486-
487-
488-
def code_from_number(phone_prefix, phone_postfix, number):
489-
prefix_len = len(phone_prefix)
490-
postfix_len = len(phone_postfix)
491-
492-
if number[0] == '+':
493-
number = number[1:]
494-
495-
if (prefix_len + postfix_len) >= len(number):
496-
return
497-
498-
# Сравниваем начало номера
499-
if not number[:prefix_len] == phone_prefix:
500-
return
501-
502-
# Сравниваем конец номера
503-
if not number[-postfix_len:] == phone_postfix:
504-
return
505-
506-
return number[prefix_len:-postfix_len]
507-
508-
509460
class AuthorizationError(Exception):
510461
pass
511462

vk_api/vk_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import sys
1313

1414
if sys.version_info[0] != 3:
15-
range = xrange # @ReservedAssignment @UndefinedVariable
15+
range = xrange
1616

1717

1818
class VkTools(object):

0 commit comments

Comments
 (0)