CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/ipwn/cmd_base.c
Views: 11766
1
/*
2
* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>
3
* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>
4
* This file is part of the Metasploit Framework.
5
* $Revision$
6
*/
7
8
#include <sys/types.h>
9
#include <sys/wait.h>
10
#include <stdlib.h>
11
#include <unistd.h>
12
#include <string.h>
13
#include <errno.h>
14
#include <signal.h>
15
#include <stdio.h>
16
#include <string.h>
17
18
#include "cmd.h"
19
20
21
void cmd_help(int argc, char * argv[])
22
{
23
printf( "Available commands:\n"
24
" help Show this help screen\n"
25
" fork Fork off another shelldemo process\n"
26
" exec <cmd> Execute <cmd>\n"
27
" system <cmd> Fork and execute <cmd> on std(in/out/err)\n"
28
" quit Exit the shell\n"
29
30
"\n"
31
" open <path> Open a file and return the file descriptor\n"
32
" lseek <fd> <offset> <whence> Reposition <fd>\n"
33
" read <fd> [bytes] Read <bytes> from file descriptor\n"
34
" write <fd> [bytes] Write [bytes] (or until \"EOF\") to <fd>\n"
35
" close <fd> Close specified file descriptor\n"
36
" dup <old_fd> Duplicate <old_fd> and return new reference\n"
37
" dup2 <old_fd> <new_fd> Duplicate <old_fd> to <new_fd>\n"
38
39
"\n"
40
" ls [path] Print information/contents about [path] (default: .)\n"
41
" getcwd Get current working directory\n"
42
" pwd Get current working directory\n"
43
" cd Set current working directory\n"
44
" chmod <permission> <path> Change <path> permissions to <permission>\n"
45
" chown <user> <path> Change <path> owner to <user>\n"
46
" chgrp <group> <path> Change <path> group to <group>\n"
47
" chdir <path> Change working directory to <path>\n"
48
" mkdir <path> [permission] Create <path> directory with [permission] (default: 755)\n"
49
" rmdir <path> Remove <path> directory\n"
50
" rename <old_file> <new_file> Rename <old_file> to <new_file>\n"
51
" unlink <path> Remove <path> file\n"
52
" chroot <path> Change root directory to <path>\n"
53
" link <file> <reference> Hard link <reference> to <file>\n"
54
" symlink <file> <reference> Symbolically link <reference> to <file>\n"
55
" cp <file> <file> Copy a file from one directory to another\n"
56
57
"\n"
58
" getid Print information about [e][ug]id\n"
59
" setuid <uid> Set UID to <uid>\n"
60
" setgid <gid> Set GID to <gid>\n"
61
62
"\n"
63
" kill <pid> [signal] Send <pid> [signal] (default: 9)\n"
64
" getpid Print current process ID\n"
65
" getppid Print parent process ID\n"
66
" ps Print process list\n"
67
68
"\n"
69
" time Display the current system time\n"
70
" uname Get kernel information\n"
71
" hostname [name] Print (or set) the hostname\n"
72
" reboot Reboot the computer\n"
73
" shutdown Shutdown the computer\n"
74
" halt Halt the computer\n"
75
76
"\n"
77
" lsfd Show information about open file descriptors\n"
78
79
"\n"
80
" download <url> <file> Download a file to disk over HTTP\n"
81
82
"\n"
83
"Warning! Before using any of the following you are recommended to fork for your own safety!\n"
84
" fchdir_breakchroot <temp_dir> Use <temp_dir> to attempt to break out of chroot\n");
85
}
86
87
88
/* XXX: sig_chld stuff is dirty, get rid of it */
89
void cmd_fork(int argc, char * argv[])
90
{
91
pid_t fork_pid;
92
93
signal(SIGCHLD, &sig_chld_ignore);
94
if((fork_pid = fork()) != 0)
95
{
96
while(waitpid(fork_pid, NULL, WNOHANG) <= 0)
97
usleep(300);
98
}
99
signal(SIGCHLD, &sig_chld_waitpid);
100
}
101
102
void cmd_exec(int argc, char * argv[])
103
{
104
int i;
105
char *prog;
106
107
argv++;
108
109
prog = argv[0];
110
111
printf("Executing");
112
for(i=0; argv[i]; i++) {
113
printf(" %s", argv[i]);
114
}
115
printf("\n");
116
117
execve(prog, argv, NULL);
118
perror("execve");
119
}
120
121
void cmd_system(int argc, char * argv[])
122
{
123
pid_t fork_pid;
124
125
signal(SIGCHLD, &sig_chld_ignore);
126
if((fork_pid = fork()) != 0)
127
{
128
while(waitpid(fork_pid, NULL, WNOHANG) <= 0)
129
usleep(300);
130
} else {
131
cmd_exec(argc, argv);
132
exit(0);
133
}
134
signal(SIGCHLD, &sig_chld_waitpid);
135
}
136
137
void cmd_quit(int argc, char * argv[])
138
{
139
exit(0);
140
}
141
142
143
void cmd_script(int argc, char * argv[])
144
{
145
FILE *fd;
146
char buff[2048];
147
148
fd = fopen(argv[1], "r");
149
if (fd == NULL) {
150
perror("fopen");
151
return;
152
}
153
154
printf("Executing script %s\n", argv[1]);
155
while (fgets(buff, sizeof(buff), fd)) {
156
chomp(buff);
157
process_input(buff, sizeof(buff));
158
}
159
160
fclose(fd);
161
}
162
163
164