Repository: Gallopsled/pwntools

Issue:
Connecting to remote broken in sagemath
Connecting to a target using `remote` fails in sage since we [don't convert the port to int](https://github.com/Gallopsled/pwntools/commit/3e4d4a2115348814141859adc82c63be9c500a81 ) anymore in 4.8.0.

```
$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.0, Release Date: 2020-01-01                     │
│ Using Python 3.8.10. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: from pwn import *
sage: remote('localhost', 1337)
[x] Opening connection to localhost on port 1337
[-] Opening connection to localhost on port 1337: Failed
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[2], line 1
----> 1 remote('localhost', Integer(1337))

File ~/dev/pwntools-upstream/pwnlib/tubes/remote.py:77, in remote.__init__(self, host, port, fam, typ, ssl, sock, ssl_context, ssl_args, sni, *args, **kwargs)
     75 fam = self._get_family(fam)
     76 try:
---> 77     self.sock   = self._connect(fam, typ)
     78 except socket.gaierror as e:
     79     if e.errno != socket.EAI_NONAME:

File ~/dev/pwntools-upstream/pwnlib/tubes/remote.py:103, in remote._connect(self, fam, typ)
    100 timeout = self.timeout
    102 with self.waitfor('Opening connection to %s on port %s' % (self.rhost, self.rport)) as h:
--> 103     for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE):
    104         self.family, self.type, self.proto, _canonname, sockaddr = res
    106         if self.type not in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:

File /usr/lib/python3.8/socket.py:918, in getaddrinfo(host, port, family, type, proto, flags)
    915 # We override this function since we want to translate the numeric family
    916 # and socket type values to enum constants.
    917 addrlist = []
--> 918 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    919     af, socktype, proto, canonname, sa = res
    920     addrlist.append((_intenum_converter(af, AddressFamily),
    921                      _intenum_converter(socktype, SocketKind),
    922                      proto, canonname, sa))

OSError: Int or String expected
```


Discussion:

Resolving patch:
diff --git a/pwnlib/tubes/listen.py b/pwnlib/tubes/listen.py
--- a/pwnlib/tubes/listen.py
+++ b/pwnlib/tubes/listen.py
@@ -80,6 +80,9 @@ def __init__(self, port=0, bindaddr='::',
                  fam='any', typ='tcp', *args, **kwargs):
         super(listen, self).__init__(*args, **kwargs)
 
+        # convert port to string for sagemath support
+        port = str(port)
+
         fam = self._get_family(fam)
         typ = self._get_type(typ)
 
diff --git a/pwnlib/tubes/remote.py b/pwnlib/tubes/remote.py
--- a/pwnlib/tubes/remote.py
+++ b/pwnlib/tubes/remote.py
@@ -61,7 +61,8 @@ def __init__(self, host, port,
                  *args, **kwargs):
         super(remote, self).__init__(*args, **kwargs)
 
-        self.rport  = port
+        # convert port to string for sagemath support
+        self.rport  = str(port)
         self.rhost  = host
 
         if sock:


Test patch: