Skip to content

Commit

Permalink
Faster shutdown during port-scan mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cytopia committed Jun 14, 2020
1 parent fc75c8a commit 7749bb1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
</tbody>
</table>

> <sup>[1] <a href="https://cytopia.github.io/pwncat/pwncat.type.html">mypy type coverage</a> <strong>(fully typed: 93.78%)</strong></sup><br/>
> <sup>[1] <a href="https://cytopia.github.io/pwncat/pwncat.type.html">mypy type coverage</a> <strong>(fully typed: 93.83%)</strong></sup><br/>
> <sup>[2] Linux builds are currently only failing, due to loss of IPv6 support: <a href="https://github.com/actions/virtual-environments/issues/929">Issue</a></sup><br/>
> <sup>[3] Windows builds are currently only failing, because they are simply stuck on GitHub actions: <a href="https://github.com/actions/virtual-environments/issues/917">Issue</a></sup>
Expand Down
15 changes: 10 additions & 5 deletions bin/pwncat
Original file line number Diff line number Diff line change
Expand Up @@ -1736,29 +1736,34 @@ class Sock(_Singleton("SingletonMeta", (object,), {})): # type: ignore
# be shutdown and we want to throw any socket at this function.
pass

def close(self, sock, name):
def close(self, sock, name): # pylint: disable=unused-argument,no-self-use
# type: (socket.socket, str) -> None
"""Shuts down and closes a socket.
Args:
sock (socket.socket): Socket to shutdown and close.
name (str): Name of the socket used for logging purposes.
"""
# NOTE: Logging is removed here as this is too much overhead when using
# the port scanner (it will have thousands of threads and too many
# calls to the logger which will cause issues with its shutdown
# and a massive performance degrade as well.
try:
# (SHUT_RD) 0 = Done receiving (disallows receiving)
# (SHUT_WR) 1 = Done sending (disallows sending)
# (SHUT_RDWR) 2 = Both
self.__log.trace("Shutting down %s socket", name) # type: ignore
# self.__log.trace("Shutting down %s socket", name) # type: ignore
sock.shutdown(socket.SHUT_RDWR)
except (OSError, socket.error):
# We do not log errors here, as unconnected sockets cannot
# be shutdown and we want to throw any socket at this function.
pass
try:
self.__log.trace("Closing %s socket", name) # type: ignore
# self.__log.trace("Closing %s socket", name) # type: ignore
sock.close()
except (OSError, socket.error) as error:
self.__log.trace("Could not close %s socket: %s", name, error) # type: ignore
except (OSError, socket.error):
pass
# self.__log.trace("Could not close %s socket: %s", name, error) # type: ignore


class Net(object):
Expand Down
43 changes: 28 additions & 15 deletions docs/pwncat.api.html
Original file line number Diff line number Diff line change
Expand Up @@ -1763,29 +1763,34 @@ <h1 class="title">Module <code>pwncat</code></h1>
# be shutdown and we want to throw any socket at this function.
pass

def close(self, sock, name):
def close(self, sock, name): # pylint: disable=unused-argument,no-self-use
# type: (socket.socket, str) -&gt; None
&#34;&#34;&#34;Shuts down and closes a socket.

