This is my first C++ program that I put any serious effort into. You may have seen the trick that allows you to start explorer.exe as the SYSTEM user (by running "at /interactive xx:xx explorer.exe", adding 1 minute to the current time). It takes a lot of jumping through loops, and I had nothing better to do, so I made a program that fetches the current time, adds one minute, and runs the command. I went a step further, and made it start on startup, so that one could always log in as SYSTEM if they wanted to.
However, the program forces itself to run instead of userinit.exe, afterward running userinit.exe manually using at.exe, effectively starting explorer.exe as SYSTEM. So, I made a quick .bat uninstaller to restore the registry afterward. And because of the registry modification, it will appear as a virus by most antivirus software.
Anyway, enough rambling. Basically what I'm asking is, since this is more or less my first program, did I use good coding practice? Are there any obvious ways I could improve my program?
Edit: Actually, after posting I realized something. For some reason, the time GetSystemTime() gave me was 7 hours ahead, so I was forced to take 7 hours off the current time. I don't know why it didn't click with me, but I guess GetSystemTime() gets GMT 0, and I am in mountain time (GMT -7). So basically, this program will work great for anyone in mountain time XD. So how can I get the systems timezone, in 0, -7, 5 etc. format?
Disregard the above, I just need to use GetLocalTime instead.
#define WINVER 0x0500
#include <windows.h>
#include <string>
#include <sstream>
#include <direct.h>
#include <fstream>
SYSTEMTIME st;
std::string exePath, drive;
std::string calcTime(int tMinAdd) {
std::ostringstream os, hr, mn;
int iHr, iMn;
int tRemainder;
GetLocalTime(&st);
hr << st.wHour;
mn << st.wMinute;
std::istringstream sh(hr.str());
sh >> iHr;
std::istringstream sm(mn.str());
sm >> iMn;
tRemainder = 60 - iMn;
iMn = iMn + tMinAdd;
if(iHr < 0)
iHr = 24 - (iHr * -1);
if(iMn > 59) {
iMn = iMn - (tRemainder);
iHr++;
if(iHr > 23)
iHr = iHr - 24;
}
//MessageBox(NULL, os.str(), "Time after added", MB_OK);
os << iHr << ":" << iMn;
return(os.str());
}
std::string makeUnin(std::string UninstBatPath) {
std::ofstream UninstBat;
if(UninstBatPath == "")
UninstBatPath = drive + ":\\Program Files\\sysLogin\\uninst.bat";
UninstBat.open(UninstBatPath.c_str());
UninstBat << "@echo off" << std::endl;
UninstBat << "echo Creating .reg file...." << std::endl;
UninstBat << "pause" << std::endl;
UninstBat << "echo REGEDIT4 > uninst.reg" << std::endl;
UninstBat << "echo [HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon] >> uninst.reg" << std::endl;
UninstBat << "echo \"Userinit\"=\"" << drive << ":\\\\Windows\\\\system32\\\\userinit.exe, \" >> uninst.reg" << std::endl;
UninstBat << "echo [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce] >> uninst.reg" << std::endl;
UninstBat << "echo \"DeleteRemaining\"=\"cmd /C rmdir /s /q \\\"" << drive << ":\\\\Program Files\\\\sysLogin\\\"\" >> uninst.reg" << std::endl;
UninstBat << "echo _" << std::endl;
UninstBat << "echo Done. Restoring registry now..." << std::endl;
UninstBat << "pause" << std::endl;
UninstBat << "regedit /s uninst.reg" << std::endl;
UninstBat << "echo _" << std::endl;
UninstBat << "echo You must restart for changes to take effect, all sysLogger files will then be deleted. ";
UninstBat << "If you installed ";
UninstBat << "sysLogin but never rebooted or logged in as SYSTEM, a reboot may not be neccesary. " << std::endl;
UninstBat << "set /p choice=Would you like to reboot now? (y, n)" << std::endl;
UninstBat << "if '%choice%'=='y' goto reboot" << std::endl;
UninstBat << "if '%choice%'=='Y' goto reboot" << std::endl;
UninstBat << "exit" << std::endl;
UninstBat << ":reboot" << std::endl;
UninstBat << "shutdown -r -t 0" << std::endl;
return ("\"" + UninstBatPath + "\"");
}
int install() {
std::string exeDir;
std::ifstream exe;
exePath = drive + ":\\Program Files\\sysLogin\\sysLog.exe";
exe.open(exePath.c_str(), std::ios::binary | std::ios::in);
if(!exe.is_open()) {
exe.close();
if(MessageBox(NULL, "Would you like to install sysLogger now?", "Installing....", MB_YESNO | MB_ICONQUESTION) == IDNO)
return(2);
char self[MAX_PATH];
char startup[MAX_PATH] = "";
HMODULE GetModH = GetModuleHandle(NULL);
makeUnin("");
exeDir = drive + ":\\Program Files\\sysLogin";
CreateDirectory(exeDir.c_str(), NULL);
GetModuleFileName(GetModH, self, sizeof(self));
CopyFile(self, exePath.c_str(), true);
HKEY hKey;
strcpy(startup, exePath.c_str());
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", 0, KEY_SET_VALUE, &hKey);
RegSetValueEx(hKey, "Userinit", 0, REG_SZ,(const unsigned char*)startup, sizeof(startup));
RegCloseKey(hKey);
makeUnin("");
exe.open(exePath.c_str(), std::ios::binary | std::ios::in);
if(exe.is_open()) {
exe.close();
return(1);
} else if(!exe.is_open()) {
exe.close();
return(-1);
}
}
exe.close();
return(0);
}
int main() {
drive = _getdrive() + 0x40;
int result = install();
if(result == 1) {
MessageBox(NULL, "SysLogger was successfully installed. Reboot to automatically login as SYSTEM. To uninstall, run %:\\Program Files\\sysLogin\\uninst.bat.", "Success", MB_OK | MB_ICONINFORMATION);
return 0;
} else if(result == -1) {
int response = MessageBox(NULL, "SysLogger failed to install. Run cleanup?", "Error", MB_CANCELTRYCONTINUE | MB_ICONEXCLAMATION);
if(response == IDTRYAGAIN) {
main();
return 0;
} else if(response == IDCONTINUE) {
system(makeUnin("cleanup.bat").c_str());
ShellExecute(NULL, "open", "cmd.exe", "/K del cleanup.bat", NULL, 0);
ShellExecute(NULL, "open", "cmd.exe", "/C del uninst.reg", NULL, 0);
return 0;
} else if(response == IDCANCEL)
return 0;
} else if(result == 2)
return 0;
std::string cmd = calcTime(1) + " /interactive \"" + drive + ":\\Windows\\system32\\userinit.exe\"";
drive = drive + ":\\";
ShellExecute(NULL, "open", "at", cmd.c_str(), drive.c_str(), 0);
//MessageBox(NULL, calcTime(1).c_str(), "Installing....", MB_OK);
return 0;
}