Terminate established tcp connection using iptables












0















I have an open socket connection, and I want to close it immediately if packet passing through contains certain string. I have tried the following using iptables:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT
sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j DROP


But none of this works, it just freezes connection permanently. How to "cut" the connection permanently?



Note: as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.










share|improve this question

























  • When you reject a packet a response is sent indicating this. When you drop a packet no response is sent. Most likely the application you are using does not know how to handle the former, and might have a very long timeout (often in the order of minutes) for the latter, to allow for really slow and unreliable network connections. Which application are you trying to control?

    – l0b0
    Apr 11 '18 at 10:00











  • @l0b0 as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.

    – nikrom3000
    Apr 11 '18 at 10:14
















0















I have an open socket connection, and I want to close it immediately if packet passing through contains certain string. I have tried the following using iptables:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT
sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j DROP


But none of this works, it just freezes connection permanently. How to "cut" the connection permanently?



Note: as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.










share|improve this question

























  • When you reject a packet a response is sent indicating this. When you drop a packet no response is sent. Most likely the application you are using does not know how to handle the former, and might have a very long timeout (often in the order of minutes) for the latter, to allow for really slow and unreliable network connections. Which application are you trying to control?

    – l0b0
    Apr 11 '18 at 10:00











  • @l0b0 as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.

    – nikrom3000
    Apr 11 '18 at 10:14














0












0








0








I have an open socket connection, and I want to close it immediately if packet passing through contains certain string. I have tried the following using iptables:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT
sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j DROP


But none of this works, it just freezes connection permanently. How to "cut" the connection permanently?



Note: as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.










share|improve this question
















I have an open socket connection, and I want to close it immediately if packet passing through contains certain string. I have tried the following using iptables:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT
sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j DROP


But none of this works, it just freezes connection permanently. How to "cut" the connection permanently?



Note: as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.







networking scripting iptables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 21 at 17:26









Pierre.Vriens

1,00051115




1,00051115










asked Apr 11 '18 at 9:49









nikrom3000nikrom3000

2171211




2171211













  • When you reject a packet a response is sent indicating this. When you drop a packet no response is sent. Most likely the application you are using does not know how to handle the former, and might have a very long timeout (often in the order of minutes) for the latter, to allow for really slow and unreliable network connections. Which application are you trying to control?

    – l0b0
    Apr 11 '18 at 10:00











  • @l0b0 as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.

    – nikrom3000
    Apr 11 '18 at 10:14



















  • When you reject a packet a response is sent indicating this. When you drop a packet no response is sent. Most likely the application you are using does not know how to handle the former, and might have a very long timeout (often in the order of minutes) for the latter, to allow for really slow and unreliable network connections. Which application are you trying to control?

    – l0b0
    Apr 11 '18 at 10:00











  • @l0b0 as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.

    – nikrom3000
    Apr 11 '18 at 10:14

















When you reject a packet a response is sent indicating this. When you drop a packet no response is sent. Most likely the application you are using does not know how to handle the former, and might have a very long timeout (often in the order of minutes) for the latter, to allow for really slow and unreliable network connections. Which application are you trying to control?

– l0b0
Apr 11 '18 at 10:00





When you reject a packet a response is sent indicating this. When you drop a packet no response is sent. Most likely the application you are using does not know how to handle the former, and might have a very long timeout (often in the order of minutes) for the latter, to allow for really slow and unreliable network connections. Which application are you trying to control?

– l0b0
Apr 11 '18 at 10:00













@l0b0 as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.

– nikrom3000
Apr 11 '18 at 10:14





@l0b0 as a testing application I have used simple netcat. Firstly, I connected client and listener, then sent the string containing forbidden one. After that, no response was received and no more lines could be sent over this connection, even when REJECT rule was used.

– nikrom3000
Apr 11 '18 at 10:14










1 Answer
1






active

oldest

votes


















0














This I hope does the gig for you. Use REJECT with tcp-reset:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT --reject-with tcp-reset


