Automatizando configuração e conexão wireless

Publicado por Perfil removido (última atualização em 08/07/2016)

[ Hits: 2.840 ]

Download 2wireless.c




Código simples, com algumas funções e que visa tornar ainda mais fácil a configuração do wpa_supplicant.

Este programa lê alguns dados fornecidos pelo usuário como ssid/usuário/login/interface etc.

Escreve alguns arquivos e faz a conexão com base nestes dados; obtendo o endereço por dhcp.

Suporte para as encriptações Open, WEP e WPA/WPA2.

  



Esconder código-fonte

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define MAX 100
#define MIN 50
FILE *wireless_conf;
FILE *custom_path_out;
FILE *wpa_bin;
char custom_path_outputopen[MIN] = "/etc/wpa_supplicant/custom_open";
char custom_path_outputwep[MIN] = "/etc/wpa_supplicant/custom_wep";
char custom_path_outputwpa[MIN] = "/etc/wpa_supplicant/custom_wpa";
char default_path_wpa[MIN] = "/etc/wpa_supplicant/default_wpa.conf";
char default_path_wep[MIN] = "/etc/wpa_supplicant/default_wep.conf";
char default_path_open[MIN] = "/etc/wpa_supplicant/default_open.conf";
char wpa_supplicant[MIN] = "/usr/bin/wpa_supplicant";

