diff --git a/opencanary/modules/telnet.py b/opencanary/modules/telnet.py index ee03a9f..3cac06c 100644 --- a/opencanary/modules/telnet.py +++ b/opencanary/modules/telnet.py @@ -1,14 +1,24 @@ 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 +@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): def dataReceived(self, data): @@ -24,7 +34,6 @@ def connectionLost(self, reason): return TelnetTransport.connectionLost(self, reason) - class AlertAuthTelnetProtocol(AuthenticatingTelnetProtocol): def connectionMade(self): # p/Cisco telnetd/ d/router/ o/IOS/ cpe:/a:cisco:telnet/ cpe:/o:cisco:ios/a @@ -68,9 +77,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)