forked from CoolerVoid/0d1n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_ops.c
195 lines (148 loc) · 2.79 KB
/
string_ops.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
#include "string_ops.h"
#include "mem_ops.h"
void *entropy_clock(void)
{
double cpu_time;
struct timeval tv;
cpu_time = ((double) clock()) / CLOCKS_PER_SEC;
gettimeofday(&tv, NULL);
srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ (int)cpu_time);
return NULL;
}
char *rand_str(char *dst, int size)
{
static const char text[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
entropy_clock();
int i, len = random() % (size - 1);
if( !len )
len=8;
for ( i=0; i<len; ++i )
dst[i] = text[random() % (sizeof text - 1)];
dst[i] = '\0';
return dst;
}
int char_type_counter(char *string,char type)
{
int counter=0;
while(*string != '\0')
{
if(*string==type)
counter++;
string++;
}
return counter;
}
void chomp(char * str)
{
while (*str)
{
if ( *str == '\n' || *str == '\r' )
{
*str = 0;
return;
}
str++;
}
}
char *payload_injector(char * ptr,char * payload,int counter)
{
char *new=xmalloc((strlen(ptr)+strlen(payload)+2)*sizeof(char));
short i=0,x=1;
memset(new, 0,sizeof(char)*(strlen(ptr)+strlen(payload)+1));
while(*ptr != '\0')
{
if(*ptr == '^')
{
if(counter==x)
{
strncat(new,payload,strlen(payload));
i+=strlen(payload);
}
x++;
}
else
{
*(new+i)=*ptr;
i++;
}
ptr++;
}
//free(new);
return new;
}
int
strstr_regex(char *string, char *expression)
{
regex_t regex;
int reti;
// Compile regular expression
reti = regcomp(®ex, expression, REG_EXTENDED);
if(reti)
{
DEBUG("Could not compile regex ! \n");
DEBUG("%s ",expression);
}
reti = regexec(®ex, string, 0, NULL, 0);
regfree(®ex);
if( !reti )
return 1;
else
return 0;
}
char *replace(char *instring,char *old,char *new)
{
int instring_size=strlen(instring),new_size=strlen(new),old_size=strlen(old),out_size=instring_size+1,count=0;
char *out=NULL,*tmp=NULL;
tmp = xmalloc(old_size+1);
out = xmalloc(out_size);
if(!out || !tmp)
return NULL;
if(instring_size<old_size || !old_size)
{
strcpy(out, instring);
free(tmp);
return out;
}
out[0]='\0';
while(count <= instring_size)
{
strncpy(tmp,(instring+count),old_size);
tmp[old_size]='\0';
if(!strcmp(tmp,old))
{
if(new_size!=old_size)
{
out_size=out_size+new_size-old_size;
out=xrealloc(out,out_size);
if(!out)
{
free(tmp);
return NULL;
}
}
strcat(out,new);
count=count+old_size-1;
}else{
tmp[1]='\0';
strcat(out,tmp);
}
count++;
}
free(tmp);
return out;
}
long parse_http_status(char * str)
{
char part_str[32];
strncpy(part_str,str,31);
char *status=strtok(part_str," ");
status=strtok(NULL," ");
if(strlen(status)<= 3)
{
long code_num=(long)atoi(status);
return code_num;
} else {
return 0;
}
}