forked from scanmem/scanmem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.c
1647 lines (1405 loc) · 49.2 KB
/
handlers.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
/*
$Id: handlers.c,v 1.12 2007-06-05 01:45:34+01 taviso Exp taviso $
Copyright (C) 2006,2007,2009 Tavis Ormandy <[email protected]>
Copyright (C) 2009 Eli Dupree <[email protected]>
Copyright (C) 2009,2010 WANG Lu <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "config.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <signal.h>
#include <assert.h>
#include <setjmp.h>
#include <alloca.h>
#include <strings.h> /*lint -esym(526,strcasecmp) */
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <sys/time.h>
#include <limits.h> /* to determine the word width */
#include <errno.h>
#include <inttypes.h>
#include <ctype.h>
#include "commands.h"
#include "endianness.h"
#include "handlers.h"
#include "interrupt.h"
#include "show_message.h"
#define USEPARAMS() ((void) vars, (void) argv, (void) argc) /* macro to hide gcc unused warnings */
/*lint -esym(818, vars, argv) dont want want to declare these as const */
/*
* This file defines all the command handlers used, each one is registered using
* registercommand(), and when a matching command is entered, the commandline is
* tokenized and parsed into an argv/argc.
*
* argv[0] will contain the command entered, so one handler can handle multiple
* commands by checking whats in there, but you still need seperate documentation
* for each command when you register it.
*
* Most commands will also need some documentation, see handlers.h for the format.
*
* Commands are allowed to read and modify settings in the vars structure.
*
*/
#define calloca(x,y) (memset(alloca((x) * (y)), 0x00, (x) * (y)))
/* try to determine the size of a pointer */
#ifndef ULONG_MAX
#warning ULONG_MAX is not defined!
#endif
#if ULONG_MAX == 4294967295UL
#define POINTER_FMT "%8lx"
#elif ULONG_MAX == 18446744073709551615UL
#define POINTER_FMT "%12lx"
#else
#define POINTER_FMT "%12lx"
#endif
bool handler__set(globals_t * vars, char **argv, unsigned argc)
{
unsigned block, seconds = 1;
char *delay = NULL;
char *end;
bool cont = false;
struct setting {
char *matchids;
char *value;
unsigned seconds;
} *settings = NULL;
assert(argc != 0);
assert(argv != NULL);
assert(vars != NULL);
if (argc < 2) {
show_error("expected an argument, type `help set` for details.\n");
return false;
}
/* supporting `set` for bytearray will cause annoying syntax problems... */
if ((vars->options.scan_data_type == BYTEARRAY)
||(vars->options.scan_data_type == STRING))
{
show_error("`set` is not supported for bytearray or string, use `write` instead.\n");
return false;
}
/* check if there are any matches */
if (vars->num_matches == 0) {
show_error("no matches are known.\n");
return false;
}
/* --- parse arguments into settings structs --- */
settings = calloca(argc - 1, sizeof(struct setting));
/* parse every block into a settings struct */
for (block = 0; block < argc - 1; block++) {
/* first seperate the block into matches and value, which are separated by '=' */
if ((settings[block].value = strchr(argv[block + 1], '=')) == NULL) {
/* no '=' found, whole string must be the value */
settings[block].value = argv[block + 1];
} else {
/* there is a '=', value+1 points to value string. */
/* use strndupa() to copy the matchids into a new buffer */
settings[block].matchids =
strndupa(argv[block + 1],
(size_t) (settings[block].value++ - argv[block + 1]));
}
/* value points to the value string, possibly with a delay suffix */
/* matchids points to the match-ids (possibly multiple) or NULL */
/* now check for a delay suffix (meaning continuous mode), eg 0xff/10 */
if ((delay = strchr(settings[block].value, '/')) != NULL) {
char *end = NULL;
/* parse delay count */
settings[block].seconds = strtoul(delay + 1, &end, 10);
if (*(delay + 1) == '\0') {
/* empty delay count, eg: 12=32/ */
show_error("you specified an empty delay count, `%s`, see `help set`.\n", settings[block].value);
return false;
} else if (*end != '\0') {
/* parse failed before end, probably trailing garbage, eg 34=9/16foo */
show_error("trailing garbage after delay count, `%s`.\n", settings[block].value);
return false;
} else if (settings[block].seconds == 0) {
/* 10=24/0 disables continous mode */
show_info("you specified a zero delay, disabling continuous mode.\n");
} else {
/* valid delay count seen and understood */
show_info("setting %s every %u seconds until interrupted...\n", settings[block].matchids ? settings[block]. matchids : "all", settings[block].seconds);
/* continuous mode on */
cont = true;
}
/* remove any delay suffix from the value */
settings[block].value =
strndupa(settings[block].value,
(size_t) (delay - settings[block].value));
} /* if (strchr('/')) */
} /* for(block...) */
/* --- setup a longjmp to handle interrupt --- */
if (INTERRUPTABLE()) {
/* control returns here when interrupted */
// settings is allocated with alloca, do not free it
// free(settings);
detach(vars->target);
ENDINTERRUPTABLE();
return true;
}
/* --- execute the parsed setting structs --- */
while (true) {
uservalue_t userval;
/* for every settings struct */
for (block = 0; block < argc - 1; block++) {
/* check if this block has anything to do this iteration */
if (seconds != 1) {
/* not the first iteration (all blocks get executed first iteration) */
/* if settings.seconds is zero, then this block is only executed once */
/* if seconds % settings.seconds is zero, then this block must be executed */
if (settings[block].seconds == 0
|| (seconds % settings[block].seconds) != 0)
continue;
}
/* convert value */
if (!parse_uservalue_number(settings[block].value, &userval)) {
show_error("bad number `%s` provided\n", settings[block].value);
goto fail;
}
/* check if specific match(s) were specified */
if (settings[block].matchids != NULL) {
char *id, *lmatches = NULL;
unsigned num = 0;
/* create local copy of the matchids for strtok() to modify */
lmatches = strdupa(settings[block].matchids);
/* now seperate each match, spearated by commas */
while ((id = strtok(lmatches, ",")) != NULL) {
match_location loc;
/* set to NULL for strtok() */
lmatches = NULL;
/* parse this id */
num = strtoul(id, &end, 0x00);
/* check that succeeded */
if (*id == '\0' || *end != '\0') {
show_error("could not parse match id `%s`\n", id);
goto fail;
}
/* check this is a valid match-id */
loc = nth_match(vars->matches, num);
if (loc.swath) {
value_t v;
value_t old;
void *address = remote_address_of_nth_element(loc.swath, loc.index /* ,MATCHES_AND_VALUES */);
/* copy val onto v */
/* XXX: valcmp? make sure the sizes match */
old = data_to_val(loc.swath, loc.index /* ,MATCHES_AND_VALUES */);
v.flags = old.flags = loc.swath->data[loc.index].match_info;
uservalue2value(&v, &userval);
show_info("setting *%p to %#"PRIx64"...\n", address, v.int64_value);
/* set the value specified */
fix_endianness(vars, &v);
if (setaddr(vars->target, address, &v) == false) {
show_error("failed to set a value.\n");
goto fail;
}
} else {
/* match-id > than number of matches */
show_error("found an invalid match-id `%s`\n", id);
goto fail;
}
}
} else {
matches_and_old_values_swath *reading_swath_index = (matches_and_old_values_swath *)vars->matches->swaths;
int reading_iterator = 0;
/* user wants to set all matches */
while (reading_swath_index->first_byte_in_child) {
/* Only actual matches are considered */
if (flags_to_max_width_in_bytes(reading_swath_index->data[reading_iterator].match_info) > 0)
{
void *address = remote_address_of_nth_element(reading_swath_index, reading_iterator /* ,MATCHES_AND_VALUES */);
/* XXX: as above : make sure the sizes match */
value_t old = data_to_val(reading_swath_index, reading_iterator /* ,MATCHES_AND_VALUES */);
value_t v;
v.flags = old.flags = reading_swath_index->data[reading_iterator].match_info;
uservalue2value(&v, &userval);
show_info("setting *%p to %"PRIx64"...\n", address, v.int64_value);
fix_endianness(vars, &v);
if (setaddr(vars->target, address, &v) == false) {
show_error("failed to set a value.\n");
goto fail;
}
}
/* Go on to the next one... */
++reading_iterator;
if (reading_iterator >= reading_swath_index->number_of_bytes)
{
reading_swath_index = local_address_beyond_last_element(reading_swath_index /* ,MATCHES_AND_VALUES */);
reading_iterator = 0;
}
}
} /* if (matchid != NULL) else ... */
} /* for(block) */
if (cont) {
sleep(1);
} else {
break;
}
seconds++;
} /* while(true) */
ENDINTERRUPTABLE();
return true;
fail:
ENDINTERRUPTABLE();
return false;
}
/* XXX: add yesno command to check if matches > 099999 */
/* FORMAT (don't change, front-end depends on this):
* [#no] addr, value, [possible types (separated by space)]
*/
bool handler__list(globals_t * vars, char **argv, unsigned argc)
{
unsigned i = 0;
int buf_len = 128; /* will be realloc later if necessary */
element_t *np = NULL;
char *v = malloc(buf_len);
if (v == NULL)
{
show_error("memory allocation failed.\n");
return false;
}
char *bytearray_suffix = ", [bytearray]";
char *string_suffix = ", [string]";
USEPARAMS();
if(!(vars->matches))
return true;
if (vars->regions)
np = vars->regions->head;
matches_and_old_values_swath *reading_swath_index = (matches_and_old_values_swath *)vars->matches->swaths;
int reading_iterator = 0;
/* list all known matches */
while (reading_swath_index->first_byte_in_child) {
match_flags flags = reading_swath_index->data[reading_iterator].match_info;
/* Only actual matches are considered */
if (flags_to_max_width_in_bytes(flags) > 0)
{
switch(globals.options.scan_data_type)
{
case BYTEARRAY:
; /* cheat gcc */
buf_len = flags.bytearray_length * 3 + 32;
v = realloc(v, buf_len); /* for each byte and the suffix', this should be enough */
if (v == NULL)
{
show_error("memory allocation failed.\n");
return false;
}
data_to_bytearray_text(v, buf_len, reading_swath_index, reading_iterator, flags.bytearray_length);
assert(strlen(v) + strlen(bytearray_suffix) + 1 <= buf_len); /* or maybe realloc is better? */
strcat(v, bytearray_suffix);
break;
case STRING:
; /* cheat gcc */
buf_len = flags.string_length + strlen(string_suffix) + 32; /* for the string and suffix, this should be enough */
v = realloc(v, buf_len);
if (v == NULL)
{
show_error("memory allocation failed.\n");
return false;
}
data_to_printable_string(v, buf_len, reading_swath_index, reading_iterator, flags.string_length);
assert(strlen(v) + strlen(string_suffix) + 1 <= buf_len); /* or maybe realloc is better? */
strcat(v, string_suffix);
break;
default: /* numbers */
; /* cheat gcc */
value_t val = data_to_val(reading_swath_index, reading_iterator /* ,MATCHES_AND_VALUES */);
truncval_to_flags(&val, flags);
valtostr(&val, v, buf_len);
break;
}
void *address = remote_address_of_nth_element(reading_swath_index,
reading_iterator /* ,MATCHES_AND_VALUES */);
unsigned long address_ul = (unsigned long)address;
int region_id = 99;
unsigned long match_off = 0;
const char *region_type = "??";
/* get region info belonging to the match -
* note: we assume the regions list and matches to be sorted
*/
while (np) {
region_t *region = np->data;
unsigned long region_start = (unsigned long)region->start;
if (address_ul < region_start + region->size &&
address_ul >= region_start) {
region_id = region->id;
match_off = address_ul - region->load_addr;
region_type = region_type_names[region->type];
break;
}
np = np->next;
}
fprintf(stdout, "[%2u] "POINTER_FMT", %2u + "POINTER_FMT", %5s, %s\n",
i++, address_ul, region_id, match_off, region_type, v);
}
/* Go on to the next one... */
++reading_iterator;
if (reading_iterator >= reading_swath_index->number_of_bytes)
{
assert(((matches_and_old_values_swath *)(local_address_beyond_last_element(reading_swath_index /* ,MATCHES_AND_VALUES */)))->number_of_bytes >= 0);
reading_swath_index = local_address_beyond_last_element(reading_swath_index /* ,MATCHES_AND_VALUES */);
reading_iterator = 0;
}
}
free(v);
return true;
}
/* XXX: handle multiple deletes, eg delete !1 2 3 4 5 6 */
bool handler__delete(globals_t * vars, char **argv, unsigned argc)
{
unsigned id;
char *end = NULL;
match_location loc;
if (argc != 2) {
show_error("was expecting one argument, see `help delete`.\n");
return false;
}
/* parse argument */
id = strtoul(argv[1], &end, 0x00);
/* check that strtoul() worked */
if (argv[1][0] == '\0' || *end != '\0') {
show_error("sorry, couldnt parse `%s`, try `help delete`\n", argv[1]);
return false;
}
loc = nth_match(vars->matches, id);
if (loc.swath)
{
/* It is not convenient to check whether anything else relies on this, so just mark it as not a REAL match */
memset(&(loc.swath->data[loc.index].match_info), 0, sizeof(match_flags));
return true;
}
else
{
/* I guess this is not a valid match-id */
show_warn("you specified a non-existant match `%u`.\n", id);
show_info("use \"list\" to list matches, or \"help\" for other commands.\n");
return false;
}
}
bool handler__reset(globals_t * vars, char **argv, unsigned argc)
{
USEPARAMS();
if (vars->matches) { free(vars->matches); vars->matches = NULL; vars->num_matches = 0; }
/* refresh list of regions */
l_destroy(vars->regions);
/* create a new linked list of regions */
if ((vars->regions = l_init()) == NULL) {
show_error("sorry, there was a problem allocating memory.\n");
return false;
}
/* read in maps if a pid is known */
if (vars->target && readmaps(vars->target, vars->regions) != true) {
show_error("sorry, there was a problem getting a list of regions to search.\n");
show_warn("the pid may be invalid, or you don't have permission.\n");
vars->target = 0;
return false;
}
return true;
}
bool handler__pid(globals_t * vars, char **argv, unsigned argc)
{
char *resetargv[] = { "reset", NULL };
char *end = NULL;
if (argc == 2) {
vars->target = (pid_t) strtoul(argv[1], &end, 0x00);
if (vars->target == 0) {
show_error("`%s` does not look like a valid pid.\n", argv[1]);
return false;
}
} else if (vars->target) {
/* print the pid of the target program */
show_info("target pid is %u.\n", vars->target);
return true;
} else {
show_info("no target is currently set.\n");
return false;
}
return handler__reset(vars, resetargv, 1);
}
bool handler__snapshot(globals_t * vars, char **argv, unsigned argc)
{
USEPARAMS();
/* check that a pid has been specified */
if (vars->target == 0) {
show_error("no target set, type `help pid`.\n");
return false;
}
/* remove any existing matches */
if (vars->matches) { free(vars->matches); vars->matches = NULL; vars->num_matches = 0; }
if (searchregions(vars, MATCHANY, NULL) != true) {
show_error("failed to save target address space.\n");
return false;
}
return true;
}
/* dregion [!][x][,x,...] */
bool handler__dregion(globals_t * vars, char **argv, unsigned argc)
{
unsigned id;
bool invert = false;
char *end = NULL, *idstr = NULL, *block = NULL;
element_t *np, *pp;
list_t *keep = NULL;
region_t *save;
/* need an argument */
if (argc < 2) {
show_error("expected an argument, see `help dregion`.\n");
return false;
}
/* check that there is a process known */
if (vars->target == 0) {
show_error("no target specified, see `help pid`\n");
return false;
}
/* check for an inverted match */
if (*argv[1] == '!') {
invert = true;
/* create a copy of the argument for strtok(), +1 to skip '!' */
block = strdupa(argv[1] + 1);
/* check for lone '!' */
if (*block == '\0') {
show_error("inverting an empty set, maybe try `reset` instead?\n");
return false;
}
/* create a list to keep the specified regions */
if ((keep = l_init()) == NULL) {
show_error("memory allocation error.\n");
return false;
}
} else {
invert = false;
block = strdupa(argv[1]);
}
/* loop for every number specified, eg "1,2,3,4,5" */
while ((idstr = strtok(block, ",")) != NULL) {
region_t *r = NULL;
/* set block to NULL for strtok() */
block = NULL;
/* attempt to parse as a regionid */
id = strtoul(idstr, &end, 0x00);
/* check that worked, "1,abc,4,,5,6foo" */
if (*end != '\0' || *idstr == '\0') {
show_error("could not parse argument %s.\n", idstr);
if (invert) {
if (l_concat(vars->regions, &keep) == -1) {
show_error("there was a problem restoring saved regions.\n");
l_destroy(vars->regions);
l_destroy(keep);
return false;
}
}
assert(keep == NULL);
return false;
}
/* initialise list pointers */
np = vars->regions->head;
pp = NULL;
/* find the correct region node */
while (np) {
r = np->data;
/* compare the node id to the id the user specified */
if (r->id == id)
break;
pp = np; /* keep track of prev for l_remove() */
np = np->next;
}
/* check if a match was found */
if (np == NULL) {
show_error("no region matching %u, or already moved.\n", id);
if (invert) {
if (l_concat(vars->regions, &keep) == -1) {
show_error("there was a problem restoring saved regions.\n");
l_destroy(vars->regions);
l_destroy(keep);
return false;
}
}
if (keep)
l_destroy(keep);
return false;
}
np = pp;
/* save this region if the match is inverted */
if (invert) {
assert(keep != NULL);
l_remove(vars->regions, np, (void *) &save);
if (l_append(keep, keep->tail, save) == -1) {
show_error("sorry, there was an internal memory error.\n");
free(save);
return false;
}
continue;
}
/* check for any affected matches before removing it */
if(vars->num_matches > 0)
{
region_t *s;
/* determine the correct pointer we're supposed to be checking */
if (np) {
assert(np->next);
s = np->next->data;
} else {
/* head of list */
s = vars->regions->head->data;
}
if (!(vars->matches = delete_by_region(vars->matches, &vars->num_matches, s, false)))
{
show_error("memory allocation error while deleting matches\n");
}
}
l_remove(vars->regions, np, NULL);
}
if (invert) {
region_t *s = keep->head->data;
if (vars->num_matches > 0)
{
if (!(vars->matches = delete_by_region(vars->matches, &vars->num_matches, s, true)))
{
show_error("memory allocation error while deleting matches\n");
}
}
/* okay, done with the regions list */
l_destroy(vars->regions);
/* and switch to the keep list */
vars->regions = keep;
}
return true;
}
bool handler__lregions(globals_t * vars, char **argv, unsigned argc)
{
element_t *np = vars->regions->head;
USEPARAMS();
if (vars->target == 0) {
show_error("no target has been specified, see `help pid`.\n");
return false;
}
if (vars->regions->size == 0) {
show_info("no regions are known.\n");
}
/* print a list of regions that are searched */
while (np) {
region_t *region = np->data;
fprintf(stderr, "[%2u] "POINTER_FMT", %7lu bytes, %5s, "POINTER_FMT", %c%c%c, %s\n",
region->id,
(unsigned long)region->start, region->size,
region_type_names[region->type], region->load_addr,
region->flags.read ? 'r' : '-',
region->flags.write ? 'w' : '-',
region->flags.exec ? 'x' : '-',
region->filename[0] ? region->filename : "unassociated");
np = np->next;
}
return true;
}
/* the name of the function is for history reason, now GREATERTHAN & LESSTHAN are also handled by this function */
bool handler__decinc(globals_t * vars, char **argv, unsigned argc)
{
uservalue_t val;
scan_match_type_t m;
USEPARAMS();
if(argc == 1)
{
memset(&val, 0x00, sizeof(val));
}
else if (argc > 2)
{
show_error("too many values specified, see `help %s`", argv[0]);
return false;
}
else
{
if (!parse_uservalue_number(argv[1], &val)) {
show_error("bad value specified, see `help %s`", argv[0]);
return false;
}
}
if (strcmp(argv[0], "=") == 0)
{
m = MATCHNOTCHANGED;
}
else if (strcmp(argv[0], "!=") == 0)
{
m = (argc == 1) ? MATCHCHANGED : MATCHNOTEQUALTO;
}
else if (strcmp(argv[0], "<") == 0)
{
m = (argc == 1) ? MATCHDECREASED : MATCHLESSTHAN;
}
else if (strcmp(argv[0], ">") == 0)
{
m = (argc == 1) ? MATCHINCREASED : MATCHGREATERTHAN;
}
else if (strcmp(argv[0], "+") == 0)
{
m = (argc == 1) ? MATCHINCREASED : MATCHINCREASEDBY;
}
else if (strcmp(argv[0], "-") == 0)
{
m = (argc == 1) ? MATCHDECREASED : MATCHDECREASEDBY;
}
else
{
show_error("unrecogised match type seen at decinc handler.\n");
return false;
}
if (vars->matches) {
if (checkmatches(vars, m, &val) == false) {
show_error("failed to search target address space.\n");
return false;
}
} else {
/* < > = != cannot be the initial scan */
if (argc == 1)
{
show_error("cannot use that search without matches\n");
return false;
}
else
{
if (searchregions(vars, m, &val) != true) {
show_error("failed to search target address space.\n");
return false;
}
}
}
if (vars->num_matches == 1) {
show_info("match identified, use \"set\" to modify value.\n");
show_info("enter \"help\" for other commands.\n");
}
return true;
}
bool handler__version(globals_t * vars, char **argv, unsigned argc)
{
USEPARAMS();
printversion(stderr);
return true;
}
bool handler__string(globals_t * vars, char **argv, unsigned argc)
{
/* test scan_data_type */
if (vars->options.scan_data_type != STRING)
{
show_error("scan_data_type is not string, see `help option`.\n");
return false;
}
/* test the length */
int i;
for(i = 0; (i < 3) && vars->current_cmdline[i]; ++i) {}
if (i != 3) /* cmdline too short */
{
show_error("please specify a string\n");
return false;
}
/* the string being scanned */
uservalue_t val;
val.string_value = vars->current_cmdline+2;
val.flags.string_length = strlen(val.string_value);
/* need a pid for the rest of this to work */
if (vars->target == 0) {
return false;
}
/* user has specified an exact value of the variable to find */
if (vars->matches) {
/* already know some matches */
if (checkmatches(vars, MATCHEQUALTO, &val) != true) {
show_error("failed to search target address space.\n");
return false;
}
} else {
/* initial search */
if (searchregions(vars, MATCHEQUALTO, &val) != true) {
show_error("failed to search target address space.\n");
return false;
}
}
/* check if we now know the only possible candidate */
if (vars->num_matches == 1) {
show_info("match identified, use \"set\" to modify value.\n");
show_info("enter \"help\" for other commands.\n");
}
return true;
}
bool handler__default(globals_t * vars, char **argv, unsigned argc)
{
uservalue_t val;
bool ret;
bytearray_element_t *array = NULL;
USEPARAMS();
switch(vars->options.scan_data_type)
{
case ANYNUMBER:
case ANYINTEGER:
case ANYFLOAT:
case INTEGER8:
case INTEGER16:
case INTEGER32:
case INTEGER64:
case FLOAT32:
case FLOAT64:
/* attempt to parse command as a number */
if (argc != 1)
{
show_error("unknown command\n");
ret = false;
goto retl;
}
if (!parse_uservalue_number(argv[0], &val)) {
show_error("unable to parse command `%s`\n", argv[0]);
ret = false;
goto retl;
}
break;
case BYTEARRAY:
/* attempt to parse command as a bytearray */
array = calloc(argc, sizeof(bytearray_element_t));
if (array == NULL)
{
show_error("there's a memory allocation error.\n");
ret = false;
goto retl;
}
if (!parse_uservalue_bytearray(argv, argc, array, &val)) {
show_error("unable to parse command `%s`\n", argv[0]);
ret = false;
goto retl;
}
break;
case STRING:
show_error("unable to parse command `%s`\nIf you want to scan for a string, use command `\"`.\n", argv[0]);
ret = false;
goto retl;
break;
default:
assert(false);
break;
}
/* need a pid for the rest of this to work */
if (vars->target == 0) {
ret = false;
goto retl;
}
/* user has specified an exact value of the variable to find */
if (vars->matches) {
/* already know some matches */
if (checkmatches(vars, MATCHEQUALTO, &val) != true) {
show_error("failed to search target address space.\n");
ret = false;
goto retl;
}
} else {
/* initial search */
if (searchregions(vars, MATCHEQUALTO, &val) != true) {
show_error("failed to search target address space.\n");
ret = false;
goto retl;
}
}
/* check if we now know the only possible candidate */
if (vars->num_matches == 1) {
show_info("match identified, use \"set\" to modify value.\n");
show_info("enter \"help\" for other commands.\n");
}
ret = true;
retl:
if (array)
free(array);
return ret;
}
bool handler__update(globals_t * vars, char **argv, unsigned argc)
{
USEPARAMS();
if (vars->matches) {
if (checkmatches(vars, MATCHANY, NULL) == false) {
show_error("failed to scan target address space.\n");
return false;
}
} else {
show_error("cannot use that command without matches\n");
return false;
}