File: dcetest.nasl

package info (click to toggle)
nessus-plugins 1.0.10-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,924 kB
  • ctags: 408
  • sloc: sh: 7,838; ansic: 3,415; makefile: 233
file content (365 lines) | stat: -rw-r--r-- 8,000 bytes parent folder | download
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
#
# DCEMAP
#
# Does a 'portmap-like' request to the remote host, to
# to determine what DCE/RPC services are running.
#
# This code is 100% based on 'dcetest', by Dave Aitel, available
# at http://www.atstake.com.
#
# dcetest is available as a free (GPL'ed) software written
# in C.
#
# License : GPLv2
#
#


#
#TODO : extract the names of PIPEs and hosts when present
# 




if(description)
{
 script_id(10736);
 
 name["english"] = "DCE Services Enumeration";
 script_name(english:name["english"]);
 
 desc["english"] = "
DCE services running on the remote can be enumerated
by connecting on port 135 and doing the appropriate
queries.

An attacker may use this fact to gain more knowledge
about the remote host.

Solution : filter incoming traffic to this port.
Risk factor : Low";



 script_description(english:desc["english"]);
 
 summary["english"] = "Enumerates the remote DCE services";
 script_summary(english:summary["english"]);
 
 script_category(ACT_GATHER_INFO);
 
 script_copyright(english:"This script is Copyright (C) 2001 Dave Aitel (ported to NASL by rd)");
 family["english"] = "Windows";
 script_family(english:family["english"]);
 script_dependencie("find_service.nes");
 script_require_ports(135);
 exit(0);
}




#------------------------------------------#
# String from a buffer. Inverts the bytes  #
#------------------------------------------#
function istring_from_buffer(b)
{
 ret = "";
 l   = strlen(b);
 for(i=0;i<l;i=i+1)
 {
  hx = hex(ord(b[i]));
  hx = hx - string("0x");
  ret = string(hx, ret);
 }
 return(ret);
}

#----------------------------------#
# String from a buffer. Straight.  #
#----------------------------------#
function string_from_buffer(b)
{
 ret = "";
 l   = strlen(b);
 for(i=0;i<l;i=i+1)
 {
  hx = hex(ord(b[i]));
  hx = hx - string("0x");
  ret = string(ret, hx);
 }
 return(ret);
}


#------------------------------------------#
# Return the guid as something printable   #
#------------------------------------------#
function strguid(guid)
{
 tbuffer = "";
 tbuffer2 = "";
 tbuffer3 = "";
 tbuffer4 = "";
 tbuffer5 = "";
 
 for(i=0;i<4;i=i+1)tbuffer = string(tbuffer, raw_string(ord(guid[i])));
 for(i=0;i<2;i=i+1)tbuffer2 = string(tbuffer2, raw_string(ord(guid[i+4])));
 for(i=0;i<2;i=i+1)tbuffer3 = string(tbuffer3, raw_string(ord(guid[i+6])));
 for(i=0;i<2;i=i+1)tbuffer4 = string(tbuffer4, raw_string(ord(guid[i+8])));
 for(i=0;i<6;i=i+1)tbuffer5 = string(tbuffer5, raw_string(ord(guid[i+10])));
 _ret = string(istring_from_buffer(b:tbuffer), "-",
 	       istring_from_buffer(b:tbuffer2), "-",
	       istring_from_buffer(b:tbuffer3), "-",
	       string_from_buffer(b:tbuffer4), "-",
	       string_from_buffer(b:tbuffer5));
	       
 return(_ret); 
 
}

#--------------------------------------------------------------------------#


function dce_bind()
{ 
 uuid = raw_string(0x08, 0x83, 0xAF, 0xE1,
		   0x1F, 0x5D, 0xC9, 0x11,
		   0x91, 0xA4, 0x08, 0x00,
		   0x2B, 0x14 ,0xA0, 0xFA);
		   

 transfer_syntax = raw_string(
 		   0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11,
		   0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60
		   );  
		   
 return(raw_string(0x05, 0x00, 0x0B, 0x03,
 		   0x10, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
		   0x01, 0x00, 0x00, 0x00, 0xD0, 0x16, 0xD0, 0x16,
		   0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
		   0x00, 0x00, 0x01, 0x00) 
		   + uuid
		   + raw_string(0x03, 0x00, 0x00, 0x00) 
		   + transfer_syntax +
		   raw_string(0x02, 0x00, 0x00, 0x00));
}



function dce_enum_get(callid, handle)
{
 _c = callid % 255;
 _ret  = raw_string(0x05, 0x00, 0x00, 0x03, 
 		    0x10, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00,
		    _c,   0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
		    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x00, 0x00) + handle + raw_string(0x01, 0x00, 0x00, 0x00);

 return(_ret);

}


function load_long(b, t)
{
 __ret_lo_lo = ord(b[t]);
 __ret_hi_lo = ord(b[t+1])*256;
 __ret_lo_hi = ord(b[t+2])*65536;
 __ret_hi_hi = ord(b[t+3])* 16777216;
 
 __ret    = __ret_hi_hi + __ret_lo_hi + __ret_hi_lo + __ret_lo_lo;
 return(__ret);
}


function load_short_ntohs(b, t)
{
 __ret_lo = ord(b[t]);
 __ret_hi = ord(b[t+1])*256;
 __ret    = __ret_hi + __ret_lo;
 return(__ret);
}

function load_short(b, t)
{
 __ret_lo = ord(b[t+1]);
 __ret_hi = ord(b[t])*256;
 __ret    = __ret_hi + __ret_lo;
 return(__ret);
}


function dce_parse(result)
{
 #
 # Update the handle
 # 
 handle = "";
 for(i=0;i<16;i=i+1)
 {
  handle = string(handle,raw_string(ord(result[28+i])));
 }
 
 
 p = 84; # 28 + 16 + 40;
 tint = load_long(b:result, t:p);
 annotation = "";
 
 p = p + 4;
 #display("tint_ann : ", tint, "\n");
 for(i=0;i<tint;i=i+1)
 {
   annotation = string(annotation, r[p+i]);
 }
 #display("=======> ", annotation, "\n");
 
 p = p + tint;
 

 while((tint % 4))
 {
  p = p + 1;
  tint = tint + 1;
 }
# p = p + 1;
# for(i=0;i<4;i=i+1)display(hex(ord(r[p+i])), " ");
# display("\n");
 realtype = load_long(b:r, t:p);
 p = p + 8;
 typefield = load_short(b:r, t:p);

 p = p + 2;
 sizefield = load_short(b:r, t:p);
 p = p + 2;
 
 guid = "";
 for(i = 0 ; i < 16 ; i = i + 1)
 {
  guid = string(guid,raw_string(r[p+i]));
 }
 
 
 p = p + 57; 			#   16+2+4+2+1+16+2+4+2+1+4+2+2;
 
 IPTYPE   = 0x4B;
 NCACN_NP  = 0x58;
 NCALRPC  = 0x47;
 NCALRPC2 = 0x56;
 NCALRPC3 = 0x4E;
 UNKNOWN1 = 0x5A;
 
 if(realtype == IPTYPE)
 {
  mytype = "     Type: ncacn_ip_";
  
  udp_or_tcp = ord(r[p]);
  if(udp_or_tcp == 0x08)proto = "udp";
  else {
   if(udp_or_tcp == 0x07)proto = "tcp";
   else proto = "unknown!";
   }
  mytype = string(mytype, proto, "\n");
  
  p = p + 3 ;   #1 + 2;
  port = load_short(b:r, t:p);
  p = p + 7;   #2 + 2 + 3;
  ip = "";
  for(i=0;i<3;i=i+1)
  {
   ip = ip + ord(r[p+i]) + ".";
  }
  ip = ip + ord(r[p+3]);
  report = string("A DCE service is listening on ", ip, ":", port, " : \n\n",
  			mytype,
			"     UUID : ", strguid(guid:guid), "\n");
  if(annotation)report = string(report, "     Annotation : ", annotation, "\n");
  myip = this_host();
  
  if(proto == "unknown!")proto = "tcp";
  
   security_note(port:port, data:report, proto:proto);
 }
 
 if((realtype == NCALRPC3) ||
    (realtype == NCALRPC2) ||
    (realtype == NCALRPC))
 {
  p = p + 1;
  strlength = load_short_ntohs(b:r, t:p);
 
  
  p = p + 2;
  servicename = "";
  for(i=0;i<strlength;i=i+1)
  {
   servicename = string(servicename, r[i+p]);
  }
  report = string("The DCE Service '", servicename, "' is running on this host\n",
		"     Type : ncalrpc\n",
  		 "     UUID : ", strguid(guid:guid), "\n");
  if(annotation)report = string(report, "     Annotation : ", annotation, "\n");		 
  security_note(port:135, data:report);
 }
 
 if(realtype == NCACN_NP)
 {
  p = p  + 1;
  sizeshort = load_short(b:r, t:p);
  p = p  + 2;
  nacn_endpoint = "";
  for(i=0;i<sizeshort;i=i+1)
  {
   nacn_endpoint = string(nacn_endpoint, r[p]);
   p = p + 1;
  }
 # display(nacn_endpoint, "\n");
 }
 #display(hex(realtype), "\n");
}


#-----------------------------------------------------------------------------------------#


handle = raw_string(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
	
if(get_port_state(135))
{	
 soc = open_sock_tcp(135);
 if(!soc)exit(0);
 
 enum = 0;

 send(socket:soc, data:dce_bind());
 r = recv(socket:soc, length:4096);
 if(strlen(r) < 60)exit(0); # bad reply length

 for(x=0;x<4096;x=x+1)
 {
 send(socket:soc, data:dce_enum_get(callid:x, handle:handle));
 #sleep(1);
 r = recv(socket:soc, length:4096);
 #display(".\n");

 if(strlen(r) <= 65)
 {
  # finished
  x = 4096;
 }
 else {
 	dce_parse(result:r);
	enum = enum + 1;
	}
 }
 close(soc);
 if(enum)security_warning(135);
}