Args:
sock (socket.socket): Socket to shutdown and close.
name (str): Name of the socket used for logging purposes.
&#34;&#34;&#34;
# NOTE: Logging is removed here as this is too much overhead when using
# the port scanner (it will have thousands of threads and too many
# calls to the logger which will cause issues with its shutdown
# and a massive performance degrade as well.
try:
# (SHUT_RD) 0 = Done receiving (disallows receiving)
# (SHUT_WR) 1 = Done sending (disallows sending)
# (SHUT_RDWR) 2 = Both
self.__log.trace(&#34;Shutting down %s socket&#34;, name) # type: ignore
# self.__log.trace(&#34;Shutting down %s socket&#34;, name) # type: ignore
sock.shutdown(socket.SHUT_RDWR)
except (OSError, socket.error):
# We do not log errors here, as unconnected sockets cannot
# be shutdown and we want to throw any socket at this function.
pass
try:
self.__log.trace(&#34;Closing %s socket&#34;, name) # type: ignore
# self.__log.trace(&#34;Closing %s socket&#34;, name) # type: ignore
sock.close()
except (OSError, socket.error) as error:
self.__log.trace(&#34;Could not close %s socket: %s&#34;, name, error) # type: ignore
except (OSError, socket.error):
pass
# self.__log.trace(&#34;Could not close %s socket: %s&#34;, name, error) # type: ignore


class Net(object):
Expand Down Expand Up @@ -15121,29 +15126,33 @@ <h2 id="args">Args</h2>
# be shutdown and we want to throw any socket at this function.
pass

def close(self, sock, name):
def close(self, sock, name): # pylint: disable=unused-argument,no-self-use
# type: (socket.socket, str) -&gt; None
&#34;&#34;&#34;Shuts down and closes a socket.

Args:
sock (socket.socket): Socket to shutdown and close.
name (str): Name of the socket used for logging purposes.
&#34;&#34;&#34;
# NOTE: Logging is removed here as this is too much overhead when using
# the port scanner (it will have thousands of threads and too many
# calls to the logger which will cause issues with its shutdown
# and a massive performance degrade as well.
try:
# (SHUT_RD) 0 = Done receiving (disallows receiving)
# (SHUT_WR) 1 = Done sending (disallows sending)
# (SHUT_RDWR) 2 = Both
self.__log.trace(&#34;Shutting down %s socket&#34;, name) # type: ignore
# self.__log.trace(&#34;Shutting down %s socket&#34;, name) # type: ignore
sock.shutdown(socket.SHUT_RDWR)
except (OSError, socket.error):
# We do not log errors here, as unconnected sockets cannot
# be shutdown and we want to throw any socket at this function.
pass
try:
self.__log.trace(&#34;Closing %s socket&#34;, name) # type: ignore
# self.__log.trace(&#34;Closing %s socket&#34;, name) # type: ignore
sock.close()
except (OSError, socket.error) as error:
self.__log.trace(&#34;Could not close %s socket: %s&#34;, name, error) # type: ignore</code></pre>
except (OSError, socket.error):
pass</code></pre>
</details>
<h3>Ancestors</h3>
<ul class="hlist">
Expand Down Expand Up @@ -15401,29 +15410,33 @@ <h2 id="args">Args</h2>
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def close(self, sock, name):
<pre><code class="python">def close(self, sock, name): # pylint: disable=unused-argument,no-self-use
# type: (socket.socket, str) -&gt; None
&#34;&#34;&#34;Shuts down and closes a socket.

Args:
sock (socket.socket): Socket to shutdown and close.
name (str): Name of the socket used for logging purposes.
&#34;&#34;&#34;
# NOTE: Logging is removed here as this is too much overhead when using
# the port scanner (it will have thousands of threads and too many
# calls to the logger which will cause issues with its shutdown
# and a massive performance degrade as well.
try:
# (SHUT_RD) 0 = Done receiving (disallows receiving)
# (SHUT_WR) 1 = Done sending (disallows sending)
# (SHUT_RDWR) 2 = Both
self.__log.trace(&#34;Shutting down %s socket&#34;, name) # type: ignore
# self.__log.trace(&#34;Shutting down %s socket&#34;, name) # type: ignore
sock.shutdown(socket.SHUT_RDWR)
except (OSError, socket.error):
# We do not log errors here, as unconnected sockets cannot
# be shutdown and we want to throw any socket at this function.
pass
try:
self.__log.trace(&#34;Closing %s socket&#34;, name) # type: ignore
# self.__log.trace(&#34;Closing %s socket&#34;, name) # type: ignore
sock.close()
except (OSError, socket.error) as error:
self.__log.trace(&#34;Could not close %s socket: %s&#34;, name, error) # type: ignore</code></pre>
except (OSError, socket.error):
pass</code></pre>
</details>
</dd>
<dt id="pwncat.Sock.connect"><code class="name flex">
Expand Down
31 changes: 19 additions & 12 deletions docs/pwncat.type.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ <h1>Mypy Type Check Coverage Summary</h1>
</tr></thead>
<tfoot><tr class="summary summary-quality-1">
<th class="summary summary-filename">Total</th>
<th class="summary summary-precision">6.22% imprecise</th>
<th class="summary summary-lines">6058 LOC</th>
<th class="summary summary-precision">6.17% imprecise</th>
<th class="summary summary-lines">6063 LOC</th>
</tr></tfoot>
<tbody><tr class="summary summary-quality-1">
<td class="summary summary-filename">bin/pwncat</td>
<td class="summary summary-precision">6.22% imprecise</td>
<td class="summary summary-lines">6058 LOC</td>
<td class="summary summary-precision">6.17% imprecise</td>
<td class="summary summary-lines">6063 LOC</td>
</tr></tbody>
</table>
</body>
Expand Down Expand Up @@ -6093,6 +6093,11 @@ <h2>pwncat</h2>
<span id="L6056" class="lineno"><a class="lineno" href="#L6056">6056</a></span>
<span id="L6057" class="lineno"><a class="lineno" href="#L6057">6057</a></span>
<span id="L6058" class="lineno"><a class="lineno" href="#L6058">6058</a></span>
<span id="L6059" class="lineno"><a class="lineno" href="#L6059">6059</a></span>
<span id="L6060" class="lineno"><a class="lineno" href="#L6060">6060</a></span>
<span id="L6061" class="lineno"><a class="lineno" href="#L6061">6061</a></span>
<span id="L6062" class="lineno"><a class="lineno" href="#L6062">6062</a></span>
<span id="L6063" class="lineno"><a class="lineno" href="#L6063">6063</a></span>
</pre></td>
<td class="table-code"><pre><span class="line-empty" title="No Anys on this line!">#!/usr/bin/env python3</span>
<span class="line-empty" title="No Anys on this line!">"""pwncat."""</span>
Expand Down Expand Up @@ -7945,32 +7950,34 @@ <h2>pwncat</h2>
<span class="line-empty" title="No Anys on this line!"> # be shutdown and we want to throw any socket at this function.</span>
<span class="line-precise" title="No Anys on this line!"> pass</span>
<span class="line-empty" title="No Anys on this line!"></span>
<span class="line-precise" title="No Anys on this line!"> def close(self, sock, name):</span>
<span class="line-precise" title="No Anys on this line!"> def close(self, sock, name): # pylint: disable=unused-argument,no-self-use</span>
<span class="line-empty" title="No Anys on this line!"> # type: (socket.socket, str) -&gt; None</span>
<span class="line-empty" title="No Anys on this line!"> """Shuts down and closes a socket.</span>
<span class="line-empty" title="No Anys on this line!"></span>
<span class="line-empty" title="No Anys on this line!"> Args:</span>
<span class="line-empty" title="No Anys on this line!"> sock (socket.socket): Socket to shutdown and close.</span>
<span class="line-empty" title="No Anys on this line!"> name (str): Name of the socket used for logging purposes.</span>
<span class="line-empty" title="No Anys on this line!"> """</span>
<span class="line-empty" title="No Anys on this line!"> # NOTE: Logging is removed here as this is too much overhead when using</span>
<span class="line-empty" title="No Anys on this line!"> # the port scanner (it will have thousands of threads and too many</span>
<span class="line-empty" title="No Anys on this line!"> # calls to the logger which will cause issues with its shutdown</span>
<span class="line-empty" title="No Anys on this line!"> # and a massive performance degrade as well.</span>
<span class="line-empty" title="No Anys on this line!"> try:</span>
<span class="line-empty" title="No Anys on this line!"> # (SHUT_RD) 0 = Done receiving (disallows receiving)</span>
<span class="line-empty" title="No Anys on this line!"> # (SHUT_WR) 1 = Done sending (disallows sending)</span>
<span class="line-empty" title="No Anys on this line!"> # (SHUT_RDWR) 2 = Both</span>
<span class="line-any" title="Any Types on this line:
Error (x2)"> self.__log.trace("Shutting down %s socket", name) # type: ignore</span>
<span class="line-empty" title="No Anys on this line!"> # self.__log.trace("Shutting down %s socket", name) # type: ignore</span>
<span class="line-precise" title="No Anys on this line!"> sock.shutdown(socket.SHUT_RDWR)</span>
<span class="line-precise" title="No Anys on this line!"> except (OSError, socket.error):</span>
<span class="line-empty" title="No Anys on this line!"> # We do not log errors here, as unconnected sockets cannot</span>
<span class="line-empty" title="No Anys on this line!"> # be shutdown and we want to throw any socket at this function.</span>
<span class="line-precise" title="No Anys on this line!"> pass</span>
<span class="line-empty" title="No Anys on this line!"> try:</span>
<span class="line-any" title="Any Types on this line:
Error (x2)"> self.__log.trace("Closing %s socket", name) # type: ignore</span>
<span class="line-empty" title="No Anys on this line!"> # self.__log.trace("Closing %s socket", name) # type: ignore</span>
<span class="line-precise" title="No Anys on this line!"> sock.close()</span>
<span class="line-precise" title="No Anys on this line!"> except (OSError, socket.error) as error:</span>
<span class="line-any" title="Any Types on this line:
Error (x2)"> self.__log.trace("Could not close %s socket: %s", name, error) # type: ignore</span>
<span class="line-precise" title="No Anys on this line!"> except (OSError, socket.error):</span>
<span class="line-precise" title="No Anys on this line!"> pass</span>
<span class="line-empty" title="No Anys on this line!"> # self.__log.trace("Could not close %s socket: %s", name, error) # type: ignore</span>
<span class="line-empty" title="No Anys on this line!"></span>
<span class="line-empty" title="No Anys on this line!"></span>
<span class="line-precise" title="No Anys on this line!">class Net(object):</span>
Expand Down
4 changes: 2 additions & 2 deletions tests/smoke/300---tcp---port_scan---no_banner/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ source "${SOURCEPATH}"
# -------------------------------------------------------------------------------------------------
# SETTINGS
# -------------------------------------------------------------------------------------------------
WAIT_STARTUP=10
WAIT_SHUTDOWN=10
WAIT_STARTUP=7
WAIT_SHUTDOWN=15
NAME1="scanner"

# -------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/smoke/301---tcp---port_scan---with_banner/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ source "${SOURCEPATH}"
# -------------------------------------------------------------------------------------------------
# SETTINGS
# -------------------------------------------------------------------------------------------------
WAIT_STARTUP=10
WAIT_SHUTDOWN=10
WAIT_STARTUP=7
WAIT_SHUTDOWN=15
NAME1="scanner"

# -------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/smoke/302---udp---port_scan---no_banner/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ source "${SOURCEPATH}"
# -------------------------------------------------------------------------------------------------
# SETTINGS
# -------------------------------------------------------------------------------------------------
WAIT_STARTUP=10
WAIT_SHUTDOWN=10
WAIT_STARTUP=7
WAIT_SHUTDOWN=15
NAME1="scanner"

# -------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/smoke/303---udp---port_scan---with_banner/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ source "${SOURCEPATH}"
# -------------------------------------------------------------------------------------------------
# SETTINGS
# -------------------------------------------------------------------------------------------------
WAIT_STARTUP=10
WAIT_SHUTDOWN=10
WAIT_STARTUP=7
WAIT_SHUTDOWN=15
NAME1="scanner"

# -------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 7749bb1

Please sign in to comment.