-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSNMPScan.cpp
373 lines (336 loc) · 10.3 KB
/
SNMPScan.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
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
// SNMPScan.cpp : implementation file
//
#include "stdafx.h"
#include "Scanner.h"
#include "SNMPScan.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAXNUMIP 10000
#define MAXNUMLIST 200
#include <snmp.h>
#include <mgmtapi.h>
#pragma comment(lib,"snmpapi")
#pragma comment(lib,"mgmtapi")
CSNMPScan *pDlgSNMPScan;
/////////////////////////////////////////////////////////////////////////////
// CSNMPScan dialog
CSNMPScan::CSNMPScan(CWnd* pParent /*=NULL*/)
: CDialog(CSNMPScan::IDD, pParent)
{
//{{AFX_DATA_INIT(CSNMPScan)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CSNMPScan::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSNMPScan)
DDX_Control(pDX, IDC_IPADDRESSTarget, m_ctlIP);
DDX_Control(pDX, IDC_TREEResult, m_ctlTreeResult);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSNMPScan, CDialog)
//{{AFX_MSG_MAP(CSNMPScan)
ON_BN_CLICKED(IDC_BUTTONStart, OnBUTTONStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSNMPScan message handlers
BOOL CSNMPScan::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
char buffer[MAX_PATH];
GetPrivateProfileString("SNMPScan","TargetIP","192.168.80.135",buffer,MAX_PATH,INIFILENAME);
m_ctlIP.SetWindowText(buffer);
UpdateData(false);
pDlgSNMPScan=this;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CSNMPScan::OnBUTTONStart()
{
// TODO: Add your control notification handler code here
UpdateData(true);
UCHAR a,b,c,d;
CString strIP,strTemp,strOid;
int iTemp;
m_ctlIP.GetAddress(a,b,c,d);
strIP.Format("%d.%d.%d.%d",a,b,c,d);
strTemp.Format("SNMP扫描[%s]",strIP);
HTREEITEM root=m_ctlTreeResult.InsertItem(strTemp,0,1);
strOid=".1.3.6.1.2.1.25.1.1.0";
iTemp=atoi(GetValue(strIP,strOid,0))/1000;
strTemp.Format("系统启动时间:%d时%02d分%02d秒",iTemp/3600,(iTemp%3600)/60,iTemp%60);
m_ctlTreeResult.InsertItem(strTemp,4,5,root);
strOid=".1.3.6.1.2.1.1.1.0";
strTemp.Format("系统信息:%s",GetValue(strIP,strOid,0));
m_ctlTreeResult.InsertItem(strTemp,4,5,root);
strOid=".1.3.6.1.4.1.77.1.4.1.0";
strTemp.Format("主机名称:%s",GetValue(strIP,strOid,0));
m_ctlTreeResult.InsertItem(strTemp,4,5,root);
HTREEITEM curr=m_ctlTreeResult.InsertItem("当前进程:",2,3,root);
strOid=".1.3.6.1.2.1.25.4.2.1.2";
BOOL bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("hrSWEunName")<0)||strTemp=="")
bContinue=false;
else
m_ctlTreeResult.InsertItem(strTemp,4,5,curr);
}
curr=m_ctlTreeResult.InsertItem("用户列表:",2,3,root);
strOid=".1.3.6.1.4.1.77.1.2.25.1.1";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("svUserName")<0)||strTemp=="")
bContinue=false;
else
m_ctlTreeResult.InsertItem(strTemp,4,5,curr);
}
curr=m_ctlTreeResult.InsertItem("安装软件:",2,3,root);
strOid=".1.3.6.1.2.1.25.6.3.1.2";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("hrSWInstalledName")<0)||strTemp=="")
bContinue=false;
else
m_ctlTreeResult.InsertItem(strTemp,4,5,curr);
}
curr=m_ctlTreeResult.InsertItem("网卡数:",2,3,root);
strOid=".1.3.6.1.2.1.2.2.1.2";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("ifDescr")<0)||strTemp=="")
bContinue=false;
else
m_ctlTreeResult.InsertItem(strTemp,4,5,curr);
}
CStringArray sa[4];
curr=m_ctlTreeResult.InsertItem("IP表:",2,3,root);
strOid=".1.3.6.1.2.1.4.20.1.1";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("ipAdEntAddr")<0)||strTemp=="")
bContinue=false;
else
sa[0].Add(strTemp);
}
strOid=".1.3.6.1.2.1.4.20.1.3";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("ipAdEntNetMask")<0)||strTemp=="")
bContinue=false;
else
sa[1].Add(strTemp);
}
for(int i=0;i<sa[0].GetSize();i++){
strTemp.Format("%s/%s",sa[0].GetAt(i),sa[1].GetAt(i));
m_ctlTreeResult.InsertItem(strTemp,4,5,curr);
}
sa[0].RemoveAll();
sa[1].RemoveAll();
sa[2].RemoveAll();
sa[3].RemoveAll();
curr=m_ctlTreeResult.InsertItem("TCP连接[本地IP/Port->远端IP/Port]",2,3,root);
strOid=".1.3.6.1.2.1.6.13.1.2";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("tcpConnLocalAddress")<0)||strTemp=="")
bContinue=false;
else
sa[0].Add(strTemp);
}
strOid=".1.3.6.1.2.1.6.13.1.3";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("tcpConnLocalPort")<0)||strTemp=="")
bContinue=false;
else
sa[1].Add(strTemp);
}
strOid=".1.3.6.1.2.1.6.13.1.4";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("tcpConnRemAddress")<0)||strTemp=="")
bContinue=false;
else
sa[2].Add(strTemp);
}
strOid=".1.3.6.1.2.1.6.13.1.5";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("tcpConnRemPort")<0)||strTemp=="")
bContinue=false;
else
sa[3].Add(strTemp);
}
for(i=0;i<sa[0].GetSize();i++){
strTemp.Format("%s/%s->%s/%s",sa[0].GetAt(i),sa[1].GetAt(i),sa[2].GetAt(i),sa[3].GetAt(i));
m_ctlTreeResult.InsertItem(strTemp,4,5,curr);
}
sa[0].RemoveAll();
sa[1].RemoveAll();
sa[2].RemoveAll();
sa[3].RemoveAll();
curr=m_ctlTreeResult.InsertItem("UDP连接[本地IP/Port]:",2,3,root);
strOid=".1.3.6.1.2.1.7.5.1.1";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("udpLocalAddress")<0)||strTemp=="")
bContinue=false;
else
sa[0].Add(strTemp);
}
strOid=".1.3.6.1.2.1.7.5.1.2";
bContinue=true;
while(bContinue){
strTemp=GetValue(strIP,strOid,1);
if((strOid.Find("udpLocalPort")<0)||strTemp=="")
bContinue=false;
else
sa[1].Add(strTemp);
}
for(i=0;i<sa[0].GetSize();i++){
strTemp.Format("%s/%s",sa[0].GetAt(i),sa[1].GetAt(i));
m_ctlTreeResult.InsertItem(strTemp,4,5,curr);
}
sa[0].RemoveAll();
sa[1].RemoveAll();
sa[2].RemoveAll();
sa[3].RemoveAll();
UpdateData(false);
}
CString CSNMPScan::GetValue(CString strIP, CString &strOid, UCHAR ucType)
{
CString strRet,strTemp;
RFC1157VarBindList variableBindings;
variableBindings.list=NULL;
variableBindings.len=1;
if((variableBindings.list=(RFC1157VarBind *)SNMP_realloc(variableBindings.list,sizeof(RFC1157VarBind)*1))==NULL)
return strRet;
AsnObjectIdentifier reqObject;
if(!SnmpMgrStrToOid(strOid.GetBuffer(0),&reqObject))
return strRet;
variableBindings.list[0].name=reqObject;
variableBindings.list[0].value.asnType=ASN_NULL;
LPSNMP_MGR_SESSION session;
int iTimeout=1000;
int iTryTimes=1;
if((session=SnmpMgrOpen(strIP.GetBuffer(0),"public",iTimeout,iTryTimes))==NULL){
SnmpUtilVarBindListFree(&variableBindings);
return strRet;
}
AsnInteger errorStatus;
AsnInteger errorIndex;
BYTE requestType;
if(ucType==0){
requestType=ASN_RFC1157_GETREQUEST;
}else{
requestType=ASN_RFC1157_GETNEXTREQUEST;
}
SNMPAPI bRet=SnmpMgrRequest(session,requestType,&variableBindings,&errorStatus,&errorIndex);
if(errorStatus!=SNMP_ERRORSTATUS_NOERROR){
SnmpUtilVarBindListFree(&variableBindings);
SnmpMgrClose(session);
return strRet;
}
char *string=NULL;
UINT i,j;
CString strValueValue;
for(i=0;i<(int)variableBindings.len;i++){
SnmpMgrOidToStr(&variableBindings.list[i].name,&string);
strOid.Format("%s",string);
if(string)
SNMP_free(string);
switch(variableBindings.list[i].value.asnType){
case ASN_INTEGER:
strValueValue.Format("%d",variableBindings.list[i].value.asnValue.number);
break;
case ASN_UNSIGNED32:
strValueValue.Format("%u",variableBindings.list[i].value.asnValue.unsigned32);
break;
case ASN_COUNTER64:
strValueValue.Format("%d",variableBindings.list[i].value.asnValue.counter64);
break;
case ASN_OCTETSTRING:
strValueValue.Format("%s",variableBindings.list[i].value.asnValue.string.stream);
strValueValue=strValueValue.Left(variableBindings.list[i].value.asnValue.string.length);
/*strValueValue.Format("%d",variableBindings.list[i].value.asnValue.string.stream);
strValueValue=strValueValue.Left(variableBindings.list[i].value.asnValue.string.length);
strValueValue="";
for(j=0;j<variableBindings.list[i].value.asnValue.string.length;j++){
strTemp.Format("%c",variableBindings.list[i].value.asnValue.string.stream[j+1]);
strValueValue+=strTemp;
}*/
//m_strValueValue.Format("%s",&variableBindings.list[i].value.asnValue.string.stream[0]);
//m_strValueType="ASN_OCTETSTRING";
break;
case ASN_BITS:
strValueValue="";
for(j=0;j<variableBindings.list[i].value.asnValue.bits.length;j++){
strTemp.Format("%c",variableBindings.list[i].value.asnValue.bits.stream[j]);
strValueValue+=strTemp;
}
break;
case ASN_OBJECTIDENTIFIER:
strValueValue="";
for(j=0;j<variableBindings.list[i].value.asnValue.object.idLength;j++){
strTemp.Format(".%d",variableBindings.list[i].value.asnValue.object.ids[j]);
strValueValue+=strTemp;
}
break;
case ASN_SEQUENCE:
strValueValue="";
for(j=0;j<variableBindings.list[i].value.asnValue.sequence.length;j++){
strTemp.Format("%c",variableBindings.list[i].value.asnValue.sequence.stream[j]);
strValueValue+=strTemp;
}
break;
case ASN_IPADDRESS:
strValueValue="";
for(j=0;j<variableBindings.list[i].value.asnValue.address.length;j++){
strTemp.Format("%d.",variableBindings.list[i].value.asnValue.address.stream[j]);
strValueValue+=strTemp;
}
strValueValue.TrimRight(".");
break;
case ASN_COUNTER32:
strValueValue.Format("%d",variableBindings.list[i].value.asnValue.counter);
break;
case ASN_GAUGE32:
strValueValue.Format("%d",variableBindings.list[i].value.asnValue.gauge);
break;
case ASN_TIMETICKS:
strValueValue.Format("%d",variableBindings.list[i].value.asnValue.ticks);
break;
case ASN_OPAQUE:
strValueValue="";
for(j=0;j<variableBindings.list[i].value.asnValue.address.length;j++){
strTemp.Format("%d",variableBindings.list[i].value.asnValue.arbitrary.stream[j]);
strValueValue+=strTemp;
}
break;
default:
strValueValue="Undefined.";
break;
}
}
strRet=strValueValue;
SnmpUtilVarBindListFree(&variableBindings);
SnmpMgrClose(session);
return strRet;
}