LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 02-17-2017, 06:49 AM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
Is it possible to detect a portscan with nc or a socket listener?


Is it possible to detect a portscan with nc or a socket listener?

I ran
Code:
nc -k -v -l 3389
On one server, then:
Code:
nmap -O 55.55.55.55
on another server, and nmap detected the port as open, but nc didn't display anything.

If it did a telnet from the 2nd server it did show up on netcat however.

If there a way to get netcat to display info for a port scan?

Would a stock listener in Python or Perl work any better?
 
Old 02-17-2017, 07:14 AM   #2
camp0
Member
 
Registered: Dec 2016
Location: Dublin
Distribution: Fedora
Posts: 70

Rep: Reputation: 4
Hi,

I don't think you can detect a port scanning by having just one process listening on one port. However, some port scanners do scanning at application layer, for check what type of apache version do you have for example, but from the perspective of nc I don't think you can detect a port scanning just by having one process listening in one port. Probably if you launch multiple instances of nc on different ports you can do it for sure.

Regards
 
Old 02-17-2017, 03:33 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
https://nmap.org/book/man-port-scanning-techniques.html

Quote:
SYN scan is the default[...] It is also relatively unobtrusive and stealthy since it never completes TCP connections
[...]
TCP connect scan is the default TCP scan type when SYN scan is not an option. This is the case when a user does not have raw packet privileges.[...]
Not only does this take longer and require more packets to obtain the same information, but target machines are more likely to log the connection. A decent IDS will catch either,[...]
IDS == Intrusion Detection System.
 
Old 02-17-2017, 03:35 PM   #4
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
What's a more basic way to log it rather than using an IDS? IE. there must be a way to do it with a single script, since an IDS doesn't have to be built into the kernel or anything.
 
Old 02-17-2017, 05:19 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I guess tcpdump might work.
 
Old 02-19-2017, 05:35 PM   #6
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
What kind of socket listener does tcpdump use then? If must be utilizing some C++ function.,
 
Old 02-19-2017, 06:13 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
pcap
 
Old 02-19-2017, 06:44 PM   #8
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by ntubski View Post
Is there a pcap library for Python, that would detect syn traffic?
 
Old 02-19-2017, 07:34 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by abefroman View Post
Is there a pcap library for Python, that would detect syn traffic?
I'm sure there are. The key is to find something that lets you work with raw sockets, so can go beneath the TCP abstraction (AFAIK, pcap does allow this).

Last edited by ntubski; 02-19-2017 at 07:35 PM. Reason: add link for raw sockets
 
Old 02-19-2017, 07:54 PM   #10
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Is it possible to do it with socket.socket?

I have the following but it seems to be picking up all traffic, rather than just port 5454, nor does the script show up on netstat on port 5454. (And it does seem to be detecting syn traffic).

Code:
import socket

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
except socket.error as e:
    print('Socket creation failed. Error Code {} Message {}'.format(str(e[0]),str(e[1])))
    sys.exit()

#Include IP headers
server_address = ('11.11.11.11', 5454)
s.bind(('11.11.11.11', '5454'))
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
while 1:
  packet = s.recvfrom(65565)
  print(packet)

Last edited by abefroman; 02-19-2017 at 08:48 PM.
 
Old 02-20-2017, 07:52 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by abefroman View Post
Is it possible to do it with socket.socket?

I have the following but it seems to be picking up all traffic, rather than just port 5454, nor does the script show up on netstat on port 5454.
Yes, that's because the port number is part of the TCP abstraction. You should be able to use socket.setsockopt() to add filters as described here: https://www.kernel.org/doc/Documenta...ing/filter.txt
 
Old 02-20-2017, 08:07 AM   #12
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
you can detect portscans using iptables, but you'd need to work around any current rules and use either the recent or limit modules. You can also mix in ipsets for more fun on it and iptables does have the ability to log activity too. It is the way I'd do it on a production server.
 
Old 02-20-2017, 09:26 AM   #13
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by ntubski View Post
I'm sure there are. The key is to find something that lets you work with raw sockets, so can go beneath the TCP abstraction (AFAIK, pcap does allow this).
If you're talking about using accept_filter, that probably won't work because listen() needs to be called first. Especailly since it is a connectionless system (SYN / followed by reset).
 
Old 02-20-2017, 07:25 PM   #14
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
It seems I either have to pull the port out of the header and use an if statement, or scapy.sniff with a filter for the port.

Would one method be better than the other?
 
Old 02-20-2017, 07:43 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by abefroman View Post
It seems I either have to pull the port out of the header and use an if statement, or scapy.sniff with a filter for the port.

Would one method be better than the other?
You're referring to http://www.secdev.org/projects/scapy....html#sniffing? I guess it's a nicer interface to the kernel filter I linked to before. That method would probably be better in terms of efficiency.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How could server detect closed client socket using TCP and c++? Lobinho Programming 6 08-10-2010 02:28 PM
IPTables portscan detection introuble Linux - Security 3 04-30-2008 04:01 AM
Snort/base and portscan El Fluffo Linux - Security 0 11-03-2007 03:20 PM
How to detect lost socket connection sasha_baranov Programming 4 02-16-2005 11:46 PM
portscan from my own machine groegert Linux - Security 1 07-12-2001 09:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 06:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration