00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifdef HAVE_CONFIG_H
00035 #include "config.h"
00036 #endif
00037
00038 #include <sys/types.h>
00039 #include <stdlib.h>
00040 #include <ctype.h>
00041 #ifdef HAVE_STRINGS_H
00042 #include <strings.h>
00043 #endif
00044 #include <errno.h>
00045
00046 #include "bounds.h"
00047 #include "rules.h"
00048 #include "decode.h"
00049 #include "plugbase.h"
00050 #include "parser.h"
00051 #include "debug.h"
00052 #include "util.h"
00053 #include "plugin_enum.h"
00054 #include "mstring.h"
00055
00056 extern u_int8_t *doe_ptr;
00057 extern u_int8_t DecodeBuffer[DECODE_BLEN];
00058
00059 void FTPBounceInit(char *, OptTreeNode *, int);
00060 void FTPBounceParse(char *, OptTreeNode *);
00061 int FTPBounce(Packet *, struct _OptTreeNode *, OptFpList *);
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 void SetupFTPBounce(void)
00075 {
00076
00077 RegisterPlugin("ftpbounce", FTPBounceInit);
00078
00079 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Plugin: FTPBounce Setup\n"););
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 void FTPBounceInit(char *data, OptTreeNode *otn, int protocol)
00099 {
00100 OptFpList *fpl;
00101
00102
00103
00104 FTPBounceParse(data, otn);
00105
00106 fpl = AddOptFuncToList(FTPBounce, otn);
00107
00108
00109
00110
00111 fpl->context = (void *) NULL;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 void FTPBounceParse(char *data, OptTreeNode *otn)
00130 {
00131 char **toks;
00132 int num_toks;
00133
00134 toks = mSplit(data, ",", 12, &num_toks, 0);
00135
00136 if(num_toks > 0)
00137 FatalError("ERROR %s (%d): Bad arguments to ftpbounce: %s\n", file_name,
00138 file_line, data);
00139
00140 mSplitFree(&toks, num_toks);
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 int FTPBounce(Packet *p, struct _OptTreeNode *otn, OptFpList *fp_list)
00160 {
00161 u_int32_t ip = 0;
00162 int octet=0;
00163 char *this_param = doe_ptr;
00164
00165 int dsize;
00166 int use_alt_buffer = p->packet_flags & PKT_ALT_DECODE;
00167 char *base_ptr, *end_ptr, *start_ptr;
00168
00169 if (!doe_ptr)
00170 {
00171 DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,
00172 "[*] ftpbounce no doe_ptr set..\n"););
00173 return 0;
00174 }
00175
00176 if(use_alt_buffer)
00177 {
00178 dsize = p->alt_dsize;
00179 start_ptr = (char *) DecodeBuffer;
00180 DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,
00181 "Using Alternative Decode buffer!\n"););
00182
00183 }
00184 else
00185 {
00186 start_ptr = p->data;
00187 dsize = p->dsize;
00188 }
00189
00190 DEBUG_WRAP(
00191 DebugMessage(DEBUG_PATTERN_MATCH,"[*] ftpbounce firing...\n");
00192 DebugMessage(DEBUG_PATTERN_MATCH,"payload starts at %p\n", start_ptr);
00193 );
00194
00195
00196 end_ptr = start_ptr + dsize;
00197 base_ptr = start_ptr;
00198
00199 if(doe_ptr)
00200 {
00201
00202 if(!inBounds(start_ptr, end_ptr, doe_ptr))
00203 {
00204 DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,
00205 "[*] ftpbounce bounds check failed..\n"););
00206 return 0;
00207 }
00208 }
00209
00210 while (isspace((int)*this_param) && (this_param < end_ptr)) this_param++;
00211
00212 do
00213 {
00214 int value = 0;
00215 do
00216 {
00217 if (!isdigit((int)*this_param))
00218 {
00219 DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,
00220 "[*] ftpbounce non digit char failed..\n"););
00221 return 0;
00222 }
00223 value = value * 10 + (*this_param - '0');
00224 this_param++;
00225 } while ((this_param < end_ptr) &&
00226 (*this_param != ',') &&
00227 (!(isspace((int)*this_param))));
00228 if (value > 0xFF)
00229 {
00230 DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,
00231 "[*] ftpbounce value > 256 ..\n"););
00232 return 0;
00233 }
00234 if (octet < 4)
00235 {
00236 ip = (ip << 8) + value;
00237 }
00238
00239 if (!isspace((int)*this_param))
00240 this_param++;
00241 octet++;
00242 } while ((this_param < end_ptr) && !isspace((int)*this_param) && (octet < 4));
00243
00244 if (octet < 4)
00245 {
00246 DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,
00247 "[*] ftpbounce insufficient data ..\n"););
00248 return 0;
00249 }
00250
00251 if (ip != ntohl(p->iph->ip_src.s_addr))
00252 {
00253 return fp_list->next->OptTestFunc(p, otn, fp_list->next);
00254 }
00255 else
00256 {
00257 DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,
00258 "PORT command not being used in bounce\n"););
00259 return 0;
00260 }
00261
00262
00263 return 0;
00264 }