Skip to content

Commit f715a34

Browse files
committed
better examples
1 parent 3ddc9a8 commit f715a34

2 files changed

Lines changed: 141 additions & 12 deletions

File tree

cpp/ql/src/experimental/Security/CWE/CWE-409-DecompressionBomb/example_bad.cpp

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,64 @@
1-
#include <iostream>
2-
#include <vector>
3-
#include "zlib.h"
4-
int UnsafeRead() {
1+
2+
int UnsafeInflate(int argc, char *argv[]) {
3+
// original string len = 36
4+
char a[50] = "Hello Hello Hello Hello Hello Hello!";
5+
// placeholder for the compressed (deflated) version of "a"
6+
char b[50];
7+
// placeholder for the Uncompressed (inflated) version of "b"
8+
char c[50];
9+
printf("Uncompressed size is: %lu\n", strlen(a));
10+
printf("Uncompressed string is: %s\n", a);
11+
printf("\n----------\n\n");
12+
13+
// STEP 1.
14+
// zlib struct
15+
z_stream defstream;
16+
defstream.zalloc = Z_NULL;
17+
defstream.zfree = Z_NULL;
18+
defstream.opaque = Z_NULL;
19+
// setup "a" as the input and "b" as the compressed output
20+
defstream.avail_in = (uInt) strlen(a) + 1; // size of input, string + terminator
21+
defstream.next_in = (Bytef *) a; // input char array
22+
defstream.avail_out = (uInt) sizeof(b); // size of output
23+
defstream.next_out = (Bytef *) b; // output char array
24+
25+
// the actual compression work.
26+
deflateInit(&defstream, Z_BEST_COMPRESSION);
27+
deflate(&defstream, Z_FINISH);
28+
deflateEnd(&defstream);
29+
30+
// This is one way of getting the size of the output
31+
printf("Compressed size is: %lu\n", strlen(b));
32+
printf("Compressed string is: %s\n", b);
33+
printf("\n----------\n\n");
34+
// STEP 2.
35+
// inflate b into c
36+
// zlib struct
37+
z_stream infstream;
38+
infstream.zalloc = Z_NULL;
39+
infstream.zfree = Z_NULL;
40+
infstream.opaque = Z_NULL;
41+
// setup "b" as the input and "c" as the compressed output
42+
// TOTHINK: Here we can add additional step from Right operand to z_stream variable access
43+
infstream.avail_in = (uInt) ((char *) defstream.next_out - b); // size of input
44+
infstream.next_in = (Bytef *) b; // input char array
45+
infstream.avail_out = (uInt) sizeof(c); // size of output
46+
infstream.next_out = (Bytef *) c; // output char array
47+
48+
// uLong total_out; /* total number of bytes output so far */
49+
// the actual DE-compression work.
50+
inflateInit(&infstream);
51+
std::cout << infstream.total_out << std::endl;
52+
inflate(&infstream, Z_NO_FLUSH);
53+
std::cout << infstream.total_out << std::endl;
54+
inflateEnd(&infstream);
55+
56+
printf("Uncompressed size is: %lu\n", strlen(c));
57+
printf("Uncompressed string is: %s\n", c);
58+
return 0;
59+
}
60+
61+
int UnsafeGzread() {
562
std::cout << "enter compressed file name!\n" << std::endl;
663
char fileName[100];
764
std::cin >> fileName;
@@ -21,10 +78,47 @@ int UnsafeRead() {
2178
break;
2279
}
2380
}
81+
for (auto &&i: unzippedData)
82+
std::cout << i;
83+
gzclose(inFileZ);
84+
return 0;
85+
}
2486

25-
for ( auto &&i: unzippedData)
26-
std::cout << i;
87+
int UnsafeGzfread() {
88+
std::cout << "enter compressed file name!\n" << std::endl;
89+
char fileName[100];
90+
std::cin >> fileName;
91+
gzFile inFileZ = gzopen(fileName, "rb");
92+
if (inFileZ == nullptr) {
93+
printf("Error: Failed to gzopen %s\n", fileName);
94+
exit(0);
95+
}
96+
while (true) {
97+
char buffer[1000];
98+
if (!gzfread(buffer, 999, 1, inFileZ)) {
99+
break;
100+
}
101+
}
27102
gzclose(inFileZ);
103+
return 0;
104+
}
28105

106+
int UnsafeGzgets() {
107+
std::cout << "enter compressed file name!\n" << std::endl;
108+
char fileName[100];
109+
std::cin >> fileName;
110+
gzFile inFileZ = gzopen(fileName, "rb");
111+
if (inFileZ == nullptr) {
112+
printf("Error: Failed to gzopen %s\n", fileName);
113+
exit(0);
114+
}
115+
char *buffer = new char[4000000000];
116+
char *result = gzgets(inFileZ, buffer, 1000000000);
117+
while (true) {
118+
result = gzgets(inFileZ, buffer, 1000000000);
119+
if (result == nullptr) {
120+
break;
121+
}
122+
}
29123
return 0;
30-
}
124+
}

cpp/ql/src/experimental/Security/CWE/CWE-409-DecompressionBomb/example_good.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#include <iostream>
2-
#include <vector>
3-
#include "zlib.h"
4-
int SafeRead() {
1+
2+
int SafeGzread() {
53
std::cout << "enter compressed file name!\n" << std::endl;
64
char fileName[100];
75
std::cin >> fileName;
@@ -35,4 +33,41 @@ int SafeRead() {
3533
gzclose(inFileZ);
3634

3735
return 0;
38-
}
36+
}
37+
38+
int SafeGzread2() {
39+
std::cout << "enter compressed file name!\n" << std::endl;
40+
char fileName[100];
41+
std::cin >> fileName;
42+
gzFile inFileZ = gzopen(fileName, "rb");
43+
if (inFileZ == nullptr) {
44+
printf("Error: Failed to gzopen %s\n", fileName);
45+
exit(0);
46+
}
47+
const int BUFFER_SIZE = 8192;
48+
unsigned char unzipBuffer[BUFFER_SIZE];
49+
unsigned int unzippedBytes;
50+
uint totalRead = 0;
51+
std::vector<unsigned char> unzippedData;
52+
while (true) {
53+
unzippedBytes = gzread(inFileZ, unzipBuffer, BUFFER_SIZE);
54+
totalRead += BUFFER_SIZE;
55+
if (unzippedBytes > 0) {
56+
unzippedData.insert(unzippedData.end(), unzipBuffer, unzipBuffer + unzippedBytes);
57+
if (totalRead > 1024 * 1024 * 4) {
58+
std::cout << "Bombs!" << totalRead;
59+
exit(1);
60+
} else {
61+
std::cout << "not Bomb yet!!" << totalRead << std::endl;
62+
}
63+
} else {
64+
break;
65+
}
66+
}
67+
68+
for (auto &&i: unzippedData)
69+
std::cout << i;
70+
gzclose(inFileZ);
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)