Deixo-vos então um pequeno exemplo de como construir um pequeno shell em C.
Pode ser muito bem adaptado para que receba valores da linha de comandos.
Acho que não valerá apena estar a explicar linha por linha, visto que é uma programação muito básica, se houver uma dúvida digam.
Source:
/*################################################## ## SIMPLES SHELL EM C / WINDOWS ## ## ## ## @2011 BY PEDRO TAVARES ## ## ## ## ## ##################################################*/ /* DECLARACAO DE BLIBLIOTECAS */ #include <stdio.h> #include <string.h> #include <stdlib.h> /* PROTOTIPOS DE FUNCOES */ void prompt(); void _parse(); void wordpad(); void ler(); /* FUNCAO <ABRIR/MOSTRAR> FICHEIRO */ void ler(){ FILE *F; char nome[20]; /* DECLARACAO ESTATICA DE VARIAVEIS */ char buff[200]; printf("nn.::::::::::::::: MOSTRA FICHEIRO :::::::::::::::.nn"); printf("nQual o nome do ficheiro que deseja abrir?"); scanf("%s",&nome); F=fopen(nome,"r"); /* ABERTURA DO FICHEIRO */ if(F==NULL){ printf("Ficheiro Invalido!!nn"); return; } while(!feof(F)){ fgets(buff,200,F); /* LISTAGEM */ printf("%s",buff); } fclose(F); /* FECHO DO FICHEIRO */ return; } /* FUNCAO <ESCREVER> FICHEIRO */ void wordpad(){ FILE *F; /* DECLARACAO ESTATICA DE VARIAVEIS */ char nome[20]; char buff[200]; printf("Qual o nome do novo ficheiro?"); scanf("%s",&nome); F=fopen(nome,"a"); /* ABERTURA DO FICHEIRO */ while(strcmp(buff,"exit")!=0){ scanf("%s",buff); fputs(buff,F); /* ENVIAR DADOS PARA O FICHEIRO */ fputs("n",F); } fclose(F); /* FECHO DO FICHEIRO */ return ; } /* FUNCAO PARSING <ANALISA STRING> */ void _parse(char *buff){ int i; for(i=0;i<strlen(buff);i++){ /* TESTAR E VERIFICAR AS CONDICOES E CHAMAR OPERACAO */ if(strcmp(&buff[i],"sair")==0){ exit(0); } if(strcmp(&buff[i],"ls")==0){ system("dir"); } if(strcmp(&buff[i],"wordpad")==0){ wordpad(); } if(strcmp(&buff[i],"ler")==0){ ler(); } } return ; } /* FUNCAO PROMPT :> */ void prompt(){ printf(":>"); return; } /* FUNCAO MAIN */ int main (){ char buff[200]; printf("Bem vindo a minha SHELL o_On"); printf("As opcoes implementadas sao: dir ; ler ; wordpad ; sairnnn"); while(1){ prompt(); scanf("%s",buff); _parse(buff); } return 0; }
Pedro Tavares is a professional in the field of information security working as an Ethical Hacker/Pentester, Malware Researcher and also a Security Evangelist. He is also a founding member at CSIRT.UBI and Editor-in-Chief of the security computer blog seguranca-informatica.pt.
In recent years he has invested in the field of information security, exploring and analyzing a wide range of topics, such as pentesting (Kali Linux), malware, exploitation, hacking, IoT and security in Active Directory networks. He is also Freelance Writer (Infosec. Resources Institute and Cyber Defense Magazine) and developer of the 0xSI_f33d – a feed that compiles phishing and malware campaigns targeting Portuguese citizens.
Read more here.