Skip to content

Instantly share code, notes, and snippets.

@iavael
Created March 10, 2012 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iavael/2011664 to your computer and use it in GitHub Desktop.
Save iavael/2011664 to your computer and use it in GitHub Desktop.
An injector of command into tty, found by me somewhere in the internets and prettified a bit.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
void print_help(char *prog_name) {
printf("Usage: %s [-n] DEVNAME COMMAND\n", prog_name);
printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n");
printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n");
exit(1);
}
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
int main (int argc, char *argv[]) {
char* cmd;
char* eol="\n";
int i;
int fd;
int newline = 0;
int mem_len = 0;
int opt; /* Option argument container */
while((opt = getopt(argc, argv, "n")) != -1) {
switch((char)opt) {
case 'n':
newline=1;
break;
case '?':
if(isprint(optopt))
/* Unknown option */
fprintf(stderr, "Unknown option `-%c'.\n", (char)optopt);
else
/* Non-character option */
fprintf(stderr, "Unknown option character `\\x%x'.\n", (unsigned int)optopt);
return 1;
default:
abort();
}
}
if(optind >= argc) {
printf("Usage: %s [-n] DEVNAME COMMAND\n", argv[0]);
printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n");
printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n");
return(1);
}
if((fd = open(argv[optind],O_RDWR)) == -1) {
perror("open DEVICE");
return 1;
}
for ( i = optind+1; i < argc; i++ )
mem_len += strlen(argv[i])+1;
cmd = malloc(mem_len);
for ( i = optind+1; i < argc; i++ ) {
strcat(cmd, argv[i]);
if ( i != argc)
strcat(cmd, " ");
}
for (i = 0; cmd[i]; i++)
ioctl (fd, TIOCSTI, cmd+i);
if (newline != 0)
ioctl (fd, TIOCSTI, eol);
close(fd);
free(cmd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment