You are on page 1of 2

Page 1 of 2

CO1508 Computer Systems & Security – Week 10


Building your little harmless virus in C++ – Part 1 Registry

Summary
In this lab, students are expected to understand and build a C++ programme that will infect
Windows Registry to start every time the machine starts and make the mouse go crazy.

I left some errors and/or bugs on purpose to encourage students to find fixes and solutions
themselves. This is essential to teach them debugging and for developing their assignment
later. Below, you’ll find the corrections that you can share with students after they give up.
Please encourage them to find solutions themselves.

Activities

1. Windows Registry – Handle to Key (HKEY)

This is the correct code. The errors were related to data types that are passed to
RegSetValueEx function. Students should read and understand that function from
winreg.h online page.

Please don’t share the solution until at least 30 minutes of the lab session is passed.

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
HKEY RunKey;
LPCTSTR valueP = TEXT("CO1508");
LPCTSTR data = TEXT("C:\\WINDOWS\\system32\\mspaint.exe");

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 2 of 2

if (RegOpenKey(HKEY_CURRENT_USER,
TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\RUN"), &RunKey) !=
ERROR_SUCCESS)
{
cout << "Unable to open registry key. Exit.\n";
return 0;
}

if (RegSetValueEx(RunKey, valueP, 0, REG_SZ, (LPBYTE)data,


lstrlen(data)*sizeof(TCHAR)) != ERROR_SUCCESS)
{
RegCloseKey(RunKey);
cout << "Unable to set the registry value. Exit.\n";
return 0;
}
else
{
RegCloseKey(RunKey);
cout << "Registry value is successfully set!\n";
MessageBox(NULL, L"You're infected :-) I dare you to restart
your machine!", L"Infection", MB_OKCANCEL);
}
return 0;
}

2. The Assignment – Start now with these tasks

Here, students should start thinking about how to use the knowledge here for their
assignment. They should start working on these tasks to use in their assignment. Please
remind them that they should understand every line of code they’re writing.

h script solution

CO1508 Computer Systems and Security, UCLAN – 2019-2020

You might also like