Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: make socks and serial optional #2245

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: |
set -x
pip install pylint
pip install --upgrade -e .
pip install --upgrade -e .[full]
pylint --exit-zero --errors-only pwnlib -f parseable | cut -d ' ' -f2- > current.txt
git fetch origin
git checkout origin/"$GITHUB_BASE_REF"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Python3 is suggested, but Pwntools still works with Python 2.7. Most of the fun
sudo apt-get update
sudo apt-get install python3 python3-pip python3-dev git libssl-dev libffi-dev build-essential
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade pwntools
python3 -m pip install --upgrade pwntools[full]
```


Expand Down
7 changes: 6 additions & 1 deletion pwn/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import platform
import re
import socks
import signal
import string
import struct
Expand Down Expand Up @@ -84,12 +83,18 @@
debug = log.debug
success = log.success

# optional deps
try:
import colored_traceback
except ImportError:
pass
else:
colored_traceback.add_hook()

try:
import socks
except ImportError:
pass

# Equivalence with the default behavior of "from import *"
# __all__ = [x for x in tuple(globals()) if not x.startswith('_')]
4 changes: 2 additions & 2 deletions pwnlib/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import threading
import time

import socks

from pwnlib.config import register_config
from pwnlib.device import Device
from pwnlib.timeout import Timeout
Expand Down Expand Up @@ -1281,6 +1279,8 @@ def proxy(self, proxy):
socket.socket = _original_socket
return None

import socks # keep dependency optional

if isinstance(proxy, str):
proxy = (socks.SOCKS5, proxy)

Expand Down
1 change: 0 additions & 1 deletion pwnlib/protocols/adb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pwnlib.util.lists import group
from pwnlib.util.misc import size
from pwnlib.util.packing import p32
from pwnlib.util.proc import pidof
from pwnlib.util.sh_string import sh_string

log = getLogger(__name__)
Expand Down
18 changes: 10 additions & 8 deletions pwnlib/tubes/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import division

import socket
import socks

from pwnlib.log import getLogger
from pwnlib.timeout import Timeout
Expand Down Expand Up @@ -41,7 +40,7 @@ class remote(sock):
>>> r = remote('127.0.0.1', 1)
Traceback (most recent call last):
...
PwnlibException: Could not connect to 127.0.0.1 on port 1
ConnectionRefusedError: [Errno 111] Connection refused

You can also use :meth:`.remote.fromsocket` to wrap an existing socket.

Expand Down Expand Up @@ -97,11 +96,12 @@ def __init__(self, host, port,
self.sock = ssl_context.wrap_socket(self.sock,**ssl_args)

def _connect(self, fam, typ):
sock = None
err = None
sock = None
timeout = self.timeout

with self.waitfor('Opening connection to %s on port %s' % (self.rhost, self.rport)) as h:
for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE):
for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ):
self.family, self.type, self.proto, _canonname, sockaddr = res

if self.type not in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
Expand All @@ -119,11 +119,13 @@ def _connect(self, fam, typ):

try:
sock.connect(sockaddr)
err = None # break ref cycle
return sock
except socks.ProxyError:
raise
except socket.error:
pass
except IOError as e:
if err is None:
err = e
if err is not None:
raise err
self.error("Could not connect to %s on port %s", self.rhost, self.rport)

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions pwnlib/tubes/serialtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import sys
import time

import serial

from pwnlib.log import getLogger
from pwnlib.tubes import tube

Expand All @@ -30,6 +28,9 @@ def __init__(
self.convert_newlines = convert_newlines
# serial.Serial might throw an exception, which must be handled
# and propagated accordingly using self.exception

import serial

try:
self.conn = serial.Serial(
port = port,
Expand Down
20 changes: 12 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,35 @@ keywords = ["pwntools", "exploit", "ctf", "capture", "the", "flag", "binary", "w

requires-python = ">=2.7"
dependencies = [
"paramiko>=1.15.2",
"mako>=1.0.0",
"pyelftools>=0.29, <0.30; python_version < '3'",
"pyelftools>=0.29; python_version >= '3'",
"capstone>=3.0.5rc2", # see Gallopsled/pwntools#971, Gallopsled/pwntools#1160
"ropgadget>=5.3",
"pyserial>=2.7",
"requests>=2.0",
"pip>=6.0.8",
"pygments>=2.0",
"pysocks",
"python-dateutil",
"packaging",
"psutil>=3.3.0",
"intervaltree>=3.0",
"sortedcontainers",
"unicorn>=2.0.1",
"six>=1.12.0",
"rpyc",
"colored_traceback",
"pathlib2; python_version < '3.4'",
"unix-ar; python_version >= '3'",
"zstandard",
]

[project.optional-dependencies]
full = [
"capstone>=3.0.5rc2", # see Gallopsled/pwntools#971, Gallopsled/pwntools#1160
"colored_traceback",
"paramiko>=1.15.2",
"pyserial>=2.7",
"pysocks",
"requests>=2.0",
"rpyc",
"unicorn>=2.0.1",
]

[project.urls]
homepage = "https://pwntools.com"
download = "https://github.com/Gallopsled/pwntools/releases"
Expand Down
Loading