Skip to content

Commit

Permalink
Implement basic tarpitting for ssh module
Browse files Browse the repository at this point in the history
Ref #5
  • Loading branch information
webknjaz committed Nov 18, 2018
1 parent 72783c5 commit 72e49d9
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions opencanary/modules/ssh.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime, timedelta

from opencanary.modules import CanaryService

import twisted
Expand All @@ -6,6 +8,8 @@
from twisted.conch.checkers import SSHPublicKeyDatabase
from twisted.conch.ssh import factory, userauth, connection, keys, session, transport
from twisted.internet import reactor, protocol, defer
from twisted.internet.defer import inlineCallbacks
from twisted.internet.task import deferLater
from twisted. application import internet

from zope.interface import implementer
Expand Down Expand Up @@ -107,8 +111,29 @@ def auth_publickey(self, packet):
self._ebPassword)

def ssh_USERAUTH_REQUEST(self, packet):
self.sendBanner()
return userauth.SSHUserAuthServer.ssh_USERAUTH_REQUEST(self, packet)
@inlineCallbacks
def deferred_auth(auth_server_instance):
attempt_time = datetime.now()
auth_server_instance.sendBanner()

peer = auth_server_instance.transport.getPeer()
attempt_key = peer.address.host, auth_server_instance.user

previous_attempt_time = auth_server_instance.transport.factory.auth_attempts.get(attempt_key)
auth_server_instance.transport.factory.auth_attempts[attempt_key] = attempt_time

if (
previous_attempt_time and
attempt_time - previous_attempt_time < timedelta(seconds=3)
):
yield deferLater(reactor, 30, lambda: None) # sleep

res = yield userauth.SSHUserAuthServer.ssh_USERAUTH_REQUEST(
auth_server_instance, packet,
)
return res

return deferred_auth(self)

# As implemented by Kojoney
class HoneyPotSSHFactory(factory.SSHFactory):
Expand All @@ -131,6 +156,7 @@ def __init__(self, logger=None, version=None):
self.sessions = {}
self.logger = logger
self.version = version
self.auth_attempts = {}

def buildProtocol(self, addr):
# FIXME: try to mimic something real 100%
Expand Down

0 comments on commit 72e49d9

Please sign in to comment.