0% found this document useful (0 votes)
18 views8 pages

#Include #Include Using Namespace STD Class Bruteforce (Int Password Public: Bruteforce (Int P) : Password (P) Int Simulate (Int Attempts 0

Uploaded by

x996km79z2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

#Include #Include Using Namespace STD Class Bruteforce (Int Password Public: Bruteforce (Int P) : Password (P) Int Simulate (Int Attempts 0

Uploaded by

x996km79z2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Cybersecurity C++ Problems (English)

1. Nested Loops: “Simulated Brute Force Attack”


Problem:
A web application is undergoing a security test to verify its password-checking system.
Assume that the system's password is only 2 digits long and ranges from 00 to 99. A
security tester simulates a brute-force attack — systematically trying all possible
combinations — to discover the password.
Input:
A single integer p (0 ≤ p ≤ 99) — the secret password.
Output:
The number of attempts needed to find the password.
Test Cases:
Input:
0
Output:
1
Input:
1
Output:
2
Input:
50
Output:
51
Input:
99
Output:
100
Input:
42
Output:
43

SOLUTION:

#include <iostream>
#include <iomanip>
using namespace std;

class BruteForce{
int password;
public:
BruteForce(int p) : password(p) {}

int simulate() {
int attempts = 0;
for (int i = 0; i <= 9; ++i) {
for (int j = 0; j <= 9; ++j) {
int guess = i * 10 + j;
++attempts;
if (guess == password) return attempts;
}
}
return attempts;
}
};

int main() {
int p;
cin >> p;
BruteForce sim(p);
cout << sim.simulate() << endl;
return 0;
}

2. Using Strings: “Header Injection Detector”


Problem:
A server is performing a security check on incoming HTTP messages. If a message
contains the sequence \r\n, it may be a sign of an HTTP Header Injection attack. Such
attacks can allow users to inject malicious headers into the request, posing a threat to the
system.
Input:
A string s (length ≤ 1000)
Output:
Injection detected OR Safe
Test Cases:
Input:
GET /index.html HTTP/1.1\r\nHost: example.com
Output:
Injection detected
Input:
GET /contact HTTP/1.1
Output:
Safe
Input:
Hello\r\nWorld
Output:
Injection detected
Input:
Simple text
Output:
Safe
Input:
\r\n\r\n
Output:
Injection detected
SOLUTION:

#include <iostream>
#include <string>
using namespace std;

class Checker {
string content;
public:
Checker(string s) : content(s) {}

string check() {
if (content.find("\\r\\n") != string::npos)
return "Injection detected";
return "Safe";
}
};

int main() {
string s;
getline(cin, s);
Checker ic(s);
cout << ic.check() << endl;
return 0;
}

3. Vectors: “Port Scanner Result Parser”


Problem:
A port scanner has checked the status of multiple ports in a system. Each port is either
open (1) or closed (0). The results are provided as n integers. The goal is to count how
many ports are open.
Input:
First line: n (1 ≤ n ≤ 100)
Second line: n space-separated integers (each either 0 or 1)
Output:
The number of open ports.
Test Cases:
Input:
5
10011
Output:
3
Input:
3
000
Output:
0
Input:
4
1111
Output:
4
Input:
1
0
Output:
0
Input:
6
101010
Output:
3

SOLUTION:

#include <iostream>
#include <vector>
using namespace std;

class PortScanner {
vector<int> ports;
public:
PortScanner(vector<int> p) : ports(p) {}

int Openolan() {
int count = 0;
for (int p : ports)
if (p == 1) count++;
return count;
}
};

int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
PortScanner ps(v);
cout << ps.Openolan() << endl;
return 0;
}

4. Search Algorithms: “Find XSS Payload in Logs”


Problem:
A web application uses a Web Application Firewall (WAF) to inspect incoming HTTP
requests and detect malicious code — especially XSS (Cross-Site Scripting) attacks. The
WAF analyzes n incoming request lines and must determine the line number of the first
occurrence of the <script> tag, if any.
Input:
First line: n (1 ≤ n ≤ 100)
Next n lines: each a string representing a request
Output:
The 1-based line number of the first line containing <script>, or -1 if not found
Test Cases:
Input:
3
GET /
POST /search
<script>alert(1)</script>
Output:
3
Input:
2
Home
About
Output:
-1
Input:
4
a
b
c
<script>
Output:
4
Input:
1
<script>
Output:
1
Input:
5
1
2
3
4
5
Output:
-1

SOLUTION:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class XSSDetector {
vector<string> logs;
public:
XSSDetector(vector<string> l) : logs(l) {}

int findXSS() {
for (int i = 0; i < logs.size(); ++i) {
if (logs[i].find("<script>") != string::npos)
return i + 1;
}
return -1;
}
};

int main() {
int n;
cin >> n;
cin.ignore();
vector<string> v(n);
for (int i = 0; i < n; ++i) getline(cin, v[i]);
XSSDetector xd(v);
cout << xd.findXSS() << endl;
return 0;
}
5. Sorting Algorithms: “Sort Response Times”
Problem:
A web server logs the response times (in milliseconds) after responding to user requests.
These times are collected for performance analysis. The goal is to sort the response times
in ascending order to easily compare the slowest and fastest responses.
Input:
First line: n (1 ≤ n ≤ 100)
Second line: n space-separated integers (0 ≤ time ≤ 1000)
Output:
Sorted response times in one line, in ascending order.
Test Cases:
Input:
5
500 200 300 100 400
Output:
100 200 300 400 500
Input:
3
000
Output:
000
Input:
4
1000 900 800 700
Output:
700 800 900 1000
Input:
1
123
Output:
123
Input:
6
50 10 60 20 70 30
Output:
10 20 30 50 60 70

SOLUTION:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class ResponseSorter {
vector<int> times;
public:
ResponseSorter(vector<int> t) : times(t) {}

void sortTimes() {
sort(times.begin(), times.end());
}

void print() {
for (int t : times) cout << t << " ";
cout << endl;
}
};

int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
ResponseSorter rs(v);
rs.sortTimes();
rs.print();
return 0;
}

You might also like