Match multiple strings in iptables












2















I have 2 strings, and i wish to queue the packet if it contains both the strings ( something like ("jsh"&&"gjhyg")), i tried following ways, but they don't seem to work:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm -m string --string "def" --algo bm



This doesn't work, it only works if the packet contains the string "abcdef", but the packet i wish to queue contains the strings at two different locations. Then I tried another method:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



But this time it works like "or", it queues packets with string "abc" or "def".










share|improve this question

























  • I would have expected the first solution to work, since multiple -m are normally combined with an AND... and I can't find any reference of using regexp in the pattern. I'd say you just can't do it :/

    – m4573r
    Oct 2 '12 at 8:04











  • I can do this, but only at application level. By analyzing the queued packets matching any one string, and then matching for second string in my netfilter_queue C module. This method is definitely slower.

    – adnan kamili
    Oct 2 '12 at 10:06
















2















I have 2 strings, and i wish to queue the packet if it contains both the strings ( something like ("jsh"&&"gjhyg")), i tried following ways, but they don't seem to work:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm -m string --string "def" --algo bm



This doesn't work, it only works if the packet contains the string "abcdef", but the packet i wish to queue contains the strings at two different locations. Then I tried another method:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



But this time it works like "or", it queues packets with string "abc" or "def".










share|improve this question

























  • I would have expected the first solution to work, since multiple -m are normally combined with an AND... and I can't find any reference of using regexp in the pattern. I'd say you just can't do it :/

    – m4573r
    Oct 2 '12 at 8:04











  • I can do this, but only at application level. By analyzing the queued packets matching any one string, and then matching for second string in my netfilter_queue C module. This method is definitely slower.

    – adnan kamili
    Oct 2 '12 at 10:06














2












2








2


1






I have 2 strings, and i wish to queue the packet if it contains both the strings ( something like ("jsh"&&"gjhyg")), i tried following ways, but they don't seem to work:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm -m string --string "def" --algo bm



This doesn't work, it only works if the packet contains the string "abcdef", but the packet i wish to queue contains the strings at two different locations. Then I tried another method:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



But this time it works like "or", it queues packets with string "abc" or "def".










share|improve this question
















I have 2 strings, and i wish to queue the packet if it contains both the strings ( something like ("jsh"&&"gjhyg")), i tried following ways, but they don't seem to work:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm -m string --string "def" --algo bm



This doesn't work, it only works if the packet contains the string "abcdef", but the packet i wish to queue contains the strings at two different locations. Then I tried another method:



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "abc" --algo bm



sudo iptables -A INPUT -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



But this time it works like "or", it queues packets with string "abc" or "def".







linux networking ubuntu iptables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 10 '14 at 14:00









Kenster

4,93022034




4,93022034










asked Oct 2 '12 at 6:29









adnan kamiliadnan kamili

2811515




2811515













  • I would have expected the first solution to work, since multiple -m are normally combined with an AND... and I can't find any reference of using regexp in the pattern. I'd say you just can't do it :/

    – m4573r
    Oct 2 '12 at 8:04











  • I can do this, but only at application level. By analyzing the queued packets matching any one string, and then matching for second string in my netfilter_queue C module. This method is definitely slower.

    – adnan kamili
    Oct 2 '12 at 10:06



















  • I would have expected the first solution to work, since multiple -m are normally combined with an AND... and I can't find any reference of using regexp in the pattern. I'd say you just can't do it :/

    – m4573r
    Oct 2 '12 at 8:04











  • I can do this, but only at application level. By analyzing the queued packets matching any one string, and then matching for second string in my netfilter_queue C module. This method is definitely slower.

    – adnan kamili
    Oct 2 '12 at 10:06

















I would have expected the first solution to work, since multiple -m are normally combined with an AND... and I can't find any reference of using regexp in the pattern. I'd say you just can't do it :/

– m4573r
Oct 2 '12 at 8:04





I would have expected the first solution to work, since multiple -m are normally combined with an AND... and I can't find any reference of using regexp in the pattern. I'd say you just can't do it :/

– m4573r
Oct 2 '12 at 8:04













I can do this, but only at application level. By analyzing the queued packets matching any one string, and then matching for second string in my netfilter_queue C module. This method is definitely slower.

– adnan kamili
Oct 2 '12 at 10:06





I can do this, but only at application level. By analyzing the queued packets matching any one string, and then matching for second string in my netfilter_queue C module. This method is definitely slower.

– adnan kamili
Oct 2 '12 at 10:06










1 Answer
1






active

oldest

votes


















0














the "and" in this case could be achived with an user defined chain




sudo iptables -N my_chain



sudo iptables -A my_chain -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



sudo iptables -A INPUT -p tcp -j my_chain ! -f -m string --string "abc" --algo bm




when the input chain process the last line and "abc" is present the control jumps to my_chain which has a similar rule checking for the presence of "def"; if "def" is there then jumps to QUEUE.






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f482274%2fmatch-multiple-strings-in-iptables%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    the "and" in this case could be achived with an user defined chain




    sudo iptables -N my_chain



    sudo iptables -A my_chain -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



    sudo iptables -A INPUT -p tcp -j my_chain ! -f -m string --string "abc" --algo bm




    when the input chain process the last line and "abc" is present the control jumps to my_chain which has a similar rule checking for the presence of "def"; if "def" is there then jumps to QUEUE.






    share|improve this answer






























      0














      the "and" in this case could be achived with an user defined chain




      sudo iptables -N my_chain



      sudo iptables -A my_chain -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



      sudo iptables -A INPUT -p tcp -j my_chain ! -f -m string --string "abc" --algo bm




      when the input chain process the last line and "abc" is present the control jumps to my_chain which has a similar rule checking for the presence of "def"; if "def" is there then jumps to QUEUE.






      share|improve this answer




























        0












        0








        0







        the "and" in this case could be achived with an user defined chain




        sudo iptables -N my_chain



        sudo iptables -A my_chain -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



        sudo iptables -A INPUT -p tcp -j my_chain ! -f -m string --string "abc" --algo bm




        when the input chain process the last line and "abc" is present the control jumps to my_chain which has a similar rule checking for the presence of "def"; if "def" is there then jumps to QUEUE.






        share|improve this answer















        the "and" in this case could be achived with an user defined chain




        sudo iptables -N my_chain



        sudo iptables -A my_chain -p tcp -j QUEUE ! -f -m string --string "def" --algo bm



        sudo iptables -A INPUT -p tcp -j my_chain ! -f -m string --string "abc" --algo bm




        when the input chain process the last line and "abc" is present the control jumps to my_chain which has a similar rule checking for the presence of "def"; if "def" is there then jumps to QUEUE.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 9 '12 at 22:23

























        answered Oct 9 '12 at 22:15









        PatPat

        2,5341021




        2,5341021






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f482274%2fmatch-multiple-strings-in-iptables%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            Brian Clough

            Cáceres