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

Make sock.connected much more robust by using socket.poll #817

Merged
merged 2 commits into from
Dec 16, 2016

Conversation

zachriggle
Copy link
Member

The current code for sock.can_recv_raw and sock.connected_raw are a bit lacking, and only show what we know about the socket.

Specifically,

  • can_recv_raw() will return True if the socket has closed
    • An attempt to recv will immediately throw EOFError
    • This is undesirable and incorrect, per the documentation on tube.can_recv
  • connected() only knows about the internal state
    • If the remote side disconnects, and while sock.connected(): time.sleep(1) will never return
    • This is undesirable and incorrect, since we can't detect disconnections

This adds a bit of code using socket.poll for connected_raw(), and recv(..., MSG_PEEK) for can_recv_raw().

@zachriggle zachriggle added this to the Someday milestone Dec 16, 2016
@zachriggle zachriggle self-assigned this Dec 16, 2016
@zachriggle zachriggle merged commit 70c9633 into Gallopsled:dev Dec 16, 2016
@zachriggle
Copy link
Member Author

For posterity, here's a simple test:

>>> l=listen()
[x] Trying to bind to 0.0.0.0 on port 0
[x] Trying to bind to 0.0.0.0 on port 0: Trying 0.0.0.0
[+] Trying to bind to 0.0.0.0 on port 0: Done
[x] Waiting for connections on 0.0.0.0:55361
>>> r=remote('localhost', l.lport)
[x] Opening connection to localhost on port 55361
[x] Opening connection to localhost on port 55361: Trying ::1
[x] Opening connection to localhost on port 55361: Trying 127.0.0.1
[+] Opening connection to localhost on port 55361: Done
[+] Waiting for connections on 0.0.0.0:55361: Got connection from 127.0.0.1 on port 55363
>>> l.close()
[*] Closed connection to 127.0.0.1 port 55363
>>> r.wait_for_close() #<--- Never returns, before this pull request

@zachriggle zachriggle modified the milestones: 3.4.0, Someday Dec 16, 2016
Copy link
Contributor

@TethysSvensson TethysSvensson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zachriggle zachriggle deleted the socket-connected-canrecv branch January 4, 2017 18:00
Kyle-Kyle pushed a commit to Kyle-Kyle/pwntools that referenced this pull request Apr 25, 2021
* Py3k

* Dont run py2 on CI

* Don't launch futurize on CI

* Move to latest isort options

* Modernize imports (isort>=5)

* Fix removed jump dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants