Skip to Content
[CAIDA - Center for Applied Internet Data Analysis logo]
The Center for Applied Internet Data Analysis
corsaro_anon.c
Go to the documentation of this file.
1 /*
2  * corsaro
3  *
4  * Alistair King, CAIDA, UC San Diego
5  * corsaro-info@caida.org
6  *
7  * Copyright (C) 2012 The Regents of the University of California.
8  *
9  * This file is part of corsaro.
10  *
11  * corsaro is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * corsaro is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with corsaro. If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #include "config.h"
27 #include "corsaro_int.h"
28 
29 #include <assert.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "libtrace.h"
37 
38 #include "utils.h"
39 
40 #include "corsaro_libanon.h"
41 #include "corsaro_log.h"
42 #include "corsaro_plugin.h"
43 
44 #ifdef WITH_PLUGIN_SIXT
45 #include "corsaro_flowtuple.h"
46 #endif
47 
48 #include "corsaro_anon.h"
49 
59 #define CORSARO_ANON_MAGIC 0x414E4F4E
60 
62 #define PLUGIN_NAME "anon"
63 
65 #define ANON_ENC_TYPE CORSARO_ANON_ENC_CRYPTOPAN
66 
68 #define ENC_TYPE_CRYPTOPAN "cryptopan"
69 
71 #define ENC_TYPE_PREFIX "prefix"
72 
74 #define ANON_SOURCE 0
75 
77 #define ANON_DEST 0
78 
81  PLUGIN_NAME, /* name */
82  CORSARO_PLUGIN_ID_ANON, /* id */
83  CORSARO_ANON_MAGIC, /* magic */
84 #ifdef WITH_PLUGIN_SIXT
85  CORSARO_PLUGIN_GENERATE_PTRS_FT(corsaro_anon), /* func ptrs */
86 #else
87  CORSARO_PLUGIN_GENERATE_PTRS(corsaro_anon),
88 #endif
90 };
91 
93 struct corsaro_anon_state_t {
95  corsaro_anon_enc_type_t encryption_type;
97  char *encryption_key;
99  int encrypt_source;
101  int encrypt_destination;
102 };
103 
105 #define STATE(corsaro) \
106  (CORSARO_PLUGIN_STATE(corsaro, anon, CORSARO_PLUGIN_ID_ANON))
107 
109 #define PLUGIN(corsaro) \
110  (CORSARO_PLUGIN_PLUGIN(corsaro, CORSARO_PLUGIN_ID_ANON))
111 
113 static void usage(corsaro_plugin_t *plugin)
114 {
115  fprintf(stderr,
116  "plugin usage: %s [-sd] [-t encryption_type] encryption_key[prefix]\n"
117  " -d enable destination address encryption\n"
118  " -s enable source address encryption\n"
119  " -t encryption type (default: %s)\n"
120  " must be either '%s', or '%s'\n",
121  plugin->argv[0],
125 }
126 
129 {
130  corsaro_plugin_t *plugin = PLUGIN(corsaro);
131  struct corsaro_anon_state_t *state = STATE(corsaro);
132  int opt;
133 
134  /* NB: remember to reset optind to 1 before using getopt! */
135  optind = 1;
136 
137  while((opt = getopt(plugin->argc, plugin->argv, ":dst:?")) >= 0)
138  {
139  switch(opt)
140  {
141  case 'd':
142  state->encrypt_destination = 1;
143  break;
144 
145  case 's':
146  state->encrypt_source = 1;
147  break;
148 
149  case 't':
150  if(strcasecmp(optarg, ENC_TYPE_CRYPTOPAN) == 0)
151  {
152  state->encryption_type = CORSARO_ANON_ENC_CRYPTOPAN;
153  }
154  else if(strcasecmp(optarg, ENC_TYPE_PREFIX) == 0)
155  {
156  state->encryption_type = CORSARO_ANON_ENC_PREFIX_SUBSTITUTION;
157  }
158  else
159  {
160  fprintf(stderr, "ERROR: invalid encryption type (%s)\n",
161  optarg);
162  usage(plugin);
163  return -1;
164  }
165  break;
166 
167  case '?':
168  case ':':
169  default:
170  usage(plugin);
171  return -1;
172  }
173  }
174 
175  /* the last (and only required argument) must be the key */
176  if(optind != (plugin->argc - 1))
177  {
178  fprintf(stderr, "ERROR: missing encryption key\n");
179  usage(plugin);
180  return -1;
181  }
182 
183  state->encryption_key = plugin->argv[optind];
184 
185  if(state->encrypt_source == 0 && state->encrypt_destination == 0)
186  {
187  fprintf(stderr,
188  "WARNING: anon plugin is encrypting nothing\n");
189  }
190 
191  return 0;
192 }
193 
194 /* == PUBLIC PLUGIN FUNCS BELOW HERE == */
195 
198 {
199  return &corsaro_anon_plugin;
200 }
201 
203 int corsaro_anon_probe_filename(const char *fname)
204 {
205  /* this writes no files! */
206  return 0;
207 }
208 
211 {
212  /* this writes no files! */
213  return 0;
214 }
215 
218 {
219  struct corsaro_anon_state_t *state;
220  corsaro_plugin_t *plugin = PLUGIN(corsaro);
221 
222  assert(plugin != NULL);
223 
224  if((state = malloc_zero(sizeof(struct corsaro_anon_state_t))) == NULL)
225  {
226  corsaro_log(__func__, corsaro,
227  "could not malloc corsaro_anon_state_t");
228  goto err;
229  }
230  corsaro_plugin_register_state(corsaro->plugin_manager, plugin, state);
231 
232  /* set the defaults */
233  state->encryption_type = CORSARO_ANON_ENC_CRYPTOPAN;
234  state->encrypt_source = ANON_SOURCE;
235  state->encrypt_destination = ANON_DEST;
236 
237  /* parse the arguments */
238  if(parse_args(corsaro) != 0)
239  {
240  return -1;
241  }
242 
243  assert(state->encryption_key != NULL);
244 
245  corsaro_anon_init(state->encryption_type, state->encryption_key);
246  return 0;
247 
248  err:
249  corsaro_anon_close_output(corsaro);
250  return -1;
251 }
252 
255 {
256  assert(0);
257  return -1;
258 }
259 
262 {
263  assert(0);
264  return -1;
265 }
266 
269 {
270  return 0;
271 }
272 
275  corsaro_in_record_type_t *record_type,
277 {
278  assert(0);
279  return -1;
280 }
281 
284  enum corsaro_in_record_type *record_type,
285  struct corsaro_in_record *record)
286 {
287  /* we write nothing to the global file. someone messed up */
288  return -1;
289 }
290 
293  corsaro_interval_t *int_start)
294 {
295  /* we don't care */
296  return 0;
297 }
298 
301  corsaro_interval_t *int_end)
302 {
303  /* we don't care */
304  return 0;
305 }
306 
310 {
311  struct corsaro_anon_state_t *state = STATE(corsaro);
312  libtrace_ip_t *iphdr = trace_get_ip(LT_PKT(packet));
313 
314  if(iphdr != NULL && (state->encrypt_source || state->encrypt_destination))
315  {
316  corsaro_anon_ip_header(iphdr, state->encrypt_source,
317  state->encrypt_destination);
318  }
319 
320  return 0;
321 }
322 
323 #ifdef WITH_PLUGIN_SIXT
324 
325 int corsaro_anon_process_flowtuple(corsaro_t *corsaro,
326  corsaro_flowtuple_t *flowtuple,
327  corsaro_packet_state_t *state)
328 {
329  uint32_t src_ip = corsaro_flowtuple_get_source_ip(flowtuple);
330  uint32_t dst_ip = corsaro_flowtuple_get_destination_ip(flowtuple);
331 
332  uint32_t src_ip_anon = corsaro_anon_ip(ntohl(src_ip));
333  uint32_t dst_ip_anon = corsaro_anon_ip(ntohl(dst_ip));
334 
335  flowtuple->src_ip = htonl(src_ip_anon);
336  CORSARO_FLOWTUPLE_IP_TO_SIXT(htonl(dst_ip_anon), flowtuple);
337 
338  return 0;
339 }
340 
342 int corsaro_anon_process_flowtuple_class_start(corsaro_t *corsaro,
343  corsaro_flowtuple_class_start_t *class)
344 {
345  /* we dont care about these */
346  return 0;
347 }
348 
350 int corsaro_anon_process_flowtuple_class_end(corsaro_t *corsaro,
351  corsaro_flowtuple_class_end_t *class)
352 {
353  /* dont care */
354  return 0;
355 }
356 #endif
Structure representing the start or end of an interval.
Definition: corsaro_int.h:156
An opaque structure defining an corsaro input file.
Definition: corsaro_file.h:86
Header file dealing with the corsaro plugin manager.
Header file dealing with the corsaro logging sub-system.
int corsaro_anon_close_input(corsaro_in_t *corsaro)
Implements the close_input function of the plugin API.
Definition: corsaro_anon.c:261
#define CORSARO_FLOWTUPLE_IP_TO_SIXT(n32, flowtuple)
Convert a 32bit network order IP address into the 3 byte flowtuple format.
int corsaro_anon_init_output(corsaro_t *corsaro)
Implements the init_output function of the plugin API.
Definition: corsaro_anon.c:217
static void usage(corsaro_plugin_t *plugin)
Print usage information to stderr.
Definition: corsaro_anon.c:113
off_t corsaro_anon_read_record(struct corsaro_in *corsaro, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Implements the read_record function of the plugin API.
Definition: corsaro_anon.c:274
A reusable opaque structure for corsaro to read an input record into.
Definition: corsaro_int.h:350
char ** argv
Array of plugin arguments This is populated by the plugin manager in corsaro_plugin_enable_plugin.
uint32_t corsaro_flowtuple_get_destination_ip(corsaro_flowtuple_t *flowtuple)
Convenience function to get the destination IP address from a FlowTuple.
#define LT_PKT(corsaro_packet)
Convenience macro to get to the libtrace packet inside an corsaro packet.
Definition: corsaro_int.h:227
static int parse_args(corsaro_t *corsaro)
Parse the arguments given to the plugin.
Definition: corsaro_anon.c:128
#define STATE(corsaro)
Extends the generic plugin state convenience macro in corsaro_plugin.h.
Definition: corsaro_anon.c:105
#define ANON_DEST
Anonymize the Destination IP by default?
Definition: corsaro_anon.c:77
int corsaro_anon_end_interval(corsaro_t *corsaro, corsaro_interval_t *int_end)
Implements the end_interval function of the plugin API.
Definition: corsaro_anon.c:300
Header file which exports corsaro_flowtuple plugin API.
static libtrace_packet_t * packet
A pointer to a libtrace packet.
Definition: corsaro_main.c:67
Header file for common utility functions.
A lightweight wrapper around a libtrace packet.
Definition: corsaro_int.h:211
corsaro_plugin_manager_t * plugin_manager
A pointer to the corsaro plugin manager state.
Definition: corsaro_int.h:273
#define CORSARO_PLUGIN_GENERATE_PTRS(plugin)
Convenience macro that defines all the function pointers for the corsaro plugin API.
corsaro_in_record_type
Corsaro input record types.
Definition: corsaro.h:97
void corsaro_plugin_register_state(corsaro_plugin_manager_t *manager, corsaro_plugin_t *plugin, void *state)
Register the state for a plugin.
uint32_t corsaro_flowtuple_get_source_ip(corsaro_flowtuple_t *flowtuple)
Convenience function to get the source IP address from a FlowTuple.
#define CORSARO_PLUGIN_GENERATE_TAIL
Convenience macro that defines all the 'remaining' blank fields in a corsaro plugin object...
void * malloc_zero(const size_t size)
Allocate memory and set it to zero.
Definition: utils.c:78
int corsaro_anon_init_input(corsaro_in_t *corsaro)
Implements the init_input function of the plugin API.
Definition: corsaro_anon.c:254
static corsaro_in_record_t * record
A pointer to a corsaro record.
Definition: corsaro_main.c:76
int corsaro_anon_close_output(corsaro_t *corsaro)
Implements the close_output function of the plugin API.
Definition: corsaro_anon.c:268
int argc
Count of arguments in argv.
int corsaro_anon_start_interval(corsaro_t *corsaro, corsaro_interval_t *int_start)
Implements the start_interval function of the plugin API.
Definition: corsaro_anon.c:292
static corsaro_plugin_t corsaro_anon_plugin
Common plugin information across all instances.
Definition: corsaro_anon.c:80
off_t corsaro_anon_read_global_data_record(struct corsaro_in *corsaro, enum corsaro_in_record_type *record_type, struct corsaro_in_record *record)
Implements the read_global_data_record function of the plugin API.
Definition: corsaro_anon.c:283
IP address anonymization plugin.
#define ENC_TYPE_CRYPTOPAN
The configuration string for the CORSARO_ANON_ENC_CRYPTOPAN type.
Definition: corsaro_anon.c:68
#define CORSARO_ANON_MAGIC
The magic number for this plugin - "ANON".
Definition: corsaro_anon.c:59
int corsaro_anon_process_packet(corsaro_t *corsaro, corsaro_packet_t *packet)
Implements the process_packet function of the plugin API.
Definition: corsaro_anon.c:308
Corsaro input state.
Definition: corsaro_int.h:323
corsaro_plugin_t * corsaro_anon_alloc(corsaro_t *corsaro)
Implements the alloc function of the plugin API.
Definition: corsaro_anon.c:197
#define PLUGIN(corsaro)
Extends the generic plugin plugin convenience macro in corsaro_plugin.h.
Definition: corsaro_anon.c:109
int corsaro_anon_probe_magic(corsaro_in_t *corsaro, corsaro_file_in_t *file)
Implements the probe_magic function of the plugin API.
Definition: corsaro_anon.c:210
Corsaro state for a packet.
Definition: corsaro_int.h:194
#define PLUGIN_NAME
The name of this plugin.
Definition: corsaro_anon.c:62
int corsaro_anon_probe_filename(const char *fname)
Implements the probe_filename function of the plugin API.
Definition: corsaro_anon.c:203
Corsaro output state.
Definition: corsaro_int.h:230
void corsaro_log(const char *func, corsaro_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro object.
Definition: corsaro_log.c:113
#define ANON_SOURCE
Anonymize the Source IP by default?
Definition: corsaro_anon.c:74
enum corsaro_in_record_type corsaro_in_record_type_t
Corsaro input record types.
An corsaro packet processing plugin.
#define ENC_TYPE_PREFIX
The configuration string for the CORSARO_ANON_ENC_PREFIX type.
Definition: corsaro_anon.c:71
Header file dealing with the internal corsaro functions.