1
1
<?php
2
- ini_set ('max_execution_time ' , 600 ); // 10 Minutes
3
- ini_set ('upload_max_filesize ' ,'1024M ' );
4
2
session_start ();
5
3
$ username = 'vue ' ;
6
4
$ password = '123456 ' ;
7
- $ maxWrongAttempts = 100 ;
8
- if (isset ($ _SESSION ['wrong_attemtps_count ' ]) && $ _SESSION ['wrong_attemtps_count ' ] > $ maxWrongAttempts ) {
9
- die ('Too many attempts ' );
10
- }
11
5
12
6
$ _SESSION ['message ' ] ='' ;
13
7
if ($ _SERVER ['REQUEST_METHOD ' ] == 'POST ' && isset ($ _POST ) && isset ($ _POST ['logout ' ])) {
14
8
session_destroy ();
15
9
header ('Location: ' . $ _SERVER ['REQUEST_URI ' ]);
16
10
die ();
17
11
}
12
+
18
13
if (isset ($ _POST ) && isset ($ _POST ['username ' ]) && isset ($ _POST ['password ' ]))
19
14
{
20
- if ($ _POST ['username ' ] == $ username && $ _POST ['password ' ] == $ password ){
21
- $ _SESSION ['username ' ] = $ username ;
22
- } else {
23
- $ _SESSION ['message ' ] ='Username or password is wrong ' ;
24
- if (!isset ($ _SESSION ['wrong_attemtps_count ' ])) {
25
- $ _SESSION ['wrong_attemtps_count ' ] = 0 ;
26
- }
27
- $ _SESSION ['wrong_attemtps_count ' ]++;
15
+ if (ipChek (getIPAddress ())) {
16
+ if ($ _POST ['username ' ] == $ username && $ _POST ['password ' ] == $ password ) {
17
+ $ _SESSION ['username ' ] = $ username ;
18
+ } else {
19
+ $ _SESSION ['message ' ] = 'Username or password is wrong ' ;
20
+
21
+ }
22
+ }else {
23
+ $ _SESSION ['message ' ] ='** Too many attempts Your IP has been blocked ** ' ;
28
24
}
29
25
}
30
26
31
27
if (isset ($ _POST ) && isset ($ _FILES ['zip ' ]))
32
28
{
29
+
33
30
if ($ _SESSION ['username ' ] != $ username ){
34
31
session_destroy ();
35
32
$ _SESSION ['message ' ] ='You are not allowed to upload ' ;
57
54
}?>
58
55
59
56
60
- <?php if (! isset ($ _SESSION ['username ' ])) { ?>
57
+ <?php if (! isset ($ _SESSION ['username ' ])) {
58
+ ?>
61
59
<div class="container">
62
60
<h3>Login</h3>
63
61
<form class="form-container" action="" method="post">
201
199
box-shadow: 0px 0px 6px 0px #4c1010;
202
200
}
203
201
</style>
202
+
203
+
204
+ <!-- ips
205
+ end-->
206
+
207
+
208
+
209
+ <?php
210
+
211
+ function ipChek ($ ipAddress ): bool
212
+ {
213
+ $ maxWrongAttempts = 10 ;
214
+ $ ips = findIps ();
215
+ if (! empty ($ ips )) {
216
+ $ found = false ;
217
+ foreach ($ ips as $ ip ) {
218
+ $ ip = explode (', ' , $ ip );
219
+ $ attempts = trim ($ ip ['1 ' ]);
220
+ if (trim ($ ip [0 ]) == $ ipAddress ) {
221
+ $ found = true ;
222
+ if ($ attempts >= $ maxWrongAttempts ) {
223
+ return false ;
224
+ }
225
+ $ attempts ++;
226
+ ipPush ($ ip [0 ], $ attempts );
227
+ }
228
+ }
229
+ if (!$ found ) {
230
+ ipPush ();
231
+ }
232
+ } else {
233
+ ipPush ();
234
+ }
235
+
236
+ return true ;
237
+ }
238
+
239
+ function ipPush ($ ip = null , $ attempts = 1 ): bool
240
+ {
241
+ if ($ ip && $ attempts ) {
242
+ $ fh = fopen ('./unzip.php ' , 'r+ ' ) or die ($ php_errormsg );
243
+ $ content = '' ;
244
+ while (!feof ($ fh )) {
245
+ $ line = fgets ($ fh , 4096 );
246
+ if (preg_match ('~ ' . $ ip . '~ ' , $ line )) {
247
+ continue ;
248
+ }
249
+ $ content .= $ line ;
250
+ }
251
+ file_put_contents ('./unzip.php ' , $ content );
252
+ fclose ($ fh );
253
+ }
254
+ $ fh = fopen ('./unzip.php ' , 'r+ ' ) or die ($ php_errormsg );
255
+ $ content = '' ;
256
+ $ pattern = '/<!-- ip ' ;
257
+ $ added = false ;
258
+ while (!feof ($ fh )) {
259
+ $ line = fgets ($ fh , 4096 );
260
+ $ content .= $ line ;
261
+ if (!$ added && preg_match ($ pattern .'s/ ' , $ line )){
262
+ $ added = true ;
263
+ $ content .= getIPAddress ().', ' . $ attempts .PHP_EOL ;
264
+ }
265
+ }
266
+ file_put_contents ('./unzip.php ' , $ content );
267
+
268
+ return true ;
269
+ }
270
+
271
+ function getIPAddress () {
272
+ //whether ip is from the share internet
273
+ if (!empty ($ _SERVER ['HTTP_CLIENT_IP ' ])) {
274
+ $ ip = $ _SERVER ['HTTP_CLIENT_IP ' ];
275
+ }
276
+ //whether ip is from the proxy
277
+ elseif (!empty ($ _SERVER ['HTTP_X_FORWARDED_FOR ' ])) {
278
+ $ ip = $ _SERVER ['HTTP_X_FORWARDED_FOR ' ];
279
+ }
280
+ //whether ip is from the remote address
281
+ else {
282
+ $ ip = $ _SERVER ['REMOTE_ADDR ' ];
283
+ }
284
+ return $ ip ;
285
+ }
286
+
287
+ function findIps (): array
288
+ {
289
+ $ ips = [];
290
+ $ fh = fopen ('./unzip.php ' , 'r ' ) or die ($ php_errormsg );
291
+ $ pattern = '/(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))/ ' ;
292
+ while (!feof ($ fh )) {
293
+ $ line = fgets ($ fh , 4096 );
294
+ if (preg_match ($ pattern , $ line )) {
295
+ $ ips [] = $ line ;
296
+ }
297
+ }
298
+ fclose ($ fh );
299
+
300
+ return $ ips ;
301
+ }
0 commit comments