-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscripting.c
executable file
·1831 lines (1313 loc) · 60.3 KB
/
scripting.c
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This should contain glue functions for being able to use scripting. I'll use code I have from other projects but Python, LUA, maybe JavaScript,
and a way to use binary packets to obtain requests/control should be easy enough to support.
i should say i hate python. I am just making it easier for people to utilize this tool.. without requiring tons of custom code..
if pythoon didnt require indentions i'd prob try it more often
// -----------------------------------------------------------------------
// Used code from: https://docs.python.org/2/extending/newtypes.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stddef.h> /* For offsetof */
#include "network.h"
#include "antisurveillance.h"
#include "scripting.h"
#include "pcap.h"
#include "packetbuilding.h"
#include "instructions.h"
#include "utils.h"
#include "research.h"
#include "attacks.h"
#include <Python.h>
#ifndef offsetof
#define offsetof(type, member) ( (int) & ((type*)0) -> member )
#endif
const char generator_function[] = "content_generator";
const char scripting_main_loop_function[] = "script_perform";
// python configuration structure which has anything the script requires over time
typedef struct {
PyObject_HEAD
AS_context *ctx;
ConnectionProperties connection_parameters;
PacketBuildInstructions *instructions;
FilterInformation flt;
// *** incomplete
// defaults are used if the script doesn't provide... so its shared across several functions
// maybe retrieve this from C subsystem
int replay_count;
int replay_interval;
} PyAS_Config;
// deallocate the structure which was created for the C extension for python
static void PyASC_dealloc(PyAS_Config *self) {
PyObject_GC_UnTrack(self);
Py_TYPE(self)->tp_free((PyObject*)self);
}
// allocate the structure which is used to bridge between python, and our C extension
static PyObject *PyASC_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
PyAS_Config *self;
self = (PyAS_Config *)type->tp_alloc(type, 0);
return (PyObject *)self;
}
// this is literally the function that gets called while initializing the object which allows controlling the anti surveillance software
static int PyASC_init(PyAS_Config *self, PyObject *args, PyObject *kwds) {
void *ctx = NULL;
static char *kwlist[] = { "ctx", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|K", kwlist, &ctx))
return -1;
if (ctx) self->ctx = ctx;
// some global defaults... *** todo: retrieve fromm C side, or double check its used everywhere
self->replay_count = 99999999;
self->replay_interval = 1;
return 0;
}
// immediate exit of the application by doing exit() in python
static PyObject *PyASC_DIE(PyAS_Config* self){
exit(-1);
}
// disable the system temporarily
static PyObject *PyASC_Disable(PyAS_Config* self){
if (self->ctx) self->ctx->paused = 1;
Py_INCREF(Py_None);
return Py_None;
}
// enable the system
static PyObject *PyASC_Enable(PyAS_Config* self){
if (self->ctx) self->ctx->paused = 0;
Py_INCREF(Py_None);
return Py_None;
}
// clear all attack structures, and outgoing queues
static PyObject *PyASC_Clear(PyAS_Config* self){
if (self->ctx) AS_Clear_All((AS_context *)self->ctx);
Py_INCREF(Py_None);
return Py_None;
}
// *** todo: modify this to accept the filter here if its been created..
// also accept count, and interval.. and setup a global default (maybe in self) to let python modify
static PyObject *PyASC_PCAPload(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = { "filename", "use_python_filter", "destination_port", 0 };
char *filename = NULL;
int use_python_filter = 0;
int destination_port = 80;
int ret = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ii", kwd_list, &filename, &use_python_filter, &destination_port)) {
PyErr_Print();
return NULL;
}
if (filename && self->ctx) {
// *** todo: allow modified parameters, and setting globals to be used for everything
ret = PCAPtoAttack(self->ctx, (char *)filename, destination_port, self->replay_count, self->replay_interval, use_python_filter ? &self->flt : NULL);
}
return PyInt_FromLong(ret);
}
// save all packet captures to a filename fromm network outgoing queue
static PyObject *PyASC_PCAPsave(PyAS_Config* self, PyObject *Pfilename){
const char* s = PyString_AsString(Pfilename);
if (s && self->ctx) {
pthread_mutex_lock(&self->ctx->network_queue_mutex);
PcapSave(self->ctx, (char *)s, self->ctx->outgoing_queue, NULL, 0);
pthread_mutex_unlock(&self->ctx->network_queue_mutex);
}
Py_INCREF(Py_None);
return Py_None;
}
// count outgoing network queue
static PyObject *PyASC_QueueIncomingCount(PyAS_Config* self){
long ret = 0;
if (self->ctx) {
pthread_mutex_lock(&self->ctx->network_queue_mutex);
ret = L_count((LINK *)self->ctx->incoming_queue);
pthread_mutex_unlock(&self->ctx->network_queue_mutex);
}
return PyInt_FromLong(ret);
}
// count outgoing network queue
static PyObject *PyASC_QueueOutgoingCount(PyAS_Config* self){
long ret = 0;
if (self->ctx) {
pthread_mutex_lock(&self->ctx->network_queue_mutex);
ret = L_count((LINK *)self->ctx->outgoing_queue);
pthread_mutex_unlock(&self->ctx->network_queue_mutex);
}
return PyInt_FromLong(ret);
}
// count outgoing network queue
static PyObject *PyASC_QueueOutgoingPoolCount(PyAS_Config* self){
long ret = 0;
if (self->ctx) {
pthread_mutex_lock(&self->ctx->network_queue_mutex);
ret = L_count((LINK *)self->ctx->outgoing_pool_waiting);
pthread_mutex_unlock(&self->ctx->network_queue_mutex);
}
return PyInt_FromLong(ret);
}
// count outgoing network queue
static PyObject *PyASC_QueueIncomingPoolCount(PyAS_Config* self){
long ret = 0;
if (self->ctx) {
pthread_mutex_lock(&self->ctx->network_queue_mutex);
ret = L_count((LINK *)self->ctx->incoming_pool_waiting);
pthread_mutex_unlock(&self->ctx->network_queue_mutex);
}
return PyInt_FromLong(ret);
}
// count outgoing network queue
static PyObject *PyASC_NetworkCount(PyAS_Config* self){
long ret = 0;
if (self->ctx) {
pthread_mutex_lock(&self->ctx->network_queue_mutex);
ret = L_count((LINK *)self->ctx->outgoing_queue);
pthread_mutex_unlock(&self->ctx->network_queue_mutex);
}
return PyInt_FromLong(ret);
}
// count outgoing network queue
static PyObject *PyASC_AttackCount(PyAS_Config* self){
long ret = 0;
if (self->ctx) ret = L_count((LINK *)self->ctx->attack_list);
return PyInt_FromLong(ret);
}
// count outgoing network queue
static PyObject *PyASC_TracerouteCount(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = { "completed", "disabled", 0 };
long ret = 0;
int completed = 0;
int disabled = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwd_list, &completed, &disabled)) {
PyErr_Print();
return NULL;
}
if (self->ctx) ret = Traceroute_Count(self->ctx, completed, disabled) ;
return PyInt_FromLong(ret);
}
// clear the outgoing network queue
static PyObject *PyASC_NetworkClear(PyAS_Config* self){
// clear all packets using the context given
if (self->ctx) ClearPackets((AS_context *)self->ctx);
Py_INCREF(Py_None);
return Py_None;
}
// disable flushing the outgoing network queue to the live wire
static PyObject *PyASC_NetworkOff(PyAS_Config* self){
if (self->ctx) self->ctx->network_disabled = 1;
Py_INCREF(Py_None);
return Py_None;
}
// enable flushing the network queue to the live wire
static PyObject *PyASC_NetworkOn(PyAS_Config* self){
if (self->ctx) self->ctx->network_disabled = 0;
Py_INCREF(Py_None);
return Py_None;
}
// set the context which is the glue to apply any changes
// *** figure out how to set this without using this solution...
static PyObject *PyASC_CTXSet(PyAS_Config* self, PyObject *Pctx){
void *ctx = PyLong_AsVoidPtr(Pctx);
self->ctx = ctx;
return PyInt_FromLong(1);
//Py_INCREF(Py_None);
//return Py_None;
}
// clear all attack structures
static PyObject *PyASC_AttackClear(PyAS_Config* self){
if (self->ctx) AS_Clear_All((AS_context *)self->ctx);
Py_INCREF(Py_None);
return Py_None;
}
// perform one iteration of all attack structures
static PyObject *PyASC_AttackPerform(PyAS_Config* self) {
if (self->ctx) AS_perform(self->ctx);
Py_INCREF(Py_None);
return Py_None;
}
// disable blackhole attacks
static PyObject *PyASC_BlackholeDisable(PyAS_Config* self){
if (self->ctx) self->ctx->blackhole_paused = 0;
Py_INCREF(Py_None);
return Py_None;
}
// enable blackhole attacks
static PyObject *PyASC_BlackholeEnable(PyAS_Config* self){
if (self->ctx) self->ctx->blackhole_paused = 1;
Py_INCREF(Py_None);
return Py_None;
}
// clear all blackhole attack parameters (targets)
static PyObject *PyASC_BlackholeClear(PyAS_Config* self){
if (self->ctx) BH_Clear(self->ctx);
Py_INCREF(Py_None);
return Py_None;
}
// add a target to the blackhole attack
static PyObject *PyASC_BlackholeAdd(PyAS_Config* self, PyObject *Ptarget){
const char* target = PyString_AsString(Ptarget);
if (self->ctx)
BH_add_IP(self->ctx, inet_addr(target));
Py_INCREF(Py_None);
return Py_None;
}
// remove a single target from the blackhole attack
static PyObject *PyASC_BlackholeDel(PyAS_Config* self, PyObject *Ptarget) {
const char* target = PyString_AsString(Ptarget);
if (self->ctx)
BH_del_IP(self->ctx, inet_addr(target));
Py_INCREF(Py_None);
return Py_None;
}
// prepare a filter with particular flags, and values those filter flags requires
static PyObject *PyASC_FilterPrepare(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = { "source_ip", "destination_ip", "source_port", "destination_port",
"packet_flags", "familiar", 0};
char *source_ip = NULL;
char *destination_ip = NULL;
int source_port = 0;
int destination_port = 0;
int packet_flags = 0;
int familiar = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ssiiii", kwd_list, &source_ip, &destination_ip, &source_port, &destination_port, &packet_flags, &familiar)) {
PyErr_Print();
return NULL;
}
if (source_ip) {
// prepare either IPv4, or IPv6 client ip checking
IP_prepare(source_ip, &self->flt.source_ip, &self->flt.source_ipv6, &self->flt.is_source_ipv6);
self->flt.flags |= FILTER_CLIENT_IP;
}
if (destination_ip) {
// prepare either IPv4, or IPv6 server ip checking
IP_prepare(destination_ip, &self->flt.destination_ip, &self->flt.destination_ipv6, &self->flt.is_destination_ipv6);
self->flt.flags |= FILTER_SERVER_IP;
}
if (source_port)
FilterPrepare(&self->flt, FILTER_CLIENT_PORT, source_port);
if (destination_port)
FilterPrepare(&self->flt, FILTER_SERVER_PORT, destination_port);
// *** need to work this out more
if (packet_flags)
FilterPrepare(&self->flt, FILTER_PACKET_FLAGS, packet_flags);
if (familiar)
FilterPrepare(&self->flt, FILTER_PACKET_FAMILIAR, 0);
Py_INCREF(Py_None);
return Py_None;
}
// create a new fiilter.. if one was already beign created hten it will be discarded
static PyObject *PyASC_FilterCreate(PyAS_Config* self, PyObject *args, PyObject *kwds) {
// zero the filter
memset((void *)&self->flt, 0, sizeof(FilterInformation));
// must initialize the filter
self->flt.init = 1;
Py_INCREF(Py_None);
return Py_None;
}
// instructions add a tcp close from a particular side of the connection
// default is from client
static PyObject *PyASC_InstructionsTCPClose(PyAS_Config* self, PyObject *args, PyObject *kwds) {
int from_client = 1;
static char *kwd_list[] = { "from_client", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &from_client)) {
PyErr_Print();
return NULL;
}
int ret = GenerateTCPCloseConnectionInstructions(&self->connection_parameters, &self->instructions, from_client);
return PyInt_FromLong(ret);
}
// send data from one side of the tcp conneccton to the other
static PyObject *PyASC_InstructionsTCPSend(PyAS_Config* self, PyObject *args, PyObject *kwds) {
int from_client = 0;
char *data = NULL;
int size = 0;
int ret = 0;
static char *kwd_list[] = { "from_client", "data", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "is#", kwd_list, &from_client, &data, &size)) {
PyErr_Print();
return NULL;
}
ret = GenerateTCPSendDataInstructions(&self->connection_parameters, &self->instructions, from_client, data, size);
return PyInt_FromLong(ret);
}
// create packets for opening a tcp conneection
static PyObject *PyASC_InstructionsTCPOpen(PyAS_Config* self, PyObject *args, PyObject *kwds) {
int ret = GenerateTCPConnectionInstructions(&self->connection_parameters, &self->instructions);
return PyInt_FromLong(ret);
}
// this is sortof redundant... it only takes 4 other commands (2 of the same)
static PyObject *PyASC_BuildHTTP(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {
"client_ip", "client_port", "destination_ip", "destination_port",
"client_body", "client_body_size", "server_body", "server_body_size",
"count", "interval",
// these are not required
"client_ttl", "server_ttl",
"client_window_size", "server_window_size","client_seq", "server_seq", "client_identifier",
"server_identifier", "client_os","server_os", "gzip_enable", "gzip_percentage","gzip_size",
"gzip_injections", 0};
char *client_ip = NULL, *destination_ip = NULL;
char *server_body = NULL, *client_body = NULL;
int client_body_size = 0, server_body_size = 0;
int client_port = 0, destination_port = 0, client_ttl = 0, server_ttl = 0, client_window_size = 0;
int server_window_size = 0;
unsigned long client_seq = 0, server_seq = 0, client_identifier = 0, server_identifier = 0;
int client_os = 0, server_os = 0;
int count = self->replay_count;
int interval = self->replay_interval;
AS_attacks *aptr = NULL;
// *** finish implementing gzip here.. it has to create a new thread if the percentage matches
// is gzip attack enabled? default yes
int gzip_enable = 1;
// what percentage chance does this http session get affected? (Default 30)
int gzip_percentage = 30;
// default to 100megs
int gzip_size = 1024*1024*100;
// injections = rand between 1-5
int gzip_injections = 1+ (rand()%5);
int ret = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sisis#s#|iiiiiikkkkiiiiii", kwd_list,
&client_ip, &client_port, &destination_ip, &destination_port, &client_body, &client_body_size,
&server_body, &server_body_size, &count, &interval, &client_ttl, &server_ttl, &client_window_size,
&server_window_size, &client_seq, &server_seq, &client_identifier, &server_identifier, &client_os,
&server_os, &gzip_enable, &gzip_percentage, &gzip_size, &gzip_injections)) {
PyErr_Print();
return NULL;
}
memset((void *)&self->connection_parameters, 0, sizeof(ConnectionProperties));
// the new connection needs these variables prepared
IP_prepare(destination_ip, &self->connection_parameters.server_ip, &self->connection_parameters.server_ipv6, &self->connection_parameters.is_ipv6);
IP_prepare(client_ip, &self->connection_parameters.client_ip, &self->connection_parameters.client_ipv6, &self->connection_parameters.is_ipv6);
self->connection_parameters.server_port = destination_port;
self->connection_parameters.client_port = client_port;
self->connection_parameters.server_identifier = server_identifier ? server_identifier : rand()%0xFFFFFFFF;
self->connection_parameters.client_identifier = client_identifier ? client_identifier : rand()%0xFFFFFFFF;
self->connection_parameters.server_seq = server_seq ? server_seq : rand()%0xFFFFFFFF;
self->connection_parameters.client_seq = client_seq ? client_seq : rand()%0xFFFFFFFF;
self->connection_parameters.client_ttl = client_ttl ? client_ttl : 64;
self->connection_parameters.server_ttl = server_ttl ? server_ttl : 53;
self->connection_parameters.max_packet_size_client = client_window_size ? client_window_size : (1500 - (20 * 2 + 12));
self->connection_parameters.max_packet_size_server = server_window_size ? server_window_size : (1500 - (20 * 2 + 12));;
gettimeofday(&self->connection_parameters.ts, NULL);
// free all instructions used by this python module
PacketBuildInstructionsFree(&self->instructions);
// open the connection...
if (GenerateTCPConnectionInstructions(&self->connection_parameters, &self->instructions) != 1) { ret = -2; goto err; }
// now we must send data from client to server (http request)
if (GenerateTCPSendDataInstructions(&self->connection_parameters, &self->instructions, FROM_CLIENT, client_body, client_body_size) != 1) { ret = -3; goto err; }
// now we must send data from the server to the client (web page body)
if (GenerateTCPSendDataInstructions(&self->connection_parameters, &self->instructions, FROM_SERVER, server_body, server_body_size) != 1) { ret = -4; goto err; }
// now lets close the connection from client side first
if (GenerateTCPCloseConnectionInstructions(&self->connection_parameters, &self->instructions, FROM_CLIENT) != 1) { ret = -5; goto err; }
// now lets create the attak structure to begin...
if ((aptr = (AS_attacks *)calloc(1, sizeof(AS_attacks))) == NULL) goto err;
aptr->ctx = self->ctx;
aptr->id = rand()%5000;
pthread_mutex_init(&aptr->pause_mutex, NULL);
aptr->type = ATTACK_SESSION;
aptr->count = count;
aptr->repeat_interval = interval;
// that concludes all packets
aptr->packet_build_instructions = self->instructions;
// lets unlink it from our structure..
self->instructions = NULL;
// now lets build the low level packets for writing to the network interface
BuildPackets(aptr);
if (aptr != NULL) {
// link it in.
aptr->next = self->ctx->attack_list;
self->ctx->attack_list = aptr;
// lets return the ID
ret = aptr->id;
}
err:;
if (ret <= 0) {
PacketBuildInstructionsFree(&self->instructions);
}
return PyInt_FromLong(ret);
}
static PyObject *PyASC_AttackDetails(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"attack_id", 0};
int attack_id = 0;
AS_attacks *aptr = NULL;
PyObject *pRet = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &attack_id)) {
PyErr_Print();
return NULL;
}
if (self->ctx) {
aptr = AttackFind(self->ctx, attack_id, NULL, NULL, NULL, 0, 0, 0, 0);
if (aptr != NULL) {
pRet = PyAttackDetails(aptr);
}
}
return pRet;
}
// start a new instruction set (removing anything previously not saved, etc)
static PyObject *PyASC_InstructionsCreate(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {
"client_ip", "client_port", "destination_ip", "destination_port", "client_ttl", "server_ttl",
"client_window_size", "server_window_size","client_seq", "server_seq", "client_identifier",
"server_identifier", "client_os","server_os", 0};
char *client_ip = NULL, *destination_ip = NULL;
int client_port = 0, destination_port = 0, client_ttl = 0, server_ttl = 0, client_window_size = 0;
int server_window_size = 0;
unsigned long client_seq = 0, server_seq = 0, client_identifier = 0, server_identifier = 0;
int client_os = 0, server_os = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sisiiiii|kkkkii", kwd_list, &client_ip, &client_port,
&destination_ip, &destination_port, &client_ttl, &server_ttl, &client_window_size, &server_window_size,
&client_seq, &server_seq, &client_identifier, &server_identifier, &client_os, &server_os)) {
PyErr_Print();
return NULL;
}
memset((void *)&self->connection_parameters, 0, sizeof(ConnectionProperties));
// the new connection needs these variables prepared
IP_prepare(destination_ip, &self->connection_parameters.server_ip, &self->connection_parameters.server_ipv6, &self->connection_parameters.is_ipv6);
//printf("ipv6? %d ip %s\n", self->connection_parameters.is_ipv6, destination_ip);
IP_prepare(client_ip, &self->connection_parameters.client_ip, &self->connection_parameters.client_ipv6, &self->connection_parameters.is_ipv6);
//printf("ipv6? %d ip %s\n", self->connection_parameters.is_ipv6, client_ip);
self->connection_parameters.server_port = destination_port;
self->connection_parameters.client_port = client_port;
self->connection_parameters.server_identifier = server_identifier;
self->connection_parameters.client_identifier = client_identifier;
self->connection_parameters.server_seq = server_seq;
self->connection_parameters.client_seq = client_seq;
self->connection_parameters.client_ttl = client_ttl;
self->connection_parameters.server_ttl = server_ttl;
self->connection_parameters.max_packet_size_client = client_window_size;
self->connection_parameters.max_packet_size_server = server_window_size;
// free all instructions used by this python module
PacketBuildInstructionsFree(&self->instructions);
Py_INCREF(Py_None);
return Py_None;
}
// ------------------
// turn an instruction set in meomry that was built into an attack structure for live attacks
// it doesnt need anymore information about the instructions since they should be in memory.
static PyObject *PyASC_InstructionsBuildAttack(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"count", "interval", "skip_adjustments", 0};
int ret = 0;
int count = self->replay_count;
int interval = self->replay_interval;
int skip_adjustments=0;
AS_attacks *aptr = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iii", kwd_list, &count, &interval, &skip_adjustments))
return NULL;
if (self->ctx) {
if (self->instructions == NULL) {
// show error.. no instructions to turn into an attack
} else {
aptr = InstructionsToAttack(self->ctx, self->instructions, count, interval);
// if it worked.. lets return its ID
if (aptr != NULL) {
aptr->skip_adjustments = skip_adjustments;
// link the attack to make it active
aptr->next = self->ctx->attack_list;
self->ctx->attack_list = aptr;
// return the attack ID
ret = aptr->id;
return PyInt_FromLong(ret);
}
}
}
Py_INCREF(Py_None);
return Py_None;
}
// enable attacks by id, ips, ports, or age
static PyObject *PyASC_AttackEnable(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"id","source_ip","destination_ip","any_ip","source_port","destination_port",
"any_port", "age", 0};
int id = 0;
char *source_ip = NULL, *destination_ip = NULL, *any_ip = NULL;
int source_port = 0, destination_port = 0, any_port = 0, age = 0;
AS_attacks *aptr = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|isssiiii", kwd_list, &id, &source_ip, &destination_ip,
&any_ip, &source_port, &destination_port, &any_port, &age)) {
PyErr_Print();
return NULL;
}
if ((aptr = AttackFind(self->ctx, id, source_ip, destination_ip, any_ip, source_port, destination_port, any_port, age)) != NULL) {
aptr->paused = 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// disable attacks by id, ips, ports, or age
// complete this ***
static PyObject *PyASC_AttackDisable(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"id","source_ip","destination_ip","any_ip","source_port","destination_port",
"any_port", "age", 0};
int id = 0;
char *source_ip = NULL, *destination_ip = NULL, *any_ip = NULL;
int source_port = 0, destination_port = 0, any_port = 0, age = 0;
AS_attacks *aptr = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|isssiiii", kwd_list, &id, &source_ip, &destination_ip,
&any_ip, &source_port, &destination_port, &any_port, &age)) {
PyErr_Print();
return NULL;
}
if ((aptr = AttackFind(self->ctx, id, source_ip, destination_ip, any_ip, source_port, destination_port, any_port, age)) != NULL) {
aptr->paused = 1;
}
Py_INCREF(Py_None);
return Py_None;
}
// attack list and have optional filters to narrow it down
// this just returns the entire list ATM... i have to add filtering (using PyList_New() with the value, innstead of append)
// redo soon
static PyObject *PyASC_AttackList(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"source_ip","destination_ip","any_ip","source_port","destination_port",
"any_port", "age", 0};
char *source_ip = NULL, *destination_ip = NULL, *any_ip = NULL;
int source_port = 0, destination_port = 0, any_port = 0, age = 0;
int attack_count = 0;
PyObject *PAttackList = NULL;
AS_attacks *aptr = NULL;
int i = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sssiiii", kwd_list, &source_ip, &destination_ip, &any_ip, &source_port, &destination_port, &any_port, &age)) {
PyErr_Print();
return NULL;
}
if (self->ctx == NULL) return NULL;
attack_count = (int)L_count((LINK *)self->ctx->attack_list);
PAttackList = PyList_New(attack_count);
if (PAttackList == NULL) {
PyErr_Print();
return NULL;
}
aptr = self->ctx->attack_list;
while (aptr != NULL) {
PyList_SET_ITEM(PAttackList, i++, PyLong_FromLong(aptr->id));
aptr = aptr->next;
}
//Py_INCREF(PAttackList);
return PAttackList;
}
// retrieve distance between two nodes via traceroute, and other mechanisms
// fuzzy means we alllow imaginary nodes whenever data isnt complete
static PyObject *PyASC_TracerouteDistance(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"first_address", "second_address", "check_targets", "fuzzy", 0};
int ret = 0;
char *first_address = NULL;
struct in6_addr first_ipv6;
int first_is_ipv6 = 0;
uint32_t first_ipv4 = 0;
char *second_address = NULL;
struct in6_addr second_ipv6;
int second_is_ipv6 = 0;
uint32_t second_ipv4 = 0;
TracerouteSpider *Sfirst = NULL, *Ssecond = NULL;
int check_targets = 0;
int imaginary=0;
// check_targets means check ->target as well as ->hop (we wanna scan by the actual client/server address instead of routes)
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|ii", kwd_list, &first_address, &second_address, &check_targets, &imaginary)) {
PyErr_Print();
return NULL;
}
IP_prepare(first_address, &first_ipv4, &first_ipv6, &first_is_ipv6);
IP_prepare(second_address, &second_ipv4, &second_ipv6, &second_is_ipv6);
// make sure both are the same type of address (cant mix 4, and 6)
if (first_is_ipv6 != second_is_ipv6) return NULL;
if (self->ctx) {
Sfirst = Traceroute_Find(self->ctx, first_ipv4, &first_ipv6, check_targets);
Ssecond = Traceroute_Find(self->ctx, second_ipv4, &second_ipv6, check_targets);
if (Sfirst && Ssecond) {
printf("IDs: %X %X\n", Sfirst->identifier_id, Ssecond->identifier_id);
ret = Traceroute_Compare(self->ctx, Sfirst, Ssecond, imaginary);
printf("Distance between %s -> %s: %d\n", first_address, second_address, ret);
}
}
return PyInt_FromLong(ret);
}
static PyObject *PyASC_TracerouteGenerateRandomGEO(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"country",0};
int ret = 0;
char *country = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwd_list, &country)) {
PyErr_Print();
return NULL;
}
if (self->ctx) {
ret = TracerouteAddRandomIP(self->ctx, country);
}
return PyInt_FromLong(ret);
}
static PyObject *PyASC_MergeAttacks(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"destination_attack_id","source_attack_id",0};
long destination_id = 0, source_id = 0;
AS_attacks *dst = NULL;
AS_attacks *src = NULL;
int ret = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "kk", kwd_list, &destination_id, &source_id)) {
PyErr_Print();
return NULL;
}
if (self->ctx) {
dst = AttackFind(self->ctx, destination_id, NULL, NULL, NULL, 0, 0, 0, 0);
src = AttackFind(self->ctx, source_id, NULL, NULL, NULL, 0, 0, 0, 0);
if (dst && src) {
// link via dependency...
dst->dependency = src;
//ret = MergeAttacks(dst, src);
}
}
return PyInt_FromLong(ret);
}
// enable flushing the network queue to the live wire
static PyObject *PyASC_ScriptEnable(PyAS_Config* self){
if (self->ctx) self->ctx->script_enable = 1;
Py_INCREF(Py_None);
return Py_None;
}
// enable flushing the network queue to the live wire
static PyObject *PyASC_ScriptDisable(PyAS_Config* self){
if (self->ctx) self->ctx->script_enable = 0;
Py_INCREF(Py_None);
return Py_None;
}
// enable flushing the network queue to the live wire
static PyObject *PyASC_SpiderLoad(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"filename",0};
char fname[] = "traceroute";
char *filename = (char *)&fname;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwd_list, &filename)) {
PyErr_Print();
return NULL;
}
if (self->ctx) {
Spider_Load(self->ctx, filename);
}
Py_INCREF(Py_None);
return Py_None;
}
// enable flushing the network queue to the live wire
static PyObject *PyASC_SpiderSave(PyAS_Config* self, PyObject *args, PyObject *kwds) {
static char *kwd_list[] = {"filename",0};
char fname[] = "traceroute";
char *filename = (char *)&fname;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwd_list, &filename)) {
PyErr_Print();
return NULL;
}
if (self->ctx) {
Spider_Save(self->ctx);
}
Py_INCREF(Py_None);
return Py_None;
}
static int _skip = 20;
// hack for temporary interactive console to debug
static PyObject *PyASC_Skip(PyAS_Config* self){
int ret = 0;
if (_skip-- == 0) {
ret = 1;
_skip = 20;
}
return PyInt_FromLong(ret);
}
// retry all missing traceroutes
static PyObject *PyASC_TracerouteRetry(PyAS_Config* self){
if (self->ctx) Traceroute_RetryAll(self->ctx);
Py_INCREF(Py_None);
return Py_None;