2 * Copyright (c) 2007 S.Sam Arun Raj
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/types.h>
28 #include <sys/capsicum.h>
31 #include <capsicum_helpers.h>
49 #include <libcasper.h>
50 #include <casper/cap_fileargs.h>
54 ELFTC_VCSID("$Id: strings.c 3648 2018-11-22 23:26:43Z emaste $");
66 ENCODING_16BIT_LITTLE,
71 #define PRINTABLE(c) \
72 ((c) >= 0 && (c) <= 255 && \
73 ((c) == '\t' || isprint((c)) || \
74 (encoding == ENCODING_8BIT && (c) > 127)))
76 static int encoding_size, entire_file, show_filename, show_loc;
77 static enum encoding_style encoding;
78 static enum radix_style radix;
79 static intmax_t min_len;
81 static struct option strings_longopts[] = {
82 { "all", no_argument, NULL, 'a'},
83 { "bytes", required_argument, NULL, 'n'},
84 { "encoding", required_argument, NULL, 'e'},
85 { "help", no_argument, NULL, 'h'},
86 { "print-file-name", no_argument, NULL, 'f'},
87 { "radix", required_argument, NULL, 't'},
88 { "version", no_argument, NULL, 'v'},
92 int getcharacter(FILE *, long *);
93 int handle_file(fileargs_t *fa, const char *);
94 int handle_elf(const char *, FILE *);
95 int handle_binary(const char *, FILE *, size_t);
96 int find_strings(const char *, FILE *, off_t, off_t);
97 void show_version(void);
101 * strings(1) extracts text(contiguous printable characters)
102 * from elf and binary files.
105 main(int argc, char **argv)
114 if (elf_version(EV_CURRENT) == EV_NONE)
115 errx(EXIT_FAILURE, "ELF library initialization failed: %s",
118 while ((ch = getopt_long(argc, argv, "1234567890ae:fhn:ot:Vv",
119 strings_longopts, NULL)) != -1) {
125 if (*optarg == 's') {
126 encoding = ENCODING_7BIT;
127 } else if (*optarg == 'S') {
128 encoding = ENCODING_8BIT;
129 } else if (*optarg == 'b') {
130 encoding = ENCODING_16BIT_BIG;
132 } else if (*optarg == 'B') {
133 encoding = ENCODING_32BIT_BIG;
135 } else if (*optarg == 'l') {
136 encoding = ENCODING_16BIT_LITTLE;
138 } else if (*optarg == 'L') {
139 encoding = ENCODING_32BIT_LITTLE;
149 min_len = strtoimax(optarg, (char**)NULL, 10);
151 errx(EX_USAGE, "option -n should specify a "
152 "positive decimal integer.");
161 radix = RADIX_DECIMAL;
162 else if (*optarg == 'o')
164 else if (*optarg == 'x')
197 cap_rights_init(&rights, CAP_READ, CAP_SEEK, CAP_FSTAT, CAP_FCNTL, CAP_MMAP_R);
198 fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN);
200 err(1, "Unable to initialize casper fileargs");
202 caph_cache_catpages();
203 if (caph_limit_stdio() < 0 || caph_enter_casper() < 0) {
205 err(1, "Unable to enter capability mode");
211 rc = find_strings("{standard input}", stdin, 0, 0);
212 else while (*argv != NULL) {
213 if (handle_file(fa, *argv) != 0)
224 handle_file(fileargs_t *fa, const char *name)
231 pfile = fileargs_fopen(fa, name, "rb");
233 warnx("'%s': %s", name, strerror(errno));
237 rt = handle_elf(name, pfile);
243 * Files not understood by handle_elf, will be passed off here and will
244 * treated as a binary file. This would include text file, core dumps ...
247 handle_binary(const char *name, FILE *pfile, size_t size)
250 (void)fseeko(pfile, 0, SEEK_SET);
251 return (find_strings(name, pfile, 0, size));
255 * Will analyse a file to see if it ELF, other files including ar(1),
256 * core dumps are passed off and treated as flat binary files. Unlike
257 * GNU size in FreeBSD this routine will not treat ELF object from
258 * different archs as flat binary files(has to overridden using -a).
261 handle_elf(const char *name, FILE *pfile)
272 if (fstat(fd, &buf) < 0)
275 /* If entire file is chosen, treat it as a binary file */
277 return (handle_binary(name, pfile, buf.st_size));
279 (void)lseek(fd, 0, SEEK_SET);
280 elf = elf_begin(fd, ELF_C_READ, NULL);
281 if (elf_kind(elf) != ELF_K_ELF) {
283 return (handle_binary(name, pfile, buf.st_size));
286 if (gelf_getehdr(elf, &elfhdr) == NULL) {
288 warnx("%s: ELF file could not be processed", name);
292 if (elfhdr.e_shnum == 0 && elfhdr.e_type == ET_CORE) {
294 return (handle_binary(name, pfile, buf.st_size));
297 while ((scn = elf_nextscn(elf, scn)) != NULL) {
298 if (gelf_getshdr(scn, &shdr) == NULL)
300 if (shdr.sh_type != SHT_NOBITS &&
301 (shdr.sh_flags & SHF_ALLOC) != 0) {
302 rc = find_strings(name, pfile, shdr.sh_offset,
312 * Retrieves a character from input stream based on the encoding
316 getcharacter(FILE *pfile, long *rt)
321 for(i = 0; i < encoding_size; i++) {
333 case ENCODING_16BIT_BIG:
334 *rt = (buf[0] << 8) | buf[1];
336 case ENCODING_16BIT_LITTLE:
337 *rt = buf[0] | (buf[1] << 8);
339 case ENCODING_32BIT_BIG:
340 *rt = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
341 ((long) buf[2] << 8) | buf[3];
343 case ENCODING_32BIT_LITTLE:
344 *rt = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
345 ((long) buf[3] << 24);
355 * Input stream is read until the end of file is reached or until
356 * the section size is reached in case of ELF files. Contiguous
357 * characters of >= min_size(default 4) will be displayed.
360 find_strings(const char *name, FILE *pfile, off_t offset, off_t size)
362 off_t cur_off, start_off;
367 if ((obuf = (char*)calloc(1, min_len + 1)) == NULL) {
368 fprintf(stderr, "Unable to allocate memory: %s\n",
373 (void)fseeko(pfile, offset, SEEK_SET);
377 if ((offset + size) && (cur_off >= offset + size))
380 memset(obuf, 0, min_len + 1);
381 for(i = 0; i < min_len; i++) {
382 if (getcharacter(pfile, &c) < 0)
387 cur_off += encoding_size;
389 if (encoding == ENCODING_8BIT &&
393 cur_off += encoding_size;
396 cur_off += encoding_size;
401 if (i >= min_len && ((cur_off <= offset + size) ||
404 printf("%s: ", name);
408 printf("%7ju ", (uintmax_t)start_off);
411 printf("%7jx ", (uintmax_t)start_off);
414 printf("%7jo ", (uintmax_t)start_off);
421 if ((offset + size) &&
422 (cur_off >= offset + size))
424 if (getcharacter(pfile, &c) < 0)
426 cur_off += encoding_size;
427 if (encoding == ENCODING_8BIT &&
444 #define USAGE_MESSAGE "\
445 Usage: %s [options] [file...]\n\
446 Print contiguous sequences of printable characters.\n\n\
448 -a | --all Scan the entire file for strings.\n\
449 -e ENC | --encoding=ENC Select the character encoding to use.\n\
450 -f | --print-file-name Print the file name before each string.\n\
451 -h | --help Print a help message and exit.\n\
452 -n N | --bytes=N | -N Print sequences with 'N' or more characters.\n\
453 -o Print offsets in octal.\n\
454 -t R | --radix=R Print offsets using the radix named by 'R'.\n\
455 -v | --version Print a version identifier and exit.\n"
461 fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
469 printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());