Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/ipwn/main.c
Views: 11766
/*1* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>2* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>3* This file is part of the Metasploit Framework.4* $Revision$5*/67#include <sys/types.h>8#include <sys/wait.h>9#include <stdlib.h>10#include <unistd.h>11#include <string.h>12#include <errno.h>13#include <stdio.h>14#include <signal.h>1516#include "cmd.h"17#include "auto.h"1819struct __cmdhandler20{21char * cmd;22void (* handler)();23unsigned int arg_process;24unsigned int arg_min;25unsigned int arg_max;26};2728struct __cmdhandler handlerlist[] =29{30{ "help", &cmd_help, 1, 0, 0 },31{ "script", &cmd_script, 1, 1, 1 },32{ "fork", &cmd_fork, 1, 0, 0 },33{ "exec", &cmd_exec, 1, 1, 14 },34{ "system", &cmd_system, 1, 1, 14 },35{ "quit", &cmd_quit, 1, 0, 0 },36{ "exit", &cmd_quit, 1, 0, 0 },3738{ "open", &cmd_open, 1, 1, 1 },39{ "lseek", &cmd_lseek, 1, 3, 3 },40{ "read", &cmd_read, 1, 1, 2 },41{ "write", &cmd_write, 1, 1, 2 },42{ "close", &cmd_close, 1, 1, 1 },43{ "dup", &cmd_dup, 1, 1, 1 },44{ "dup2", &cmd_dup2, 1, 2, 2 },4546{ "ls", &cmd_ls, 1, 0, 1 },47{ "getcwd", &cmd_getcwd, 1, 0, 0 },48{ "pwd", &cmd_getcwd, 1, 0, 0 },49{ "cd", &cmd_setcwd, 1, 0, 1 },50{ "chmod", &cmd_chmod, 1, 2, 2 },51{ "chown", &cmd_chown, 1, 2, 2 },52{ "chgrp", &cmd_chgrp, 1, 2, 2 },53{ "chdir", &cmd_chdir, 1, 1, 1 },54{ "mkdir", &cmd_mkdir, 1, 1, 2 },55{ "rmdir", &cmd_rmdir, 1, 1, 1 },56{ "rename", &cmd_rename, 1, 2, 2 },57{ "unlink", &cmd_unlink, 1, 1, 1 },58{ "chroot", &cmd_chroot, 1, 1, 1 },59{ "link", &cmd_link, 1, 2, 2 },60{ "symlink", &cmd_symlink, 1, 2, 2 },61{ "cp", &cmd_cp, 1, 2, 2 },6263{ "getid", &cmd_getid, 1, 0, 0 },64{ "setuid", &cmd_setuid, 1, 1, 1 },65{ "setgid", &cmd_setgid, 1, 1, 1 },6667{ "kill", &cmd_kill, 1, 1, 2 },68{ "getpid", &cmd_getpid, 0, 0, 0 },69{ "getppid", &cmd_getppid, 0, 0, 0 },70{ "ps", &cmd_ps, 0, 0, 0 },7172{ "time", &cmd_time, 1, 0, 0, },73{ "uname", &cmd_uname, 1, 0, 0 },74{ "hostname", &cmd_hostname, 1, 0, 1 },75{ "reboot", &cmd_reboot, 1, 0, 0 },76{ "shutdown", &cmd_shutdown, 1, 0, 0 },77{ "halt", &cmd_halt, 1, 0, 0 },7879{ "lsfd", &cmd_lsfd, 1, 0, 0 },8081{ "download", &cmd_download, 1, 2, 2 },8283{ "fchdir_breakchroot", &cmd_fchdir_breakchroot, 1, 1, 1 },84};8586#define HANDLERLIST_SIZE (sizeof(handlerlist) / sizeof(struct __cmdhandler))87#define MAX_ARGV 1588#define VERSION "0.01"8990int main(int argc, char **argv) {91char *p, *s, *b;92int sig;9394if (argc <= 1 || strcmp(argv[1], "-k") != 0) {95printf("Self-destruction mode is enabled by default, use -k to keep.\n");96printf("Removing %s...\n", argv[0]);97unlink(argv[0]);98}99100/* process any embedded commands */101if (automatic[0] != '#') {102b = s = strdup(automatic);103while ((p = strstr(s, "\n")) != NULL) {104*p = '\0';105106printf("(auto) %s\n", s);107process_input(s, strlen(s));108109s = p + 1;110}111printf("(auto) %s\n", s);112113process_input(s, strlen(s));114free(b);115}116117/* XXX: Big negative sbrk() to remove heap? */118for(sig = 1; sig <= 64; sig++)119signal(sig, SIG_IGN);120121signal(SIGCHLD, &sig_chld_waitpid);122123setvbuf(stdout, (char *)NULL, _IONBF, 0);124printf(125" __________________\n"126"< iPwn Shell v%s >\n"127" ------------------\n"128" \\ ^__^\n"129" \\ (00)\\_______\n"130" (__)\\ )\\/\\\n"131" ||----w |\n"132" || ||\n\n", VERSION);133134while(1)135{136char cmd[2048];137char cmd_bak[sizeof(cmd)];138char buf[1024];139char *cwd;140141if(getcwd(buf, sizeof(buf)) == NULL)142cwd = "(unknown)";143else144cwd = buf;145146printf("ipwn (uid=%d) (%s) > ", getuid(), cwd);147148memset(cmd, 0, sizeof(cmd));149if(fgets(cmd, sizeof(cmd), stdin) == NULL)150exit(0);151152chomp(cmd);153memcpy(cmd_bak, cmd, sizeof(cmd_bak));154155process_input(cmd, sizeof(cmd));156}157}158159160int process_input(char *cmd, int cmd_size) {161char * argv[MAX_ARGV];162int argc;163int i, hit;164char *bak;165166parse(cmd, &argc, argv);167if(argc == 0)168return(0);169170bak = strdup(cmd);171172for(hit = i = 0; i < HANDLERLIST_SIZE; i++)173{174if(strcmp(argv[0], handlerlist[i].cmd) == 0)175{176hit = 1;177178if(handlerlist[i].arg_process)179{180if(argc > handlerlist[i].arg_max+1)181printf("%s: Too many arguments\n", argv[0]);182else if(argc < handlerlist[i].arg_min+1)183printf("%s: Too few arguments\n", argv[0]);184else185handlerlist[i].handler(argc, argv);186}187else188{189handlerlist[i].handler(bak + strlen(handlerlist[i].cmd) + 1);190}191}192}193194if(hit == 0)195{196printf("%s: Unknown command.\n", argv[0]);197}198199free(bak);200201return 0;202}203204205void parse(char * str, int * const argc, char * argv[])206{207*argc = 0;208argv[0] = '\0';209210if(strlen(str) == 0)211return;212213for(argv[(*argc)++] = str; strlen(str) && *argc < MAX_ARGV; str++)214{215if(*str == ' ')216{217*str = '\0';218argv[(*argc)++] = str+1;219argv[(*argc)] = '\0';220}221if(*str == '\\')222{223switch(*(str + 1))224{225// case 'n':226// break;227default:228memmove(str, str+1, strlen(str));229break;230}231}232}233}234235void chomp(char * str)236{237if(strlen(str) > 0 && str[strlen(str) - 1] == '\n')238str[strlen(str) - 1] = '\0';239if(strlen(str) > 0 && str[strlen(str) - 1] == '\r')240str[strlen(str) - 1] = '\0';241}242243244void sig_chld_ignore(int signal)245{246return;247}248249void sig_chld_waitpid(int signal)250{251while(waitpid(-1, 0, WNOHANG) > 0);252}253254255