diff options
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | README.md | 26 | ||||
| -rw-r--r-- | bin2sh.c | 77 |
3 files changed, 108 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c279619 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + gcc -o bin2sh bin2sh.c + +clean: + rm -rf bin2sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..d3eac86 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# bin2sh + +This converts any file to a shell script, which outputs itself. + +This makes it easy to copy/paste a binary into a netcat session to +transfer a file to another system. + +## Building +``` +make +``` + +## Example +``` + % ./bin2sh Makefile +echo -ne "\x61\x6c\x6c\x3a\x0a\x09\x67\x63\x63\x20\x2d\x6f\x20\x62\x69\x6e" +echo -ne "\x32\x73\x68\x20\x62\x69\x6e\x32\x73\x68\x2e\x63\x0a\x0a\x63\x6c" +echo -ne "\x65\x61\x6e\x3a\x0a\x09\x72\x6d\x20\x2d\x72\x66\x20\x62\x69\x6e" +echo -ne "\x32\x73\x68\x0a" + + % ./bin2sh Makefile |bash > mf2 + + % md5sum mf2 Makefile +2c36f087e9cc78f3a6406f89e70728db mf2 +2c36f087e9cc78f3a6406f89e70728db Makefile +``` diff --git a/bin2sh.c b/bin2sh.c new file mode 100644 index 0000000..2ec81dd --- /dev/null +++ b/bin2sh.c @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <stdint.h> +#include <stdbool.h> + +#define WIDTH 10 + + +void usage(const char *progname) { + fprintf(stderr, "usage: %s <file> [-r <file>] [-w <width>]\n", progname); +} + + +int main(int argc, char *argv[]) { + FILE *fp; + uint8_t buf[1]; + int i = 0; + int opt; + char *redir = NULL; + unsigned int width = WIDTH; + bool r = false; + + + if (argc < 2) { + usage(argv[0]); + return EXIT_FAILURE; + } + + while ((opt = getopt(argc, argv, "w:r:")) != -1) { + switch (opt) { + case 'w': + width = atoi(optarg); + break; + case 'r': + redir = optarg; + break; + default: + usage(argv[0]); + return EXIT_FAILURE; + } + } + + argc -= optind; + argv += optind; + + fp = fopen(argv[0], "rb"); + if (fp == NULL) { + fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno)); + return EXIT_FAILURE; + } + + printf("echo -ne \""); + + while(fread(buf, 1, 1, fp)) { + i++; + printf("\\x%02x", buf[0]); + if (i % width == 0) { + if (redir) { + printf("\" %s %s\n", r ? ">>" : ">", redir); + r = true; + } else { + printf("\"\n"); + } + printf("echo -ne \""); + } + } + + if (i % WIDTH != 0) + printf("\"\n"); + + fclose(fp); + + return EXIT_SUCCESS; +} |
