Files
wrt54gl_chwpwd/src/mainGUI.cpp
2021-09-01 18:34:13 +02:00

246 lines
9.1 KiB
C++
Raw Blame History

//---------------------------------------------------------------------------
#include <vcl.h>
#include <Math.hpp>
#pragma hdrstop
#include "inifiles.hpp"
#include "mainGUI.h"
#include "tsplash.h"
#include "stdio.h"
#include "THTTPdownloader.h"
#include "base64.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
#define IDM_ABOUTBOX 1
HMENU pSysMenu = GetSystemMenu(this->Handle, FALSE);
if (pSysMenu != NULL)
{
AppendMenu(pSysMenu, MF_SEPARATOR,0,0);
AppendMenu(pSysMenu, MF_STRING, IDM_ABOUTBOX, "Acerca de...");
}
WindowProc=MyWndProc;
TIniFile *ini;
ini = new TIniFile(ExtractFileDir(Application->ExeName)+"\\chpwd.ini");
StringGrid1->RowCount = 1 + ini->ReadInteger("aps","count",1);
APs = new AP[StringGrid1->RowCount];
StringGrid1->ColWidths[0] = 200;
StringGrid1->ColWidths[1] = 50;
StringGrid1->Cells[0][0] = "IP";
StringGrid1->Cells[1][0] = "Status";
TStringList *StringList = new TStringList();
int i; AnsiString aux;
for(i=1;i<StringGrid1->RowCount;i++){
StringList->Clear();
aux = ini->ReadString("aps","AP"+AnsiString(i),"0:0:0:0;user;pass;wep128pass");
ExtractStrings(TSysCharSet()<<';',TSysCharSet(), (char *)aux.data(), StringList);
if ( StringList->Count == 4 ) {
APs[i-1].IP = StringList->Strings[0];
APs[i-1].user = StringList->Strings[1];
APs[i-1].pass = StringList->Strings[2];
APs[i-1].wep = StringList->Strings[3];
}
StringGrid1->RowHeights[i] = 14;
}
delete StringList;
delete ini;
currWEP->Text = APs[0].wep;
currHexWEP->Caption = wepASCII2HEX( currWEP->Text );
generarClaveWEP->Caption = "";
generarClaveWEPClick(0);
}
//---------------------------------------------------------------------------
AnsiString __fastcall TForm1::wepASCII2HEX(AnsiString txt) {
char ascWEPkey[14], hexWEPkey[27];
sprintf(ascWEPkey,"%13s",(char *)txt.data());
BinToHex(ascWEPkey, hexWEPkey, 13);
hexWEPkey[26]='\0';
return AnsiString( hexWEPkey );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MyWndProc(Messages::TMessage &Message)
{
HMENU hMenu;
hMenu = GetSystemMenu (this->Handle, FALSE);
if (Message.Msg == WM_SYSCOMMAND)
{
switch(Message.WParamLo)
{
case IDM_ABOUTBOX:
if ( Splash==NULL ) Splash = new TSplash(NULL);
Splash->pressKeyToClose = true;
Splash->Show();
Splash->Repaint();
break;
default:
WndProc(Message);
}
} else
WndProc(Message);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
if(Splash){
delete Splash;
Splash = NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
TIniFile *ini;
ini = new TIniFile(ExtractFileDir(Application->ExeName)+"\\chpwd.ini");
for(int i=1;i<StringGrid1->RowCount;i++){
ini->WriteString("aps","AP"+AnsiString(i),APs[i-1].IP+";"+APs[i-1].user+";"+APs[i-1].pass+";"+APs[i-1].wep);
}
delete ini;
delete []APs;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
if ( ARow>0 ) {
TStringGrid *grid;
grid = (TStringGrid *)Sender;
grid->Canvas->FillRect(Rect);
Rect.right -= 2;
if ( ACol==0 ) DrawText( grid->Canvas->Handle, (char *)APs[ARow-1].IP.data(),APs[ARow-1].IP.Length(), &Rect,DT_LEFT ); else
if ( ACol==1 ) DrawText( grid->Canvas->Handle, (char *)APs[ARow-1].status.data(),APs[ARow-1].status.Length(), &Rect,DT_RIGHT );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::generarClaveWEPClick(TObject *Sender)
{
char ascWEPkey[14], hexWEPkey[27];
Randomize();
int i, r;
// char charArray[] = {' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '\'', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'};
char charArray[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', '+', 'l', '-', '.', 'm', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'o', 'p', 'q', 'r', 's', 't', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '_', '5', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '6', '7', '8', '9'};
int keyLengthInBytes = 13;
for(i=0;i<keyLengthInBytes;i++) {
r = RandomRange(0, 94); // range is 0 ~ 94
ascWEPkey[i] = charArray[r]; // creates the ASCII version of the WEP key
}
ascWEPkey[keyLengthInBytes] = '\0';
newWEP->Text = AnsiString(ascWEPkey);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::newWEPChange(TObject *Sender)
{
hexWEP->Caption = wepASCII2HEX( newWEP->Text );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if ( newWEP->Text.Length()!=13 ) {
ShowMessage("La nueva clave debe contener 13 d<>gitos.");
} else {
Button1->Caption = "Por favor, espere...";
// Propagar la clave por los routers.
for (int i=0;i<StringGrid1->RowCount;i++) {
if ( updateRouter(APs[i].IP, APs[i].user, APs[i].pass, hexWEP->Caption) ) {
APs[i].wep = newWEP->Text;
APs[i].status = "OK";
} else {
APs[i].status = "ERROR";
}
}
Button1->Caption = "Aplicar Cambios";
}
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::updateRouter(AnsiString IP, AnsiString user, AnsiString pass, AnsiString WEP)
{
TMemoryStream* out;
int error;
AnsiString Authorization, content, request, aux;
aux = user+":"+pass;
Authorization = Base64Encode((char *)aux.data());
/*
request = "GET / HTTP/1.1\r\n";
request+= "Host: "+IP+"\r\n";
request+= "User-Agent: JDsoft\r\n";
request+= "Accept: text/html;q=0.7\r\n";
request+= "Accept-Language: es-es,es\r\n";
request+= "Accept-Encoding: gzip,deflate\r\n";
request+= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
request+= "Authorization: Basic "+Authorization+"\r\n"; // YWRtaW46YWRtaW4=
request+= "Keep-Alive: 300\r\n";
request+= "Proxy-Connection: keep-alive\r\n";
request+= "\r\n";
out = new TMemoryStream();
error = HTTPdownloader->descargarHTML(IP.c_str(),request.c_str(),out);
if ( error==-1 ) return false;
*/
content = "submit_button=WL_WPATable&change_action=&submit_type=&action=Apply&security_mode_last=";
content+= "&wl_wep_last=&security_mode2=wep&wl_key=1&wl_WEP_key=&wl_wep=restricted&wl_wep_bit=128";
content+= "&wl_passphrase=&generateButton=Null";
content+= "&wl_key1="+WEP;
content+= "&wl_key2=&wl_key3=&wl_key4=";
request = "POST /apply.cgi HTTP/1.1\r\n";
request+= "Host: "+IP+"\r\n";
request+= "User-Agent: JDsoft\r\n";
request+= "Accept: text/html;q=0.7\r\n";
request+= "Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3\r\n";
request+= "Accept-Encoding: gzip,deflate\r\n";
request+= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
request+= "Keep-Alive: 300\r\n";
request+= "Proxy-Connection: keep-alive\r\n";
request+= "Referer: http://"+IP+"/WL_WPATable.asp\r\n";
request+= "Authorization: Basic "+Authorization+"\r\n";
request+= "Content-Type: application/x-www-form-urlencoded\r\n";
request+= "Content-Length: "+AnsiString(content.Length())+"\r\n\r\n";
request+= content;
/*TMemoryStream* */ out = new TMemoryStream();
error = HTTPdownloader->descargarHTML(IP.c_str(),request.c_str(),out);
if ( error!=-1 ) {
char *pos, *posEnd;
out->Position=0;
pos=StrPos((char *)out->Memory,"Settings are successful.");
if ( pos<=0 ) error=-1; else error = 0;
}
return (error!=-1);
}
//---------------------------------------------------------------------------