int associate_open(){
   char ssid[MIN];
   char custom_path[MAX];
   char op_path[1];

   printf("Network ssid: ");
   __fpurge(stdin);
   if(scanf("%[a-zA-Z0-9/,.|_-]%*[^\n]%*1[\n]",ssid)==1 && strlen(ssid)>1)
    {
        __fpurge(stdin);
        printf("default/custom path file (d/c): ");
   if(scanf("%[dc]",op_path)==1 && strlen(op_path)==1)
    {
        if(op_path[0] == 'c')
   {
      printf("Path (with name file): ");
      __fpurge(stdin);
      scanf("%s",custom_path);
       if((wireless_conf = fopen(custom_path,"w"))==NULL)
       {
         fprintf(stderr,"ERROR OPEN custom path: %s\n",strerror(errno));
         exit(1);
      }
      else
      {
          fprintf(wireless_conf,"network={\nssid=\"%s\"\nkey_mgmt=NONE\n}\n",ssid);
          printf("Wireless conf file at: %s\n",custom_path);
      if((custom_path_out = fopen(custom_path_outputopen,"w"))==NULL)
        {
            fprintf(stderr,"ERROR failed to create output file: %s\n",strerror(errno));
            exit(1);
        }
        else
        {
            fprintf(custom_path_out,"%s\n",custom_path);
       }
    }
}
    if(op_path[0] == 'd')
    {
      if((wireless_conf = fopen(default_path_open,"w"))==NULL)
       {
         fprintf(stderr,"ERROR OPEN default path: %s\n",strerror(errno));
         exit(1);
      }
      else
      {
          fprintf(wireless_conf,"network={\nssid=\"%s\"\nkey_mgmt=NONE\n}\n",ssid);
          printf("Wireless conf file at: %s\n",default_path_open);
       }
   }
}
else
{
    printf("invalid option.\n");
}}
else
{
    printf("invalid ssid.\n");
}
fcloseall();
}
int associate_wep(){
   char ssid[MIN];
   char pass[MIN];
   char cmd[MAX];
   char op_path[1];
   char custom_path[MAX];

   printf("Network ssid: ");
   __fpurge(stdin);
   if(scanf("%[a-zA-Z0-9/,.|_-]%*[^\n]%*1[\n]",ssid)==1 && strlen(ssid)>1)
    {
        __fpurge(stdin);
        printf("Network password: ");
   if(scanf("%[a-zA-Z0-9/,.|_-]%*[^\n]%*1[\n]",pass)==1 && strlen(pass)>1)
    {
        __fpurge(stdin);
        printf("default/custom path file (d/c): ");
   if(scanf("%[dc]",op_path)==1 && strlen(op_path)==1)
    {
        if(op_path[0] == 'c')
   {
      printf("Path (with name file): ");
       scanf("%s",custom_path);
      if((wireless_conf = fopen(custom_path,"w"))==NULL)
        {
            fprintf(stderr,"ERROR WEP custom path: %s\n",strerror(errno));
            exit(1);
        }
        else
        {
            fprintf(wireless_conf,"network={\nssid=\"%s\"\nkey_mgmt=NONE\nwep_key0=\"%s\"\n}\n",ssid,pass);
            printf("Wireless conf file at: %s\n",custom_path);
      if((custom_path_out = fopen(custom_path_outputwep,"w"))==NULL)
        {
            fprintf(stderr,"ERROR failed to create output file: %s\n",strerror(errno));
            exit(1);
        }
        else
        {
            fprintf(custom_path_out,"%s\n",custom_path);
        }
    }
}
   if(op_path[0] == 'd')
   {
      if((wireless_conf = fopen(default_path_wep,"w"))==NULL)
        {
            fprintf(stderr,"ERROR WEP default path: %s\n",strerror(errno));
            exit(1);
        }
        else
        {
            fprintf(wireless_conf,"network={\nssid=\"%s\"\nkey_mgmt=NONE\nwep_key0=\"%s\"\n}\n",ssid,pass);
            printf("Wireless default file at: %s\n",default_path_wep);
        }
    }
}}
else
{
    printf("invalid password.\n");
}}
else
{
    printf("invalid ssid.\n");
}
fcloseall();
}
int associate_wpa(){
   char ssid[MIN];
   char pass[MIN];
   char cmd[MAX];
   char op_path[1];
   char custom_path[MAX];

   printf("Network ssid: ");
   __fpurge(stdin);
   if(scanf("%[a-zA-Z0-9/,.|_-]%*[^\n]%*1[\n]",ssid)==1 && strlen(ssid)>1)
    {
        printf("Network password: ");
        __fpurge(stdin);
   if(scanf("%[a-zA-Z0-9/,.|_-]%*[^\n]%*1[\n]",pass)==1 && strlen(pass)>1)
    {
        printf("default/custom path file (d/c): ");
        __fpurge(stdin);
   if(scanf("%[dc]",op_path)==1 && strlen(op_path)==1)
    {
        if(op_path[0] == 'c')
   {
      printf("Path (with name file): ");
       scanf("%s",custom_path);

       if((wireless_conf = fopen(custom_path,"w"))==NULL)
       {
         fprintf(stderr,"ERROR WPA custom path: %s\n",strerror(errno));
         exit(1);
      }
      else
      {
          sprintf(cmd,"wpa_passphrase %s %s > %s",ssid,pass,custom_path);
          system(cmd);
          printf("Wireless conf file at: %s\n",custom_path);
               if((custom_path_out = fopen(custom_path_outputwpa,"w"))==NULL)
                    {
                        fprintf(stderr,"ERROR failed to create output file: %s\n",strerror(errno));
                        exit(1);
                    }
                    else
                    {
                        fprintf(custom_path_out,"%s\n",custom_path);
                    }
              }
        }
   if(op_path[0] == 'd')
   {
      if((wireless_conf = fopen(default_path_wpa,"w"))==NULL)
       {
         fprintf(stderr,"ERROR WPA default path: %s\n",strerror(errno));
         exit(1);
      }
      else
      {
          sprintf(cmd,"wpa_passphrase %s %s > %s",ssid,pass,default_path_wpa);
          system(cmd);
          printf("Wireless default file at: %s\n",default_path_wpa);
           }
          }
}
else
{
    printf("invalid option.\n");
}}
else
{
    printf("invalid password.\n");
}}
else
{
    printf("invalid ssid.\n");
}
fcloseall();
}
int connect_open(){
   char op_path[1];
   char custom_path[MAX];
   char iface[MIN];
   char cmd[MAX];
   char cmd2[MIN];

   if((wpa_bin = fopen(wpa_supplicant,"rb"))==NULL)
   {
      fprintf(stderr,"ERROR install wpa_supplicant package: %s",strerror(errno));
   }
   else
   {
      printf("Connect using default/custom file (d/c): ");
      __fpurge(stdin);
      if(scanf("%[dc]",op_path)==1 && strlen(op_path)==1)
        {
            if(op_path[0] == 'd')
      {
         printf("Interface: ");
         scanf("%s",iface);
         system("killall wpa_supplicant dhcpcd");
         sprintf(cmd,"wpa_supplicant -B -c %s -i %s\n",default_path_open,iface);
         system(cmd);
         sprintf(cmd2,"dhcpcd %s",iface);
         if((system(cmd2))==0)
         {
             printf("\nSucessfully connected!\n");
          }
          else
            {
                printf("\nFailed to make connection.\n");
            }
       }
       if(op_path[0] == 'c')
       {
            if((custom_path_out = fopen(custom_path_outputopen,"r"))==NULL)
            {
                fprintf(stderr,"ERROR failed to read output file: %s\n");
            }
            else
            {
                fscanf(custom_path_out,"%s",custom_path);
                printf("Interface: ");
                scanf("%s",iface);
                system("killall wpa_supplicant dhcpcd");
                sprintf(cmd,"wpa_supplicant -B -c %s -i %s\n",custom_path,iface);
                system(cmd);
                sprintf(cmd2,"dhcpcd %s",iface);
                if((system(cmd2))==0)
                    {
                        printf("\nSucessfully connected!\n");
                    }
                    else
                    {
                        printf("\nFailed to make connection.\n");
                    }
                }
            }
        }
        else
        {
            printf("invalid option.\n");
        }
    }
    fcloseall();
    }
