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

Fix regression in telnet service #321

Merged
merged 3 commits into from
Oct 31, 2023
Merged
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
22 changes: 21 additions & 1 deletion opencanary/modules/telnet.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
from opencanary.modules import CanaryService

from zope.interface import implementer
from twisted.application import internet
from twisted.internet.error import ConnectionDone, ConnectionLost
from twisted.internet import protocol
from twisted.cred import portal
from twisted.cred import credentials
from twisted.conch.telnet import AuthenticatingTelnetProtocol
from twisted.conch.telnet import ITelnetProtocol
from twisted.conch.telnet import TelnetTransport
from twisted.conch.telnet import ECHO
from twisted.spread.pb import Avatar


class MyTelnet(Avatar):
def __init__(self, name):
self.name = name


@implementer(portal.IRealm)
class Realm:
def requestAvatar(self, avatarId, mind, *interfaces):
if ITelnetProtocol in interfaces:
av = MyTelnet()
av.state = "Command"
return ITelnetProtocol, av, lambda: None
raise NotImplementedError("Not supported by this realm")


class CanaryTelnetTransport(TelnetTransport):
Expand Down Expand Up @@ -68,9 +86,11 @@ def __init__(self, config=None, logger=None):
self.banner += b"\n"

def getService(self):
r = Realm()
p = portal.Portal(r)
f = protocol.ServerFactory()
f.canaryservice = self
f.logger = self.logger
f.banner = self.banner
f.protocol = lambda: CanaryTelnetTransport(AlertAuthTelnetProtocol)
f.protocol = lambda: CanaryTelnetTransport(AlertAuthTelnetProtocol, p)
return internet.TCPServer(self.port, f, interface=self.listen_addr)