-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
269 lines (252 loc) · 8.18 KB
/
widget.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
initialcommand();
QDir dir("log");
if(!dir.exists(QCoreApplication::applicationDirPath()+"/logs"))
{
dir.mkdir(QCoreApplication::applicationDirPath()+"/logs");
}
buf = new char[16777215];
serverSocket = new QTcpServer();
connect(serverSocket, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
ui->cb_state->setEnabled(false);
ui->loging->setContextMenuPolicy(Qt::NoContextMenu);
//ui->loging->appendHtml("<head><style>.honeypot{color:red;}</style></head>");
on_pb_start_clicked();
}
Widget::~Widget()
{
delete[] buf;
delete ui;
}
void Widget::on_pb_start_clicked()
{
if(!serverSocket->isListening())
{
if (serverSocket->listen(QHostAddress(ui->le__ListingAddr->text().split(":")[0]), ui->le__ListingAddr->text().split(":")[1].toUShort()))
{
log("监听成功");
ui->cb_state->setChecked(true);
} else {
log("端口监听失败");
ui->cb_state->setChecked(false);
}
}
}
void Widget::on_pb_stop_clicked()
{
if(serverSocket->isListening())
{
serverSocket->close();
for(int i=0; i<clientSocket.length(); i++)
{
SQLsocket[clientSocket[i]]->close();
clientSocket[i]->close();
}
ui->cb_state->setChecked(false);
log("监听终止");
}
if(!ui->cb_holdlist->isChecked())
{
failcount.clear();
blacklist.clear();
}
}
void Widget::on_pb_restart_clicked()
{
on_pb_stop_clicked();
on_pb_start_clicked();
}
void Widget::acceptConnection()
{
QTcpSocket *Currentsocket = serverSocket->nextPendingConnection();
clientSocket.append(Currentsocket);
SQLsocket[Currentsocket]= new QTcpSocket(this);
SQLsocket[Currentsocket]->connectToHost(QHostAddress(ui->le_SQLAddr->text().split(":")[0]), ui->le_SQLAddr->text().split(":")[1].toUShort());
connect(Currentsocket, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(Currentsocket, SIGNAL(disconnected()), this, SLOT(clientdisconnected()));
connect(SQLsocket[Currentsocket], SIGNAL(readyRead()), this, SLOT(readSQLserver()));
//log("A new connection:" + QString::number(int(Currentsocket) & 0x0000FFFF,16));
}
void Widget::readClient()
{
char certsucces[11] = {7,0,0,1,0,0,0,2,0,0,0};
for(int i=0; i<clientSocket.length(); i++)
{
QString ipaddr = clientSocket[i]->peerAddress().toString();
if(blacklist.contains(ipaddr))
{
if(blacklist[ipaddr].secsTo(time.currentTime()) >= ui->sb_ttl->value())
{
//处理过期黑名单
failcount[ipaddr] = 0;
blacklist.remove(ipaddr);
}
}
if("Reject" == direction(ipaddr))
{
//黑名单最大尝试次数后拒绝连接
clientSocket[i]->close();
clientSocket.removeAll(clientSocket[i]);
continue;
}
qint64 size = clientSocket[i]->read(buf,16777215);
if(size == 0) continue;
if("Relay" == direction(ipaddr))
{
//转发消息
SQLsocket[clientSocket[i]]->write(buf,size);
if(buf[4] >= 0x00 && buf[4] <= 0x1C && buf[3] != 0x01)
{
log("[" + clientSocket[i]->peerAddress().toString()+ "] [" +command[buf[4]] + "] " + &buf[5]);
}
}
if("HoneyPot" == direction(ipaddr))
{
//进入蜜罐
certsucces[3] =buf[3]+1;
clientSocket[i]->write(certsucces,sizeof(certsucces));
if(buf[4] >= 0x00 && buf[4] <= 0x1C && buf[3] != 0x01)//写入独立log
{
logpot("[" + clientSocket[i]->peerAddress().toString()+ "] [" +command[buf[4]] + "] " + &buf[5]);
}
}
for(unsigned int i=0; i<size; i++) buf[i]=0;
}
}
void Widget::readSQLserver()
{
for(int i=0; i<clientSocket.length(); i++)
{
qint64 size = SQLsocket[clientSocket[i]]->read(buf,16777215);
if(size == 0) continue;
QString ipaddr = clientSocket[i]->peerAddress().toString();
if((buf[3] == 2 && buf[4] == -1))
{
failcount[ipaddr] +=1;
if(failcount[ipaddr] >= ui->sb_attempt->value())
{
blacklist[ipaddr] = time.currentTime();
}
log(ipaddr+"登录失败x"+QString::number(failcount[ipaddr]));
}
clientSocket[i]->write(buf,size);
for(unsigned int i=0; i<size; i++) buf[i]=0;
}
}
void Widget::clientdisconnected()
{
for(int i=0; i<clientSocket.length(); i++)
{
for(unsigned int i=0; i<16777215; i++) buf[i]=0;
if(clientSocket[i]->state() == QAbstractSocket::UnconnectedState)
{
//log("client:" + QString::number(int(clientSocket[i]) & 0x0000FFFF,16) + " disconnected.");
SQLsocket[clientSocket[i]]->close();
delete SQLsocket[clientSocket[i]];
SQLsocket.remove(clientSocket[i]);
clientSocket.removeOne(clientSocket[i]);
}
}
}
int Widget::log(QString log_string)//运行日志
{
log_string = time.currentTime().toString()+ " " + log_string;
if(!ui->cb_logpot->isChecked())
{
ui->loging->appendHtml("<font color=\"#00FF00\">" + log_string + "</font> ");
}
log_string+= "\n";
if(ui->cb_log->isChecked())
{
QFile file(QCoreApplication::applicationDirPath()+"/logs/" + date.currentDate().toString("yyyy-MM-dd") + ".log");
if(file.open(QIODevice::Append | QIODevice::Text))
{
file.write(log_string.toUtf8());
file.close();
}
}
log_normal += log_string;
return log_string.length();
}
int Widget::logpot(QString log_string)//蜜罐内日志
{
log_string = time.currentTime().toString()+ " " + log_string;
ui->loging->appendHtml("<font color=\"#"+ui->le_color->text()+"\">" + log_string + "</font> ");
log_string+= "\n";
if(ui->cb_log->isChecked())
{
QFile file(QCoreApplication::applicationDirPath()+"/logs/" + date.currentDate().toString("yyyy-MM-dd") + "_honeypot.log");
if(file.open(QIODevice::Append | QIODevice::Text))
{
file.write(log_string.toUtf8());
file.close();
}
}
log_pot += log_string;
return log_string.length();
}
QString Widget::direction(QString ipaddr)
{
if(blacklist.contains(ipaddr))
{
//黑名单内
if(ui->rb_reject->isChecked())
{
//最大尝试次数后拒绝服务
return "Reject";
} else {
//最大尝试次数后进入蜜罐
return "HoneyPot";
}
} else {
//黑名单外
if(ui->cb_relay->isChecked())
{
//启用转发
return "Relay";
} else {
//禁用转发, 进入蜜罐
return "HoneyPot";
}
}
}
//const definition
int Widget::initialcommand()
{
command.insert(0x00, "SLEEP");
command.insert(0x01, "QUIT");
command.insert(0x02, "INIT_DB");
command.insert(0x03, "QUERY");
command.insert(0x04, "FIELD_LIST");
command.insert(0x05, "CREATE_DB");
command.insert(0x06, "DROP_DB");
command.insert(0x07, "REFRESH");
command.insert(0x08, "SHUTDOWN");
command.insert(0x09, "STATISTICS");
command.insert(0x0A, "PROCESS_INFO");
command.insert(0x0B, "CONNECT");
command.insert(0x0C, "PROCESS_KILL");
command.insert(0x0D, "DEBUG");
command.insert(0x0E, "PING");
command.insert(0x0F, "TIME");
command.insert(0x10, "DELAYED_INSERT");
command.insert(0x11, "CHANGE_USER");
command.insert(0x12, "BINLOG_DUMP");
command.insert(0x13, "TABLE_DUMP");
command.insert(0x14, "CONNECT_OUT");
command.insert(0x15, "REGISTER_SLAVE");
command.insert(0x16, "STMT_PREPARE");
command.insert(0x17, "STMT_EXECUTE");
command.insert(0x18, "STMT_SEND_LONG_DATA");
command.insert(0x19, "STMT_CLOSE");
command.insert(0x1A, "STMT_RESET");
command.insert(0x1B, "SET_OPTION");
command.insert(0x1C, "STMT_FETCH");
return 0;
}