int connect_wep(){
   char op_path[1];
   char custom_path[MAX];
   char iface[MIN];
   char cmd[MAX];
   char cmd2[MIN];

   if((wpa_bin = fopen(wpa_supplicant,"rb"))==NULL)
   {
      fprintf(stderr,"ERROR install wpa_supplicant package: %s",strerror(errno));
   }
   else
   {
      printf("Connect using default/custom file (d/c): ");
                __fpurge(stdin);
      if(scanf("%[dc]",op_path)==1 && strlen(op_path)==1)
      {
          if(op_path[0] == 'd')
      {
         printf("Interface: ");
         scanf("%s",iface);
         system("killall wpa_supplicant dhcpcd");
         sprintf(cmd,"wpa_supplicant -B -c %s -i %s\n",default_path_wep,iface);
         system(cmd);
         sprintf(cmd2,"dhcpcd %s",iface);
         if((system(cmd2))==0)
         {
             printf("\nSucessfully connected!\n");
          }
          else
                    {
                         printf("\nFailed to make connection.\n");
                    }
       }
       if(op_path[0] == 'c')
       {
                if((custom_path_out = fopen(custom_path_outputwep,"r"))==NULL)
            {
                fprintf(stderr,"ERROR failed to read output file: %s\n",strerror(errno));
            }
            else
            {
                fscanf(custom_path_out,"%s",custom_path);
                printf("Interface: ");
                scanf("%s",iface);
                system("killall wpa_supplicant dhcpcd");
                sprintf(cmd,"wpa_supplicant -B -c %s -i %s\n",custom_path,iface);
                system(cmd);
                sprintf(cmd2,"dhcpcd %s",iface);
                if((system(cmd2))==0)
                    {
                        printf("\nSucessfully connected!\n");
                    }
                    else
                    {
                        printf("\nFailed to make connection.\n");
                    }
                }
            }
        }
        else
        {
            printf("invalid option.\n");
        }
    }
    fcloseall();
    }
