~ubuntu-branches/ubuntu/precise/ettercap/precise

« back to all changes in this revision

Viewing changes to src/ec_dissector_portmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Murat Demirten
  • Date: 2003-06-21 19:57:18 UTC
  • Revision ID: james.westby@ubuntu.com-20030621195718-qqbbfk18e1djchd9
Tags: upstream-0.6.b
ImportĀ upstreamĀ versionĀ 0.6.b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    ettercap -- dissector portmap
 
3
 
 
4
    Copyright (C) 2001  ALoR <alor@users.sourceforge.net>, NaGA <crwm@freemail.it>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 
 
20
    $Id: ec_dissector_portmap.c,v 1.3 2002/04/12 15:59:15 alor Exp $
 
21
*/
 
22
 
 
23
 
 
24
#include "include/ec_main.h"
 
25
 
 
26
#include "include/ec_dissector.h"
 
27
#include "include/ec_inet_structures.h"
 
28
 
 
29
//#define MAP_LEN 20
 
30
#define XID 0
 
31
#define PROG 1
 
32
#define PROTO 2
 
33
#define VER 3
 
34
 
 
35
#define XID_LEN 1024
 
36
#define DUMP 1
 
37
#define MAP_LEN 20
 
38
 
 
39
extern RPC_DISSECTOR Available_RPC_Dissectors[];
 
40
 
 
41
int Programs_tcp[XID_LEN][4];
 
42
int Programs_udp[XID_LEN][4];
 
43
 
 
44
// protos....
 
45
 
 
46
FUNC_DISSECTOR(Dissector_portmapTCP);
 
47
FUNC_DISSECTOR(Dissector_portmapUDP);
 
48
void RPC_PortInsert( RPC_DISSECTOR *Entry, short port);
 
49
 
 
50
// ------------------------------------------------------------
 
51
 
 
52
void RPC_PortInsert( RPC_DISSECTOR *Entry, short port)
 
53
{
 
54
   RPC_PORTS **to_enter;
 
55
 
 
56
   to_enter = &Entry->ports;
 
57
   // He ya...another funny list!!!!!
 
58
   if (Entry->ports == NULL)
 
59
      Entry->ports = (RPC_PORTS *)malloc (sizeof(RPC_PORTS));
 
60
   else
 
61
   {
 
62
      while(*to_enter != NULL)
 
63
      {
 
64
         if ((*to_enter)->port == port) return;
 
65
         to_enter = (RPC_PORTS **)&((*to_enter)->next);
 
66
      }
 
67
      *to_enter = (RPC_PORTS *)malloc (sizeof(RPC_PORTS));
 
68
   }
 
69
 
 
70
   (*to_enter)->port = port;
 
71
   (*to_enter)->next = NULL;
 
72
}
 
73
 
 
74
 
 
75
 
 
76
FUNC_DISSECTOR(Dissector_portmapTCP)
 
77
{
 
78
   TCP_header *tcp;
 
79
   u_char *buf;
 
80
   int type,xid,proc,proto,program,version,port,state,len,offs,i,j;
 
81
 
 
82
   tcp = (TCP_header *) data;
 
83
   buf = (char *)((int)tcp + tcp->doff * 4);
 
84
 
 
85
   xid  = *(int *)(buf+4);
 
86
   proc = *(int *)(buf+24);
 
87
   type = *(int *)(buf+8);
 
88
   if ((len=data_to_ettercap->datalen)==0) return (0);
 
89
 
 
90
   // CALL
 
91
   if (ntohs(tcp->dest) == SERV_PORT)
 
92
   {
 
93
      proto = *(int *)(buf+52);
 
94
      program = *(int *)(buf+44);
 
95
      version = *(int *)(buf+48);
 
96
 
 
97
      if (type!=0) return (0);
 
98
 
 
99
      for (i=0; i<XID_LEN; i++)
 
100
         if (!Programs_tcp[i][XID]) break;
 
101
 
 
102
      if (i==XID_LEN) return (0);
 
103
 
 
104
      if (ntohl(proc)==3) // GETPORT
 
105
      {
 
106
         Programs_tcp[i][XID] = xid;
 
107
         Programs_tcp[i][PROTO] = proto;
 
108
         Programs_tcp[i][PROG] = program;
 
109
         Programs_tcp[i][VER] = version;
 
110
      }
 
111
 
 
112
      if (ntohl(proc)==4) //DUMP
 
113
      {
 
114
         Programs_tcp[i][XID]=xid;
 
115
         Programs_tcp[i][PROG]=DUMP;
 
116
      }
 
117
 
 
118
      return (0);
 
119
   }
 
120
 
 
121
   // REPLY
 
122
   for (j=0; j<XID_LEN; j++)
 
123
      if (Programs_tcp[j][XID] == xid) break;
 
124
 
 
125
   if (j == XID_LEN) return (0);
 
126
 
 
127
   Programs_tcp[j][XID] = 0;
 
128
   state = *(int *)(buf+12);
 
129
 
 
130
   if (state != 0 || ntohl(type) != 1) // Unsuccess or not a reply :(
 
131
      return (0);
 
132
 
 
133
   if (Programs_tcp[j][PROG]!=DUMP)  // GETPORT Reply
 
134
   {
 
135
      port = *(int *)(buf+28);
 
136
      i = 0;
 
137
 
 
138
      while ( Available_RPC_Dissectors[i].program != 0 )
 
139
      {
 
140
         if ( Available_RPC_Dissectors[i].program == ntohl(Programs_tcp[j][PROG]) &&
 
141
              Available_RPC_Dissectors[i].version == ntohl(Programs_tcp[j][VER]) &&
 
142
              Available_RPC_Dissectors[i].proto  == (short)ntohl(Programs_tcp[j][PROTO]))
 
143
         {
 
144
            RPC_PortInsert( &Available_RPC_Dissectors[i], (short)(ntohl(port)) );
 
145
            break;
 
146
         }
 
147
         i++;
 
148
      }
 
149
   }
 
150
   else           // DUMP Reply
 
151
   {
 
152
      offs = 28;
 
153
      while ( (len-offs)>=MAP_LEN )
 
154
      {
 
155
         program = *(int *)(buf+offs+4);
 
156
         version = *(int *)(buf+offs+8);
 
157
         proto   = *(int *)(buf+offs+12);
 
158
         port    = *(int *)(buf+offs+16);
 
159
 
 
160
         i = 0;
 
161
         while ( Available_RPC_Dissectors[i].program != 0 )
 
162
         {
 
163
            if ( Available_RPC_Dissectors[i].program == ntohl(program) &&
 
164
                 Available_RPC_Dissectors[i].version == ntohl(version) &&
 
165
                 Available_RPC_Dissectors[i].proto  == (short)ntohl(proto))
 
166
            {
 
167
               RPC_PortInsert( &Available_RPC_Dissectors[i], (short)(ntohl(port)) );
 
168
               break;
 
169
            }
 
170
            i++;
 
171
         }
 
172
         offs += MAP_LEN;
 
173
       }
 
174
   }
 
175
   return(0);
 
176
}
 
