基于Twisted的Telnet简单蜜罐

0x00 前言

  利用Twisted端口绑定,读取socket的输入命令,然后给出预设的输出,此次实现的telnet蜜罐比较简单,就算是编程练习把。

0x01 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# coding:utf-8
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import logging
import re

WELCOME_MSG = 'Welcome to Microsoft Telnet Client '
LOGIN_SUCCESS = '*===============================================================\n\r\
Microsoft Telnet Server.\n\r\
*===============================================================\n\r'


class Telnet(LineReceiver):
def __init__(self):
self.name = None
self.password = None
self.state = 'get_name'
self.prefix = 'C:\\Users\\Administrator\>'
logging.basicConfig(filename='./telnet.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s: %(message)s', datefmt = '%Y-%m-%d %I:%M:%S')

def connectionMade(self):
self.sendLine(LOGIN_SUCCESS)
self.transport.write('login:')

def connectionLost(self, line):
logging.info("connection closed")
self.state = 'get_name'

def lineReceived(self, line):
if self.state == 'get_name':
self.name = line
self.state = 'get_pass'
self.transport.write('password:')
elif self.state == 'get_pass':
self.password = line
logging.info(self.name +' ' + self.password )
self.state = 'get_command'
self.transport.write(self.prefix)
else:
logging.info("command" + ' ' + line)
self.handle_command(line)

def handle_command(self, line):
command = line.strip().lower()
if command.startswith('dir'):
self.sendLine("")
self.sendLine("2016/04/11 17:55 <DIR> .")
self.sendLine("2016/04/11 17:55 <DIR> ..")
self.sendLine("2016/04/26 16:52 <DIR> Desktop")
self.sendLine("2016/04/11 17:42 <DIR> Documents")
self.sendLine("2016/04/11 17:42 <DIR> Downloads")
self.sendLine("2016/04/11 17:55 <DIR> Favorites")
self.sendLine("2016/04/11 17:55 <DIR> Links")
self.sendLine("2016/04/11 17:55 <DIR> Music")
self.sendLine("2016/04/11 17:55 <DIR> Pictures")
self.sendLine("2016/04/11 17:55 <DIR> Videos")
self.sendLine(" 0 File(s) 0 bytes")
self.sendLine(" 14 Dir(s) 33,003,470,848 bytes free ")
self.sendLine("")
self.transport.write(self.prefix)
elif command.startswith('whoami'):
self.sendLine("win-u5vm2v23fm5\\administrator")
self.sendLine("")
self.transport.write(self.prefix)
elif command.startswith('ipconfig'):
self.sendLine("Ethernet adapter 本地连接: ")
self.sendLine("Connection-specific DNS Suffix . :")
self.sendLine("Link-local IPv6 Address . . . . . : fe80::b424:11e8:941f:6fa3%11")
self.sendLine("IPv4 Address. . . . . . . . . . . : 192.168.1.105")
self.sendLine("Subnet Mask . . . . . . . . . . . : 255.255.255.0")
self.sendLine("Default Gateway . . . . . . . . . : 192.168.1.255.")
self.sendLine("")
self.transport.write(self.prefix)
elif command.startswith('cd'):
self.sendLine("Device not ready: I/O Error.")
self.sendLine("")
self.transport.write(self.prefix)
else:
self.sendLine("'" + line + "'" " is not recognized as an internal or external command,\n\roperable program or batch file.")
self.sendLine("")
self.transport.write(self.prefix)


class TelnetFactory(Factory):
def buildProtocol(self, addr):
return Telnet()


reactor.listenTCP(233, TelnetFactory())
reactor.run()

0x02 结果截图

1.连接登录界面:
登录
2.命令执行界面:
命令执行