int connect_wpa(){
   char op_path[1];
   char custom_path[MAX];
   char iface[MIN];
   char cmd[MAX];
   char cmd2[MIN];

   if((wpa_bin = fopen(wpa_supplicant,"rb")) == NULL)
   {
      fprintf(stderr,"ERROR install wpa_supplicant package: %s",strerror(errno));
   }
   else
   {
       printf("Connect using default/custom file (d/c): ");
       __fpurge(stdin);
    if(scanf("%[dc]",op_path)==1 && strlen(op_path)==1)
    {
        if(op_path[0] == 'd')
    {
        printf("Interface: ");
        scanf("%s",iface);
   system("killall wpa_supplicant dhcpcd");
   sprintf(cmd,"wpa_supplicant -B -c %s -i %s\n",default_path_wpa,iface);
   system(cmd);
   sprintf(cmd2,"dhcpcd %s",iface);
   if((system(cmd2))==0)
   {
       printf("\nSucessfully connected!\n");
   }
   else
        {
             printf("\nFailed to make connection.\n");
        }
   }
   if(op_path[0] == 'c')
   {
            if((custom_path_out = fopen(custom_path_outputwpa,"r"))==NULL)
            {
                fprintf(stderr,"ERROR failed to read output file: %s\n",strerror(errno));
            }
            else
            {
                fscanf(custom_path_out,"%s",custom_path);
                printf("Interface: ");
                scanf("%s",iface);
                system("killall wpa_supplicant dhcpcd");
                sprintf(cmd,"wpa_supplicant -B -c %s -i %s\n",custom_path,iface);
                system(cmd);
                sprintf(cmd2,"dhcpcd %s",iface);
                if((system(cmd2))==0)
                    {
                        printf("\nSucessfully connected!\n");
                    }
                    else
                    {
                        printf("\nFailed to make connection.\n");
                    }
                }
            }
        }
        else
        {
            printf("invalid option.\n");
        }
    }
    fcloseall();
    }
int verify_user(){
    FILE *id_out;
    char idp[15] = "/var/tmp/id";
    char id[MIN];
    int contador = 0;
    char aux;

    system("id > /var/tmp/id");
    if((id_out = fopen(idp,"r"))==NULL)
    {
        fprintf(stderr,"ERROR failed to read id: %s",strerror(errno));
    }
    else
    {
        fscanf(id_out,"%s",id);
        for(aux=0;id!='\0';aux++)
        {
            if(id[4] == '0')
            {
                system("rm /var/tmp/id");
                break;
            }
            else
            {
                printf("%s\n",id);
                printf("Launch as root!\n");
                system("rm /var/tmp/id");
                exit(1);
            }
        }
    }
    fclose(id_out);
    }
int main(void){
        verify_user();
   int menu = 0;

   printf("\n0 (Exit) \n1 Open Wireless     | 2 Connect Open\n3 WEP Wireless      | 4 Connect WEP\n");
   printf("5 WPA/WPA2 Wireless | 6 Connect WPA/WPA2\n7 (About)\n\n");
   while(menu >= 0)
   {
      printf("[2wireless]# ");
      scanf("%d",&menu);
      switch(menu)
      {
         case 0:
             exit(0);
         case 1:
             associate_open();
             break;
         case 2:
             connect_open();
             break;
         case 3:
             associate_wep();
             break;
         case 4:
             connect_wep();
             break;
         case 5:
             associate_wpa();
             break;
         case 6:
             connect_wpa();
             break;
         case 7:
             printf("\nThis is a small C code to connect wireless device to the internet through ");
             printf("Open, WEP and WPA/WPA2 encryption.\n");
             printf("2wireless Developed by Cristhoffer Corrêa.\n");
             break;
         default:
         continue;
      }
   }
   return 0;
   }

Scripts recomendados

Converter Dólar em Real

Servidor de arquivos (Cliente/servidor)

Microcad

Configurando DNS (ppp)

Portscan simples em C


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts