-
Notifications
You must be signed in to change notification settings - Fork 3
/
sample_analysis.c
233 lines (191 loc) · 5.76 KB
/
sample_analysis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* test_stranger.c
*
* Created on: Feb 13, 2014
* Author: muath
*/
#include <stranger/stranger.h>
#include <stranger/stranger_lib_internal.h>
/***********************************************************************************************
/
//This is the test prorgram for the following PHP codes
<?php
$www = $_GET["www"];
$limit = (int)$_GET["limit"];
$l_otherinfo = "URL";
$www = ereg_replace("[^A-Za-z0-9 .\-@://]","",$www);
if(strlen($www) < $limit) {
echo "<td>" . $l_otherinfo . ": " . $www . "</td>";
}
?>
********************************************************************************************************/
void dfa_init_indices_map_coeffs(int* indices, int* map, int* coeffs,
int numVars) {
int i;
for (i = 0; i < numVars; i++) {
indices[i] = i;
indices[i + numVars] = i + numVars;
map[2 * i] = 2 * i + 1;
map[2 * i + 1] = 2 * i;
coeffs[2 * i] = 0;
coeffs[2 * i + 1] = 0;
}
}
void reset_coeffs(int* coeffs, int var) {
int i;
for (i = 0; i < 2 * var; i++)
coeffs[i] = 0;
}
//[^A-Za-z0-9 .-@:/]
DFA *dfaSpecial2(int var, int *indices) {
unsigned long n;
char a = ' ';
dfaSetup(3, var, indices);
dfaAllocExceptions(74);
dfaStoreException(1, bintostr((unsigned long) a, var));
for (n = 46; n <= 90; n++)
dfaStoreException(1, bintostr(n, var));
for (n = 97; n <= 122; n++)
dfaStoreException(1, bintostr(n, var));
dfaStoreException(1, getSharp1(var));
dfaStoreException(1, getSharp0(var));
dfaStoreState(2);
//state 1
dfaAllocExceptions(0);
dfaStoreState(1);
//state 2
dfaAllocExceptions(0);
dfaStoreState(1);
return dfaBuild("--+");
}
//[^A-Za-z0-9 .\-@:/]
DFA *dfaSpecial3(int var, int *indices) {
unsigned long n;
char a = ' ';
dfaSetup(3, var, indices);
dfaAllocExceptions(70);
dfaStoreException(1, bintostr((unsigned long) a, var));
for (n = 45; n <= 58; n++)
dfaStoreException(1, bintostr(n, var));
for (n = 64; n <= 90; n++)
dfaStoreException(1, bintostr(n, var));
for (n = 97; n <= 122; n++)
dfaStoreException(1, bintostr(n, var));
dfaStoreException(1, getSharp1(var));
dfaStoreException(1, getSharp0(var));
dfaStoreState(2);
//state 1
dfaAllocExceptions(0);
dfaStoreState(1);
//state 2
dfaAllocExceptions(0);
dfaStoreState(1);
return dfaBuild("--+");
}
DFA* construct_limit(DFA* M, int svar, int* sindices) {
int var = 2; //0: attack.length, 2: limit
DFA* a; //an arithmetic automaton
//arithmetic automaton
int* indices;
int* map;
int* coeffs;
int constant = 0;
//semiliner
indices = (int *) malloc(sizeof(int) * 2 * var);
map = (int *) malloc(sizeof(int) * 2 * var);
coeffs = (int *) malloc(sizeof(int) * 2 * var);
dfa_init_indices_map_coeffs(indices, map, coeffs, var);
//arithmetic analysis
//0: www.length, 2: limit
reset_coeffs(coeffs, var);
coeffs[0] = 1;
coeffs[2] = -1;
constant = -1;
a = build_DFA_ineq_2sc(2 * var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
/*
uL = dfa_string_to_unaryDFA(M, svar, sindices);
s=getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
*/
return a;
}
int main(int argc, char**argv) {
int var = 2; //0: attack.length, 2: limit
DFA* a; //an arithmetic automaton
int i;
//arithmetic automaton
int* indices;
int* map;
int* coeffs;
int constant = 0;
//string automaton
int svar = NUM_ASCII_TRACKS;
int* sindices = allocateAscIIIndexWithExtraBit(NUM_ASCII_TRACKS);
DFA* attack = NULL; //for fixed point computation
DFA* M[4];
DFA* stmp;
//semiliner
DFA* uL = NULL;
struct semilinear_type* s;
indices = (int *) malloc(sizeof(int) * 2 * var);
map = (int *) malloc(sizeof(int) * 2 * var);
coeffs = (int *) malloc(sizeof(int) * 2 * var);
dfa_init_indices_map_coeffs(indices, map, coeffs, var);
//attack strings
attack = dfa_concat_extrabit(dfaAllStringASCIIExceptReserveWords(svar,
sindices), dfa_construct_string("<script ", svar, sindices), svar,
sindices);
attack = dfa_concat_extrabit(attack, dfaAllStringASCIIExceptReserveWords(
svar, sindices), svar, sindices);
//string analysis
M[0] = dfaAllStringASCIIExceptReserveWords(svar, sindices); //$www
//dfaPrintVerbose(M[0]);
M[1] = dfa_construct_string("URL", svar, sindices);
//dfaPrintVerbose(M[1]);
M[2] = dfaSpecial2(svar, sindices); //for [^A-Za-z0-9 .-@:/]
//dfaPrintVerbose(M[2]);
M[3] = dfa_replace_extrabit(M[0], M[2], "", svar, sindices);
dfaFree(M[0]);
M[0] = dfa_construct_string("<td>", svar, sindices);
M[0] = dfa_concat_extrabit(M[0], M[1], svar, sindices);
M[0] = dfa_concat_extrabit(M[0],
dfa_construct_string(": ", svar, sindices), svar, sindices);
M[0] = dfa_concat_extrabit(M[0], M[3], svar, sindices);
M[0] = dfa_concat_extrabit(M[0], dfa_construct_string("</td>", svar,
sindices), svar, sindices);
//dfaPrintVitals(M[0]);
//arithmetic analysis
//0: www.length, 2: limit
reset_coeffs(coeffs, var);
coeffs[0] = 1;
coeffs[2] = -1;
constant = -1;
a = build_DFA_ineq_2sc(2 * var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
//One should compute teh reange of $www.length from a
//Here it is [0,inf].
//Construct \Sigma^[0,inf]
stmp = dfaAllStringASCIIExceptReserveWords(svar, sindices);
M[0] = dfaMinimize(dfaProduct(M[0], stmp, dfaAND));
i = check_intersection(M[0], attack, svar, sindices);
if (i == 0)
printf("Result: Secure!\n");
else if (i == 1)
printf("Result: Vulnerable!\n");
else
printf("Result: error!\n");
uL = dfa_string_to_unaryDFA(M[0], svar, sindices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
printf("Memory Allocated: %d\n", mem_allocated());
for (i = 0; i < 4; i++)
dfaFree(M[i]);
dfaFree(attack);
dfaFree(stmp);
dfaFree(uL);
dfaFree(a);
return 0;
}