1 /*
2 ** cmdline.c for elfsh
3 **
4 ** Started on  Fri Nov  2 15:21:56 2001 mayhem
5 ** Last update Wed Jun 25 08:51:39 2003 mayhem
6 */
7 #include "elfsh.h"
8 
9 
10 /* Bad parameter handler */
vm_badparam(char * str)11 void	vm_badparam(char *str)
12 {
13   fprintf(stderr,
14 	  "\n [!] Bad parameters for command %s .::. "
15 	  "type 'help' for command list \n\n", str);
16 }
17 
18 /* Unknow command handler */
vm_unknown(char * str)19 void	vm_unknown(char *str)
20 {
21   fprintf(stderr, "\n [!] Unknown command %s .::. "
22 	  "type 'help' for command list \n\n", str);
23 }
24 
25 /* Open the script file */
vm_openscript(char * name,char * av0)26 int	vm_openscript(char *name, char *av0)
27 {
28   int	fd;
29 
30   /* Open script file */
31   XOPEN(fd, name, O_RDONLY, 0, -1);
32   if (dup2(fd, 0) < 0)
33     return (-1);
34   return (0);
35 }
36 
37 /* Print the banner */
vm_print_banner()38 void		vm_print_banner()
39 {
40   printf("\n\n\t Welcome to The ELF shell %s .::. \n\n", ELFSH_VERSION);
41   printf("\t .::. This software is under the General Public License \n"
42 	 "\t .::. Please visit http://www.gnu.org to know about "
43 	 "Free Software \n\n\n");
44 }
45 
46 /* Print the Unknown buffer */
vm_build_unknown(char * buf,const char * str,u_long type)47 char		*vm_build_unknown(char *buf, const char *str, u_long type)
48 {
49   snprintf(buf, ELFSH_MEANING, "%s %08X", str, (u_int) type);
50   return (buf);
51 }
52 
53 
54 /* Retreive a file object giving its unique ID */
vm_getfile(u_int index)55 elfshobj_t	*vm_getfile(u_int index)
56 {
57   elfshobj_t	*cur;
58 
59   for (cur = world.list; cur; cur = cur->next)
60     if (cur->id == index)
61       return (cur);
62   return (NULL);
63 }
64 
65 /* Retreive a module object giving its unique ID */
vm_getmod(u_int index)66 elfshmod_t	*vm_getmod(u_int index)
67 {
68   elfshmod_t	*cur;
69 
70   for (cur = world.modlist; cur; cur = cur->next)
71     if (cur->id == index)
72       return (cur);
73   return (NULL);
74 }
75 
76 /* Print error depending on the state of the machine */
vm_doerror(void (* fct)(char * str),char * str)77 int		vm_doerror(void (*fct)(char *str), char *str)
78 {
79   if (world.state.vm_mode != ELFSH_VMSTATE_CMDLINE)
80     fct(str);
81   else
82     {
83       cmd_help();
84       exit(-1);
85     }
86   return (-1);
87 }
88 
89 /* Mark the current object SHT to be removed on savnig */
cmd_shtrm()90 int             cmd_shtrm()
91 {
92   world.current->shtrm = 1;
93   if (!world.state.vm_quiet)
94     puts(" [*] SHT marked as removed, you need to save\n");
95   return (0);
96 }
97 
98 /* Mark the file as stripped */
cmd_strip()99 int		cmd_strip()
100 {
101   world.current->strip = 1;
102   if (!world.state.vm_quiet)
103     puts(" [*] Object marked as stripped, you need to save\n");
104   return (0);
105 }
106 
107 /* Mark SHT as removed and file as stripped */
cmd_sstrip()108 int		cmd_sstrip()
109 {
110   world.current->strip = 1;
111   world.current->shtrm = 1;
112   if (!world.state.vm_quiet)
113     puts(" [*] Object marked as sstripped, you need to save\n");
114   return (0);
115 }
116 
117 /* Change the VM state as QUIET */
cmd_quiet()118 int		cmd_quiet()
119 {
120   world.state.vm_quiet = 1;
121   return (0);
122 }
123 
124 /* Change the VM state as VERBOSE (default) */
cmd_verb()125 int		cmd_verb()
126 {
127   printf(" [*] Switched to verbose mode \n\n");
128   world.state.vm_quiet = 0;
129   return (0);
130 }
131 
132 /* Useful when you have only one terminal */
cmd_meta()133 int	cmd_meta()
134 {
135   if (!world.state.vm_quiet)
136     printf(" [*] You are still in elfsh, exit bash "
137 	   "to get back to the regular prompt \n\n");
138   return (system(ELFSH_SHELL));
139 }
140 
141 /* Useful for debugging */
cmd_stop()142 int	cmd_stop()
143 {
144   return (raise(SIGSTOP));
145 }
146 
147 /* Useful to differentiate 0 and a string */
vm_isnbr(char * string)148 int	vm_isnbr(char *string)
149 {
150   size_t len = strlen(string);
151   size_t ii;
152 
153   for (ii=0; ii < len; ii++)
154     if (!isdigit((int) string[ii]))
155       return (0);
156   return (1);
157 }
158 
159 
160 
161