USB Autorun logging C++ code

A friend of mine thinks that someone he lives together with is using his external HDD, when he isn’t around. So he asked me to develop a piece of software that could log whenever his external HDD was plugged into an computer.


Autorun.cfg / Autorun.inf

So basically a lot of USB devices have an autorun.cfg or autorun.inf which runs whenever the device is plugged into the computer.

These Autorun files tells the computer various information about the device, such as name, icon and can even run other files.


After developing the piece of software, I found out the commands for auto-running software from an USB on Windows was disabled in an update. In my friends case this isn’t a good thing, but for computers in general, it’s great, since its one huge security hole.


Imagine. Before this update, virusses could easily be distributed through USB devices.

Scary thought, yes? I think it is.


using namespace std;

int main(int argc, char** argv) {
    //Get pc name
    string computerName = getenv("COMPUTERNAME") + string("\n\n");
    //Get epoch time and translate to readable date/time    
    time_t raw_time;
    struct tm * timeinfo;
    time (&raw_time);
    timeinfo = localtime(&raw_time);

    //open or create file and write to it
    fstream myFile;"log.TXT", fstream::in | fstream::out | fstream::app);
    myFile << asctime(timeinfo) + computerName;
    //Exit program
    return 0;

Basically this piece of C++ code creates a string with the computer name and two linebreakes

Then it gets Unix Epoch time which is seconds since 00:00:00 UTC on January 1st 1970

We then create or open, depending on whether the file already exist and tell it to make input, output and append to the end of it.

Then I write a human readable version of Unix Epoch time, and the computer name to the file and then close the file and exit the program.