177
 
 
178
 
 
179
 
 
180
FUNC_DISSECTOR(Dissector_portmapUDP)
 
181
{
 
182
   UDP_header *udp;
 
183
   u_char *buf;
 
184
   int type,xid,proc,proto,program,version,port,state,len,offs,i,j;
 
185
 
 
186
   udp = (UDP_header *) data;
 
187
   buf = data + UDP_HEADER;
 
188
 
 
189
   xid  = *(int *)buf;
 
190
   proc = *(int *)(buf+20);
 
191
   type = *(int *)(buf+4);
 
192
   len = data_to_ettercap->datalen;
 
193
 
 
194
   // CALL
 
195
   if (ntohs(udp->dest) == SERV_PORT)
 
196
   {
 
197
      proto = *(int *)(buf+48);
 
198
      program = *(int *)(buf+40);
 
199
      version = *(int *)(buf+44);
 
200
 
 
201
      if (type!=0) return (0);
 
202
 
 
203
      for (i=0; i<XID_LEN; i++)
 
204
         if (!Programs_udp[i][XID]) break;
 
205
 
 
206
      if (i==XID_LEN) return (0);
 
207
 
 
208
      if (ntohl(proc)==3) // GETPORT
 
209
      {
 
210
         Programs_udp[i][XID] = xid;
 
211
         Programs_udp[i][PROTO] = proto;
 
212
         Programs_udp[i][PROG] = program;
 
213
         Programs_udp[i][VER] = version;
 
214
      }
 
215
 
 
216
      if (ntohl(proc)==4) //DUMP
 
217
      {
 
218
         Programs_udp[i][XID]=xid;
 
219
         Programs_udp[i][PROG]=DUMP;
 
220
      }
 
221
      return (0);
 
222
   }
 
223
 
 
224
   // REPLY
 
225
   for (j=0; j<XID_LEN; j++)
 
226
      if (Programs_udp[j][XID]==xid) break;
 
227
 
 
228
   if (j==XID_LEN) return (0);
 
229
 
 
230
   Programs_udp[j][XID]=0;
 
231
   state = *(int *)(buf+8);
 
232
 
 
233
   if (state != 0 || ntohl(type) != 1) // Unsuccess or not a reply :(
 
234
      return (0);
 
235
 
 
236
   if (Programs_udp[j][PROG]!=DUMP)  // GETPORT Reply
 
237
   {
 
238
      port = *(int *)(buf+24);
 
239
      i = 0;
 
240
 
 
241
      while ( Available_RPC_Dissectors[i].program != 0 )
 
242
      {
 
243
         if ( Available_RPC_Dissectors[i].program == ntohl(Programs_udp[j][PROG]) &&
 
244
              Available_RPC_Dissectors[i].version == ntohl(Programs_udp[j][VER]) &&
 
245
              Available_RPC_Dissectors[i].proto  == (short)ntohl(Programs_udp[j][PROTO]))
 
246
         {
 
247
            RPC_PortInsert( &Available_RPC_Dissectors[i], (short)(ntohl(port)) );
 
248
            break;
 
249
         }
 
250
         i++;
 
251
      }
 
252
   }
 
253
   else           // DUMP Reply
 
254
   {
 
255
      offs = 24;
 
256
      while ( (len-offs)>=MAP_LEN )
 
257
      {
 
258
         program = *(int *)(buf+offs+4);
 
259
         version = *(int *)(buf+offs+8);
 
260
         proto   = *(int *)(buf+offs+12);
 
261
         port    = *(int *)(buf+offs+16);
 
262
 
 
263
         i = 0;
 
264
         while ( Available_RPC_Dissectors[i].program != 0 )
 
265
         {
 
266
            if ( Available_RPC_Dissectors[i].program == ntohl(program) &&
 
267
                 Available_RPC_Dissectors[i].version == ntohl(version) &&
 
268
                 Available_RPC_Dissectors[i].proto  == (short)ntohl(proto))
 
269
            {
 
270
               RPC_PortInsert( &Available_RPC_Dissectors[i], (short)(ntohl(port)) );
 
271
               break;
 
272
            }
 
273
            i++;
 
274
         }
 
275
         offs += MAP_LEN;
 
276
      }
 
277
   }
 
278
   return(0);
 
279
}
 
280
 
 
281
/* EOF */
 
282
 
 
283
// vim:ts=3:expandtab
 
284