-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfireView.cpp
More file actions
473 lines (402 loc) · 10.1 KB
/
fireView.cpp
File metadata and controls
473 lines (402 loc) · 10.1 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// fireView.cpp : implementation of the CFireView class
//
#include "stdafx.h"
#include "fire.h"
#include "fireDoc.h"
#include "fireView.h"
#include "Sockutil.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFireView
IMPLEMENT_DYNCREATE(CFireView, CFormView)
BEGIN_MESSAGE_MAP(CFireView, CFormView)
ON_BN_CLICKED(IDC_ADDRULE, OnAddrule)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_BLOCKPING, OnBlockping)
ON_BN_CLICKED(IDC_BLOCKALL, OnBlockall)
ON_BN_CLICKED(IDC_ALLOWALL, OnAllowall)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_VIEWRULES, OnViewrules)
ON_WM_SHOWWINDOW()
ON_UPDATE_COMMAND_UI(ID_Start, OnUpdateStart)
ON_COMMAND(ID_STOP, OnStop)
ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop)
ON_UPDATE_COMMAND_UI(ID_ALLOWALL, OnUpdateAllowall)
ON_UPDATE_COMMAND_UI(ID_BLOCKALL, OnUpdateBlockall)
ON_COMMAND(ID_Start, OnStart)
ON_COMMAND(ID_BLOCKALL, OnBlockall)
ON_COMMAND(ID_ALLOWALL, OnAllowall)
ON_COMMAND(ID_BLOCKPING, OnBlockping)
ON_UPDATE_COMMAND_UI(ID_BLOCKPING, OnUpdateBlockping)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFireView construction/destruction
//初始化界面状态
CFireView::CFireView()
: CFormView(CFireView::IDD)
{
//********************************************************
m_pBrush = new CBrush;
ASSERT(m_pBrush);
m_clrBk = RGB(0x00,0x66,0x99);
m_clrText = RGB(0xff,0xff,0x00);
m_pBrush->CreateSolidBrush(m_clrBk);
m_pColumns = new CStringList;
ASSERT(m_pColumns);
_rows = 1;
start = TRUE;
block = TRUE;
allow = TRUE;
ping = TRUE ;
}
CFireView::~CFireView()
{
if (m_pBrush)
delete m_pBrush;
}
void CFireView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_RESULT, m_cResult);
DDX_Control(pDX, IDC_VIEWRULES, m_cvrules);
DDX_Control(pDX, IDC_BLOCKPING, m_cping);
DDX_Control(pDX, IDC_BLOCKALL, m_cblockall);
DDX_Control(pDX, IDC_START, m_cstart);
}
BOOL CFireView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//*****************************************************************
m_filterDriver.LoadDriver("IpFilterDriver", "System32\\Drivers\\IpFltDrv.sys", NULL, TRUE);
//we don't deregister the driver at destructor
m_filterDriver.SetRemovable(FALSE);
//we load the Filter-Hook Driver
m_ipFltDrv.LoadDriver("DrvFltIp", NULL, NULL, TRUE);
//****************************************************************
return CFormView::PreCreateWindow(cs);
}
void CFireView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
m_parent = (CMainFrame*)GetParent();
ShowHeaders();
}
/////////////////////////////////////////////////////////////////////////////
// CFireView diagnostics
#ifdef _DEBUG
void CFireView::AssertValid() const
{
CFormView::AssertValid();
}
void CFireView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFireView message handlers
void CFireView::OnAddrule()
{
// TODO: Add your control notification handler code here
m_Addrule.DoModal ();
}
void CFireView::OnStart()
{
CString _text;
m_cstart.GetWindowText(_text);
//Start响应事件
if(_text != "Stop" )
{
if(m_ipFltDrv.WriteIo(START_IP_HOOK, NULL, 0) != DRV_ERROR_IO)
{
MessageBox("Firewall Started Sucessfully");
start = FALSE;
m_cstart.SetWindowText("Stop");
m_parent ->SetOnlineLed(TRUE);
m_parent ->SetOfflineLed(FALSE);
}
}
//Stop响应事件
else
{
if(m_ipFltDrv.WriteIo(STOP_IP_HOOK, NULL, 0) != DRV_ERROR_IO)
{
MessageBox("Firewall Stopped Succesfully");
m_cstart.SetWindowText("Start");
start = TRUE;
m_parent ->SetOnlineLed(FALSE);
m_parent ->SetOfflineLed(TRUE);
}
}
}
//禁用所有的ICMP包
void CFireView::OnBlockping()
{
//MessageBox("此功能需要你来实现!");
// Your code
IPFilter filter; //定义一个过滤器
filter.protocol = 1;
filter.destinationIp =0;
filter.destinationMask=0;
filter.destinationPort=0;
filter.sourceIp =0;
filter.sourceMask=0;
filter.sourcePort=0;
filter.drop=TRUE;
m_Addrule.AddFilter(filter); //添加该过滤器
m_cping.EnableWindow(FALSE);
ping = FALSE;
allow = TRUE;
block = TRUE;
}
//禁用所有包
void CFireView::OnBlockall()
{
//MessageBox("此功能需要你来实现!");
// Your code
IPFilter filter; //定义一个过滤器
filter.protocol=0;
filter.destinationIp=0;
filter.destinationMask = 0;
filter.destinationPort = 0;
filter.sourceIp=0;
filter.sourceMask = 0;
filter.sourcePort=0;
filter.drop=TRUE;
m_Addrule.AddFilter(filter); //添加该过滤器
block=FALSE; //设置block,ping,allow的值
ping=FALSE;
allow=TRUE;
m_cblockall.EnableWindow(FALSE);
}
//启用所有包
void CFireView::OnAllowall()
{
if(m_ipFltDrv.WriteIo(CLEAR_FILTER,NULL,0)!=DRV_ERROR_IO){
m_cResult.DeleteAllItems(); //删除所有的条目
m_cping.EnableWindow();
m_cblockall.EnableWindow();
m_cvrules.EnableWindow();
allow=FALSE;
block = TRUE;
ping = TRUE;
_rows=1;
}
}
//使用指定过滤规则
BOOL CFireView::ImplementRule(void)
{
HANDLE _hFile;
DWORD error,nbytesRead;
char data;
CString _buff = "";
_hFile = CreateFile("saved.rul",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if(_hFile == INVALID_HANDLE_VALUE)
{
error = GetLastError();
MessageBox("Unable to open the file");
return FALSE;
}
else
{
BOOL bResult;
do{
bResult = ReadFile(_hFile,&data,1,&nbytesRead,NULL);
if((data != '\n'))
{
_buff = _buff + data;
}
else
if((bResult && nbytesRead) !=0)
{
_buff.Remove('\n');
ParseToIp(_buff);
_buff = "";
}
}while((bResult && nbytesRead) !=0);
CloseHandle(_hFile);
}
// Your code
return TRUE;
}
//将字符串解析为filter特定格式
void CFireView:: ParseToIp(CString str)
{
// Your code, please pay attention to the form of IP address and port!
CString _str[8];
int index=0;
for(int i = 0;i<7;i++){
index = str.Find(_T(','));
CString temp =str.Left(str.Find(_T(',')));
_str[i]=temp;
str.Delete(0,index+1);
}
_str[7]=str;
AddItem(0,0,(LPCTSTR)_str[0]);
AddItem(0,1,(LPCTSTR)_str[1]);
AddItem(0,2,(LPCTSTR)_str[2]);
AddItem(0,3,(LPCTSTR)_str[3]);
AddItem(0,4,(LPCTSTR)_str[4]);
AddItem(0,5,(LPCTSTR)_str[5]);
int _proto = atoi((LPCTSTR)_str[6]);
CString proto;
if(_proto == 0)
proto = "ANY";
if(_proto == 1)
proto = "ICMP";
if(_proto == 6)
proto = "TCP";
if(_proto == 17)
proto = "UDP";
AddItem(0,6,((LPCTSTR)proto));
int _drop = atoi((LPCTSTR)_str[7]);
if(_drop == 0)
AddItem(0,7,"ALLOW");
if(_drop == 1)
AddItem(0,7,"DENY");
_rows = _rows + 1;
IPFilter ip1;
ip1.destinationIp = inet_addr((LPCTSTR)_str[0]);
ip1.destinationMask = inet_addr((LPCTSTR)_str[1]);
ip1.sourceIp = inet_addr((LPCTSTR)_str[3]);
ip1.sourceMask = inet_addr((LPCTSTR)_str[4]);
ip1.sourcePort = htons(atoi((LPCTSTR)_str[5]));
ip1.protocol = atoi((LPCTSTR)_str[6]);
int drop;
drop = atoi((LPCTSTR)_str[7]);
if(drop == 0)
{
ip1.drop=FALSE;
}
if(drop == 1)
{
ip1.drop=TRUE;
}
m_Addrule.AddFilter(ip1);
}
//增加过滤规则表列
BOOL CFireView::AddColumn(LPCTSTR strItem,int nItem,int nSubItem,int nMask,int nFmt)
{
LV_COLUMN lvc;
lvc.mask = nMask;
lvc.fmt = nFmt;
lvc.pszText = (LPTSTR) strItem;
lvc.cx = m_cResult.GetStringWidth(lvc.pszText) + 25;
if(nMask & LVCF_SUBITEM)
{
if(nSubItem != -1)
lvc.iSubItem = nSubItem;
else
lvc.iSubItem = nItem;
}
return m_cResult.InsertColumn(nItem,&lvc);
}
//增加过滤规则表一个元素
BOOL CFireView::AddItem(int nItem,int nSubItem,LPCTSTR strItem ,int nImageIndex)
{
LV_ITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = nItem;
lvItem.iSubItem = nSubItem;
lvItem.pszText = (LPTSTR) strItem;
if(nImageIndex != -1)
{
lvItem.mask |= LVIF_IMAGE;
lvItem.iImage |= LVIF_IMAGE;
}
if(nSubItem == 0)
return m_cResult.InsertItem(&lvItem);
return m_cResult.SetItem(&lvItem);
}
void CFireView::AddHeader(LPTSTR hdr)
{
if (m_pColumns)
m_pColumns->AddTail(hdr);
}
void CFireView::ShowHeaders()
{
int nIndex = 0;
POSITION pos = m_pColumns->GetHeadPosition();
while (pos)
{
CString hdr = (CString)m_pColumns->GetNext(pos);
AddColumn(hdr,nIndex++);
}
}
void CFireView::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFormView::OnShowWindow(bShow, nStatus);
AddHeader(_T("Dest IP"));
AddHeader(_T("Dest MASK"));
AddHeader(_T("Dest PORT"));
AddHeader(_T("Source IP"));
AddHeader(_T("Source MASK"));
AddHeader(_T("Source PORT"));
AddHeader(_T("PROTOCOL"));
AddHeader(_T("ACTION"));
}
void CFireView::OnStop()
{
OnStart();
}
void CFireView::OnUpdateStart(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI ->Enable(start);
}
void CFireView::OnUpdateStop(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI ->Enable(!start);
}
void CFireView::OnUpdateAllowall(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI ->Enable(allow);
}
void CFireView::OnUpdateBlockall(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI ->Enable(block);
}
void CFireView::OnUpdateBlockping(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI ->Enable(ping);
}
BOOL CFireView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
//***********************************************************************
HBRUSH CFireView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
switch(nCtlColor)
{
case CTLCOLOR_BTN:
case CTLCOLOR_STATIC:
pDC->SetBkColor(m_clrBk);
pDC->SetTextColor(m_clrText);
case CTLCOLOR_DLG:
return static_cast<HBRUSH>(m_pBrush->GetSafeHandle());
}
return CFormView::OnCtlColor(pDC,pWnd,nCtlColor);
}
void CFireView::OnViewrules()
{
ImplementRule();
}