SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Discussion

Reply
 
Thread Tools Display Modes
Old 31/03/2016, 06:42 AM   #1
Kar
Banned
 
Join Date: May 2010
Location: Black Asylum
Posts: 2,940
Reputation: 538
Default Anti Advertising without regex

Hello everyone, today I want to start a community effort to build an anti advertising function to detect adverts in strings without the use of regex.

I hope people will contribute.

I've started off using RyDer's stringContainsIP function as a base and modified it but I would like help from you guys.

You can contribute here: https://github.com/Kar2k/samp-anti-advertising

Current features:

Actual IP detection (use of numbers ranging from 0-255) with port.
Whitespace skipping.
Detection works with other symbols such as (127_0_0_1:7777).

What are all the things to consider when creating this?

Future plans:

- Allow IP detection without port (only works if a port is used currently). DONE.
- Instead of considering every character in the string.. only look for the numbers and check if they are valid IP numbers (0-255) and use that to consider detection. DONE.
- Ignoring letters between numbers and dots / colons .. E.G:
127aa.0.0zz.1.ff7777 DONE.
- Fixed separation option between ip numbers detection .. E.G: 127 $$$$ 0 $$$$ 0 $$$$ 1. As most advertisers that come in your server usual has a fixed set of characters in between the numbers in this case its " $$$$ ". Adding an option to detect that will greatly decrease false positives. DONE.
- Website detection possibly (maybe not.. too much false positives).

Last edited by Kar; 18/04/2016 at 09:21 PM.
Kar is offline   Reply With Quote
Old 31/03/2016, 09:29 AM   #2
xTURBOx
Huge Clucker
 
xTURBOx's Avatar
 
Join Date: Aug 2015
Location: Earth
Posts: 378
Reputation: 49
Default Re: Anti Advertising without regex

well from my experience some people use commas in ips like
127,0,0,1 port 7777
__________________
xTURBOx is offline   Reply With Quote
Old 31/03/2016, 03:05 PM   #3
Kar
Banned
 
Join Date: May 2010
Location: Black Asylum
Posts: 2,940
Reputation: 538
Default Re: Anti Advertising without regex

