/* * Copyright (c) 2004-2005 vlad902 * Copyright (c) 2007 H D Moore * This file is part of the Metasploit Framework. * $Revision$ */ #include #include #include #include #include #include #include #include #include #include "cmd.h" void cmd_help(int argc, char * argv[]) { printf( "Available commands:\n" " help Show this help screen\n" " fork Fork off another shelldemo process\n" " exec Execute \n" " system Fork and execute on std(in/out/err)\n" " quit Exit the shell\n" "\n" " open Open a file and return the file descriptor\n" " lseek Reposition \n" " read [bytes] Read from file descriptor\n" " write [bytes] Write [bytes] (or until \"EOF\") to \n" " close Close specified file descriptor\n" " dup Duplicate and return new reference\n" " dup2 Duplicate to \n" "\n" " ls [path] Print information/contents about [path] (default: .)\n" " getcwd Get current working directory\n" " pwd Get current working directory\n" " cd Set current working directory\n" " chmod Change permissions to \n" " chown Change owner to \n" " chgrp Change group to \n" " chdir Change working directory to \n" " mkdir [permission] Create directory with [permission] (default: 755)\n" " rmdir Remove directory\n" " rename Rename to \n" " unlink Remove file\n" " chroot Change root directory to \n" " link Hard link to \n" " symlink Symbolically link to \n" " cp Copy a file from one directory to another\n" "\n" " getid Print information about [e][ug]id\n" " setuid Set UID to \n" " setgid Set GID to \n" "\n" " kill [signal] Send [signal] (default: 9)\n" " getpid Print current process ID\n" " getppid Print parent process ID\n" " ps Print process list\n" "\n" " time Display the current system time\n" " uname Get kernel information\n" " hostname [name] Print (or set) the hostname\n" " reboot Reboot the computer\n" " shutdown Shutdown the computer\n" " halt Halt the computer\n" "\n" " lsfd Show information about open file descriptors\n" "\n" " download Download a file to disk over HTTP\n" "\n" "Warning! Before using any of the following you are recommended to fork for your own safety!\n" " fchdir_breakchroot Use to attempt to break out of chroot\n"); } /* XXX: sig_chld stuff is dirty, get rid of it */ void cmd_fork(int argc, char * argv[]) { pid_t fork_pid; signal(SIGCHLD, &sig_chld_ignore); if((fork_pid = fork()) != 0) { while(waitpid(fork_pid, NULL, WNOHANG) <= 0) usleep(300); } signal(SIGCHLD, &sig_chld_waitpid); } void cmd_exec(int argc, char * argv[]) { int i; char *prog; argv++; prog = argv[0]; printf("Executing"); for(i=0; argv[i]; i++) { printf(" %s", argv[i]); } printf("\n"); execve(prog, argv, NULL); perror("execve"); } void cmd_system(int argc, char * argv[]) { pid_t fork_pid; signal(SIGCHLD, &sig_chld_ignore); if((fork_pid = fork()) != 0) { while(waitpid(fork_pid, NULL, WNOHANG) <= 0) usleep(300); } else { cmd_exec(argc, argv); exit(0); } signal(SIGCHLD, &sig_chld_waitpid); } void cmd_quit(int argc, char * argv[]) { exit(0); } void cmd_script(int argc, char * argv[]) { FILE *fd; char buff[2048]; fd = fopen(argv[1], "r"); if (fd == NULL) { perror("fopen"); return; } printf("Executing script %s\n", argv[1]); while (fgets(buff, sizeof(buff), fd)) { chomp(buff); process_input(buff, sizeof(buff)); } fclose(fd); }