Skip to content

Commit

Permalink
fixed ssl error when using aio mode and fofa requests
Browse files Browse the repository at this point in the history
  • Loading branch information
antx-code committed May 7, 2022
1 parent 0b01de1 commit c11d3a2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pocx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.2.0'
__version__ = '0.2.1'

from .aio_poc import AioPoc
from .basic_poc import BasicPoc
13 changes: 12 additions & 1 deletion pocx/aio_poc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
import asyncio
import httpx
import urllib3
import ssl

try:
ssl_context = httpx.create_ssl_context()
except:
ssl_context = ssl.create_default_context()
ssl_context.options ^= ssl.OP_NO_TLSv1 # Enable TLS 1.0 back

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
httpx._config.DEFAULT_CIPHERS += ":ALL:@SECLEVEL=1"

Expand All @@ -14,7 +22,10 @@ def __init__(self) -> None:
self.name = "AioPoc"
self.cve = ''
self.example = ''
self.session = httpx.AsyncClient(verify=False)
try:
self.session = httpx.AsyncClient(verify=False)
except Exception as e:
self.session = httpx.AsyncClient(verify=ssl_context)
self.session.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"}
logger.info(f'Testing {self.name} with {self.mode}.')

Expand Down
13 changes: 12 additions & 1 deletion pocx/basic_poc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
from loguru import logger
import httpx
import urllib3
import ssl

try:
ssl_context = httpx.create_ssl_context()
except:
ssl_context = ssl.create_default_context()
ssl_context.options ^= ssl.OP_NO_TLSv1 # Enable TLS 1.0 back

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
httpx._config.DEFAULT_CIPHERS += ":ALL:@SECLEVEL=1"

Expand All @@ -13,7 +21,10 @@ def __init__(self) -> None:
self.name = "BasicPoc"
self.cve = ''
self.example = ''
self.session = httpx.Client(verify=False)
try:
self.session = httpx.Client(verify=False)
except Exception as e:
self.session = httpx.Client(verify=ssl_context)
self.session.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"}
logger.info(f'Testing {self.name} with {self.mode}.')

Expand Down
36 changes: 26 additions & 10 deletions pocx/funcs/fofa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
import json
import base64
from loguru import logger
import urllib3
import ssl

try:
ssl_context = httpx.create_ssl_context()
except:
ssl_context = ssl.create_default_context()
ssl_context.options ^= ssl.OP_NO_TLSv1 # Enable TLS 1.0 back

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
httpx._config.DEFAULT_CIPHERS += ":ALL:@SECLEVEL=1"


class Fofa():
Expand Down Expand Up @@ -60,14 +71,13 @@ def _search(self, grammar: str, page: int = 1, size: int = 100):
furl = f'https://{self.domain}/api/v1/search/all?email={self.email}&key={self.key}&qbase64={b64}&{grammar}&page={page}&size={size}'
try:
assets = httpx.get(furl).content.decode('utf-8')
result = json.loads(assets)
if not result['error']:
return result
logger.error(f'Fofa API error: {result["errmsg"]}')
return None
except Exception as e:
logger.error(e)
return None
except Exception as _:
assets = httpx.get(furl, verify=ssl_context).content.decode('utf-8')
result = json.loads(assets)
if not result['error']:
return result
logger.error(f'Fofa API error: {result["errmsg"]}')
return None

@logger.catch(level='ERROR')
def assets(self, grammar: str, page: int = 1, size: int = 100):
Expand Down Expand Up @@ -116,6 +126,12 @@ def asset_pages(self, grammar: str, size: int = 100):
results = self._search(grammar, 1, 1)
if not results:
return 1
count = results['size'] % size
pages = results['size'] // size if count == 0 else results['size'] // size + 1
all_counts = results['size']
if all_counts >= 10000:
logger.warning("Fofa's asset counts is {all_counts}, which is too much, so we only search the first 10000.")
count = 10000 % size
pages = 10000 // size if count == 0 else 10000 // size + 1
else:
count = results['size'] % size
pages = all_counts // size if count == 0 else all_counts // size + 1
return pages
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pocx"
version = "0.2.0"
version = "0.2.1"
description = "A Simple, Fast and Powerful poc engine tools was built by antx, which support synchronous mode and asynchronous mode."
authors = ["antx <wkaifeng2007@163.com>"]
license = "MIT"
Expand Down

0 comments on commit c11d3a2

Please sign in to comment.