I've updated the detection to skip all non-IP numbers (non +/- 0-255).
Added more valid IP number detection (can't use numbers like 1-1.1-1.1-1.1-1).

Code:
[31/03/2016 10:02:50] 000.000.000.000:7777 - 1.
[31/03/2016 10:02:50] 0.0.0.0:7777 - 1.
[31/03/2016 10:02:50] 127.0.0.1:7777 - 1.
[31/03/2016 10:02:50] 127  .  0.  0.  1:  7777 - 1.
[31/03/2016 10:02:50] 255.255.255.255:7777 - 1.
[31/03/2016 10:02:50] PLS COME JOIN SERVER 37____187____22____119 - 1.
[31/03/2016 10:02:50] PLS COME JOIN SERVER 37 $$$$ 187 $$$$ 22 $$$$ 119 - 1.
[31/03/2016 10:02:50] 0000.000.000.0000:7777 - 0.
[31/03/2016 10:02:50] 255.256.255.255:7777 - 0.
[31/03/2016 10:02:50] -1.-1.-1.-1:7777 - 0.
[31/03/2016 10:02:50] 1-1.1-1.1-1.1-1:7777 - 0.
1 means its a valid IP.

FULL update at https://github.com/Kar2k/samp-anti-a...8d53a5b2dc5286
Kar is offline   Reply With Quote
Old 31/03/2016, 05:59 PM   #4
xTURBOx
Huge Clucker
 
xTURBOx's Avatar
 
Join Date: Aug 2015
Location: Earth
Posts: 378
Reputation: 49
Default Re: Anti Advertising without regex

Nice one
__________________
xTURBOx is offline   Reply With Quote
Old 31/03/2016, 06:23 PM   #5
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,773
Reputation: 754
Default Re: Anti Advertising without regex

pawn Code:
// ** INCLUDES

#include <a_samp>
#include <sscanf>

// ** DEFINES

// *** FUNCTIONS

#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))

// ** MAIN

main()
{
    print("Loaded \"anti_advert.amx\".");

    new str[128];

    // Valid
    str = "000.000.000.000:7777";
    printf("%s - %d.", str, IsAdvertisement(str));
    str = "0.0.0.0:7777";
    printf("%s - %d.", str, IsAdvertisement(str));
    str = "255.255.255.255:7777";
    printf("%s - %d.", str, IsAdvertisement(str));
    str = "PLS COME JOIN SERVER 37____187____22____119";
    printf("%s - %d.", str, IsAdvertisement(str));
    str = "PLS COME JOIN SERVER 37 $$$$ 187 $$$$ 22 $$$$ 119";
    printf("%s - %d.", str, IsAdvertisement(str));

    // Invalid
    str = "0000.000.000.0000:7777";
    printf("%s - %d.", str, IsAdvertisement(str));
    str = "255.256.255.255:7777";
    printf("%s - %d.", str, IsAdvertisement(str));
    str = "-1.-1.-1.-1:7777";
    printf("%s - %d.", str, IsAdvertisement(str));
    str = "-32.-187.-22.-119 - NOTE: REMOVE THE -s AND JOIN NOW!";
    printf("%s - %d.", str, IsAdvertisement(str));
}

// ** CALLBACKS

public OnGameModeInit()
{  
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerText(playerid, text[])
{
    if(IsAdvertisement(text))
    {
        SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}[ERROR]: {FFFFFF}Your message contains an IP Address (strict detection).");
        return 0;
    }
    return 1;
}

// ** FUNCTIONS

forward bool:IsAdvertisement(text[]);
public bool:IsAdvertisement(text[])
{
    new message[128], extract[2], element[4][4], count_1, count_2, temp, bool:number_next = false, bool:next_number = false, bool:advert = false;
    strcpy(message, text, sizeof(message));

    for(new i = 0, j = strlen(message); i < j; i ++)
    {
        switch(message[i])
        {
            case '0'..'9':
            {
                if(next_number)
                {
                    continue;
                }

                number_next = false;

                strmid(extract, message[i], 0, 1);
                strcat(element[count_1], extract);
               
                count_2 ++;

                if(count_2 == 3 || message[i + 1] == EOS)
                {
                    strmid(extract, message[i + 1], 0, 1);

                    if(IsNumeric(extract))
                    {
                        element[0][0] = EOS;
                        element[1][0] = EOS;
                        element[2][0] = EOS;
                        element[3][0] = EOS;

                        count_1 = 0;
                        count_2 = 0;

                        next_number = true;
                        continue;
                    }

                    temp = strval(element[count_1]);

                    if(count_1 == 0)
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;
                        }
                        else
                        {
                            element[count_1][0] = EOS;

                            count_2 = 0;

                            next_number = true;
                        }
                    }
                    else
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;
                        }
                        else
                        {
                            element[0][0] = EOS;
                            element[1][0] = EOS;
                            element[2][0] = EOS;
                            element[3][0] = EOS;

                            count_1 = 0;
                            count_2 = 0;

                            next_number = true;
                        }
                    }
                }

                if(count_1 == 4)
                {
                    advert = true;
                    break;
                }
            }
            default:
            {
                next_number = false;

                if(number_next)
                {
                    continue;
                }

                if(!isnull(element[count_1]))
                {
                    temp = strval(element[count_1]);

                    if(count_1 == 0)
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;

                            number_next = true;
                        }
                        else
                        {
                            element[count_1][0] = EOS;

                            count_2 = 0;
                        }
                    }
                    else
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;

                            number_next = true;
                        }
                        else
                        {
                            element[0][0] = EOS;
                            element[1][0] = EOS;
                            element[2][0] = EOS;
                            element[3][0] = EOS;

                            count_1 = 0;
                            count_2 = 0;
                        }
                    }

                    if(count_1 == 4)
                    {
                        advert = true;
                        break;
                    }
                }
            }
        }
    }
    return advert;
}

