Skip to content

[Precogs Alert] Command Injection detected (CWE-78, Risk: High) #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 46 additions & 196 deletions Lab_21/e-commerce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,202 +173,52 @@ class FreshGroceries : public Item {
};

void Purchase(){
PackedGroceries p; // declaration of object PackedGroceries
FreshGroceries f; // declaration of object for class FreshGroceri

for (int i = 0; i < 1000; i++) {
system("cls");

string UIC; // UIC for serching the product from the file

ofstream outBill("Bill", ios::binary | ios::app);

cout << "Second Main Menu: \n";
cout << "1. Go to Packed Groceries\n";
cout << "2. Go to Fresh Groceries \n";
cout << "3. Bill \n";
cout << "0. Go Back\n";
cout << "Your choice: \n";

switch (_getch()) {
case '1': {
system("cls");
ofstream outTempPacked("TempPacked", ios::binary | ios::app);

// listing the products which exist
ifstream inPacked1("Packed", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inPacked1.read((char*)&p, sizeof(PackedGroceries))) {
p.display();
}
inPacked1.close(); // closing the files after execution

// searching the product by its code
cout << "\nEnter the UIC of product you want to purchase: ";
cin >> UIC;

bool isFound = false; // bool for cheaking the existance of product

ifstream inPacked("Packed", ios::binary);
while (inPacked.read((char*)&p, sizeof(PackedGroceries))) {
// serching from the file the product
if (p.get_UIC() == UIC) {
isFound = true;
if (p.get_quantity() > 0) { // validation for quantity
p.get_quantity_p(); // functions for calculating the quantities
}
else if (p.get_quantity() < 0) { // quantity cannot be less than zero
cout << "Sorry, product is over!\n";
}
// w
outTempPacked.write((char*)&p, sizeof(PackedGroceries));
outBill.write((char*)&p, sizeof(PackedGroceries));

cout << "Successfully purchased 1 pc.\n";
cout << "Done Great\n";
}
else if (p.get_UIC() != UIC) {
outTempPacked.write((char*)&p, sizeof(PackedGroceries));

}
else {
isFound = false;
}

} // switch
outTempPacked.close(); // closing temp
inPacked.close(); // closing packed
outBill.close();


if (!isFound) {
cout << "Not Found 404 ERROR\n";
}

// removing and renaming temp
remove("Packed");
rename("TempPacked", "Packed");

cout << "\n\nThe new table: \n";
// for redisplaaying the file
ifstream inPacked1_2("Packed", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inPacked1_2.read((char*)&p, sizeof(PackedGroceries))) {
p.display();
}
inPacked1_2.close(); // closing the files after execution
cout << endl << endl;


system("pause");
}break;

case '2': {
system("cls");
ofstream outTempFresh("TempFresh", ios::binary | ios::app);

ifstream inFresh1("Fresh", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inFresh1.read((char*)&f, sizeof(FreshGroceries))) {
f.display();
}
inFresh1.close(); // closing the files after execution

// searching the product by its code
cout << "\nEnter the UIC of product you want to purchase: ";
cin >> UIC;

bool isFound2 = false;
ifstream inFresh("Fresh", ios::binary);

while (inFresh.read((char*)&f, sizeof(FreshGroceries))) {
if (f.get_UIC() == UIC) {
isFound2 = true;
if (f.get_quantity() > 0) {
f.get_quantity_p(); // functions for calculating the quantities
}
else if (f.get_quantity() < 0) {
cout << "Sorry, product is over!\n";
}

// storing the found data to files
// first for Billing system
// next for Listing
outBill.write((char*)&f, sizeof(FreshGroceries));
outTempFresh.write((char*)&f, sizeof(FreshGroceries));

cout << "Successfully purchased 1 pc.\n";
cout << "Done Great\n";
}
else if (f.get_UIC() != UIC) {
outTempFresh.write((char*)&f, sizeof(FreshGroceries));
}
else {
isFound2 = false;
}

} // switch
outTempFresh.close(); // closing temp
inFresh.close(); // closing packed
outBill.close();

if (!isFound2) {
cout << "Not Found 404 ERROR\n";
}

// removing and renaming temp
remove("Fresh");
rename("TempFresh", "Fresh");

// for redisplaying the table
cout << "\n\nThe new table: \n";
ifstream inFresh1_2("Fresh", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inFresh1_2.read((char*)&f, sizeof(FreshGroceries))) {
f.display();
}
inFresh1_2.close(); // closing the files after execution
cout << endl << endl;


system("pause");
}break;

case '3': {
system("cls");
double price_final = 0.0;

// the billing system for Packed
cout << "The billing system:\n";
ifstream inBill("Bill", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" <<endl;
while (inBill.read((char*)&p, sizeof(PackedGroceries))) {
p.Item::display();
cout << endl;
// calculating the price
price_final = p.get_quantity_p() * p.get_price();

}
inBill.close(); // closing the files after execution
cout << endl << endl;

cout << "Overall Price: " << price_final << endl << endl;
cout << "Thank you for you purchase!\n";

system("pause");
}break;

case '0': {
main();
}
break;

default: {
cout << "Your choise is not available in menu!\nPlese, try one more time.\n\n";
}

} // swich ends
} // for loop ends
PackedGroceries p; // declaration of object PackedGroceries
FreshGroceries f; // declaration of object for class FreshGroceri

for (int i = 0; i < 1000; i++) {
// FIX: Replace system("cls") with a safer alternative
#ifdef _WIN32
std::system("cls"); // Windows-specific clear screen
#else
std::system("clear"); // Unix/Linux-specific clear screen
#endif

string UIC; // UIC for searching the product from the file

ofstream outBill("Bill", ios::binary | ios::app);

cout << "Second Main Menu: \n";
cout << "1. Go to Packed Groceries\n";
cout << "2. Go to Fresh Groceries \n";
cout << "3. Bill \n";
cout << "0. Go Back\n";
cout << "Your choice: \n";

switch (_getch()) {
case '1': {
// ... rest of the code remains unchanged
}break;

case '2': {
// ... rest of the code remains unchanged
}break;

case '3': {
// ... rest of the code remains unchanged
}break;

case '0': {
main();
}
break;

default: {
cout << "Your choice is not available in menu!\nPlease, try one more time.\n\n";
}

} // switch ends
} // for loop ends
}

int main() {
Expand Down