Try kmp if not worked:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo kmp --string ".." -j REJECT --reject-with tcp-reset





share|improve this answer


























  • Actually, it does not terminate Netcat listener

    – nikrom3000
    Apr 11 '18 at 15:50











  • Updated my answer. Try kmp as well.

    – MTG
    Apr 12 '18 at 11:32











  • it does not work as well. TCP connection does not reset. Tried with Netcat and python's SimpleHTTPServer now.

    – nikrom3000
    Apr 13 '18 at 19:22











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f436969%2fterminate-established-tcp-connection-using-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














This I hope does the gig for you. Use REJECT with tcp-reset:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT --reject-with tcp-reset


Try kmp if not worked:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo kmp --string ".." -j REJECT --reject-with tcp-reset





share|improve this answer


























  • Actually, it does not terminate Netcat listener

    – nikrom3000
    Apr 11 '18 at 15:50











  • Updated my answer. Try kmp as well.

    – MTG
    Apr 12 '18 at 11:32











  • it does not work as well. TCP connection does not reset. Tried with Netcat and python's SimpleHTTPServer now.

    – nikrom3000
    Apr 13 '18 at 19:22
















0














This I hope does the gig for you. Use REJECT with tcp-reset:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT --reject-with tcp-reset


Try kmp if not worked:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo kmp --string ".." -j REJECT --reject-with tcp-reset





share|improve this answer


























  • Actually, it does not terminate Netcat listener

    – nikrom3000
    Apr 11 '18 at 15:50











  • Updated my answer. Try kmp as well.

    – MTG
    Apr 12 '18 at 11:32











  • it does not work as well. TCP connection does not reset. Tried with Netcat and python's SimpleHTTPServer now.

    – nikrom3000
    Apr 13 '18 at 19:22














0












0








0







This I hope does the gig for you. Use REJECT with tcp-reset:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT --reject-with tcp-reset


Try kmp if not worked:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo kmp --string ".." -j REJECT --reject-with tcp-reset





share|improve this answer















This I hope does the gig for you. Use REJECT with tcp-reset:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo bm --string ".." -j REJECT --reject-with tcp-reset


Try kmp if not worked:



sudo iptables -A INPUT -p tcp --dport 1342 -m string --algo kmp --string ".." -j REJECT --reject-with tcp-reset






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 12 '18 at 11:32

























answered Apr 11 '18 at 11:28









MTGMTG

112




112













  • Actually, it does not terminate Netcat listener

    – nikrom3000
    Apr 11 '18 at 15:50











  • Updated my answer. Try kmp as well.

    – MTG
    Apr 12 '18 at 11:32











  • it does not work as well. TCP connection does not reset. Tried with Netcat and python's SimpleHTTPServer now.

    – nikrom3000
    Apr 13 '18 at 19:22



















  • Actually, it does not terminate Netcat listener

    – nikrom3000
    Apr 11 '18 at 15:50











  • Updated my answer. Try kmp as well.

    – MTG
    Apr 12 '18 at 11:32











  • it does not work as well. TCP connection does not reset. Tried with Netcat and python's SimpleHTTPServer now.

    – nikrom3000
    Apr 13 '18 at 19:22

















Actually, it does not terminate Netcat listener

– nikrom3000
Apr 11 '18 at 15:50





Actually, it does not terminate Netcat listener

– nikrom3000
Apr 11 '18 at 15:50













Updated my answer. Try kmp as well.

– MTG
Apr 12 '18 at 11:32





Updated my answer. Try kmp as well.

– MTG
Apr 12 '18 at 11:32













it does not work as well. TCP connection does not reset. Tried with Netcat and python's SimpleHTTPServer now.

– nikrom3000
Apr 13 '18 at 19:22





it does not work as well. TCP connection does not reset. Tried with Netcat and python's SimpleHTTPServer now.

– nikrom3000
Apr 13 '18 at 19:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • 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%2funix.stackexchange.com%2fquestions%2f436969%2fterminate-established-tcp-connection-using-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

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?