stock IsNumeric(const string[])
{
    return !sscanf(string, "{d}");
}
Results:
000.000.000.000:7777 - 1.
0.0.0.0:7777 - 1.
255.255.255.255:7777 - 1.
PLS COME JOIN SERVER 37____187____22____119 - 1.
PLS COME JOIN SERVER 37 $$$$ 187 $$$$ 22 $$$$ 119 - 1.
0000.000.000.0000:7777 - 0.
255.256.255.255:7777 - 0.
-1.-1.-1.-1:7777 - 1.
-32.-187.-22.-119 - NOTE: REMOVE THE -s AND JOIN NOW! - 1.

I'm happy with my results. But you may be wondering, why are the last 2 IPs detected as valid IPs? Well, it's for more security in my opinion. If a player sends a message like "-32.-187.-22.-119 - NOTE: REMOVE THE -s AND JOIN NOW!" it will go through and logically the player would win the battle and bring all your players onto their server.

Also, the function is way faster than regex based systems. So that aspect isn't to be worried about.
SickAttack is offline   Reply With Quote
Old 31/03/2016, 08:32 PM   #6
Kar
Banned
 
Join Date: May 2010
Location: Black Asylum
Posts: 2,940
Reputation: 538
Default Re: Anti Advertising without regex

Cool.

I was going to add an option to allow or ignore negatives.

Also going to add fixed separation checks and an option to must have ports.

EDIT: Added it, thanks!

Last edited by Kar; 31/03/2016 at 09:06 PM.
Kar is offline   Reply With Quote
Old 02/04/2016, 08:48 PM   #7
Macluawn
Gangsta
 
Macluawn's Avatar
 
Join Date: Aug 2010
Location: Latvia
Posts: 613
Reputation: 78
Default Re: Anti Advertising without regex

What is your argument against regex? Some benchmarks might be useful.
Macluawn is offline   Reply With Quote
Old 02/04/2016, 11:04 PM   #8
Kar
Banned
 
Join Date: May 2010
Location: Black Asylum
Posts: 2,940
Reputation: 538
Default Re: Anti Advertising without regex

Quote:
Originally Posted by Macluawn View Post
What is your argument against regex? Some benchmarks might be useful.
I'm not sure, the regex plugins crash the server when weird characters are entered (for me atleast). So even if I benchmark, I still won't be able to use it properly. That's why I'm creating this.
Kar is offline   Reply With Quote
Old 04/04/2016, 04:49 PM   #9
kristo
Banned
 
Join Date: Jun 2012
Location: Estonia
Posts: 352
Reputation: 179
Default Re: Anti Advertising without regex

Quote:
Originally Posted by Kar View Post
I'm not sure, the regex plugins crash the server when weird characters are entered (for me atleast). So even if I benchmark, I still won't be able to use it properly. That's why I'm creating this.
Only the one released in 2011 does this. This one's fine: http://forum.sa-mp.com/showthread.php?t=526725
kristo is offline   Reply With Quote
Old 04/04/2016, 08:46 PM   #10
Macluawn
Gangsta
 
Macluawn's Avatar
 
Join Date: Aug 2010
Location: Latvia
Posts: 613
Reputation: 78
Default Re: Anti Advertising without regex

Quote:
Originally Posted by Kar View Post
I'm not sure, the regex plugins crash the server when weird characters are entered (for me atleast). So even if I benchmark, I still won't be able to use it properly. That's why I'm creating this.
How about not going down npm's road, and actually fix the problem in the plugin instead if making something new?
Macluawn is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anti-Advertising Biggy54 Scripting Help 3 04/09/2014 10:27 AM
[FilterScript] [NEW] Anti swear and Anti Advertising [BLOCKS WORDS] Dopefull Filterscripts 24 10/10/2013 12:48 PM
[HELP]Anti-IP advertising Nenad Help Archive 14 06/05/2011 06:19 PM
Anti Advertising bajskorv123 Help Archive 6 21/04/2011 10:53 AM


All times are GMT. The time now is 06:44 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.