socket

Socket From Hackepedia

Sockets are an API for IPC or network communication with a process. For IPC Unix domain sockets are used, for network communication INET sockets are preferred. INET sockets work on OSI layer 3 and above. Lower layer access is provided by bpf the Berkeley packet filter. Sockets provide descriptors to a process with which data or control data can be exchanged with the kernel.

Unix domain sockets

When a Unix domain socket is set up it is bound to the local systems filesystem. The path it can be bound to is limited to 103 characters (see /usr/include/sys/un.h) instead of the filesystem limit of 1023 characters. This means that a socket should be set up close to the root perhaps in /tmp (as sshd does). Unix domain sockets make preferred IPC in OpenBSD because of the availability of the getpeereid syscall which allows a daemon to check the credentials of who is connecting to the socket. A socket in the filesystem looks like this:
$ ls -l /tmp/ssh*
total 0
srwxr-xr-x  1 pbug  wheel  0 Oct  8 11:27 agent.1327
notice the 's' indicating that this file is a socket.

INET sockets

In order to communicate with the Internet a program can communicate with it via the Kernel which has a built-in internet stack. Common protocols that one can talk via sockets are TCP and UDP as well as ICMP which is grouped into the raw mode of sockets. When a program is a TCP server the common sequence of syscalls are socket(2), bind(2), listen(2), and accept(2). When a program is a TCP client the common sequence of syscalls are socket(2), connect(2). TCP and UDP sockets have ports to identify them. On a system one can use netstat to see this. In UNIX only root can bind to ports less than 1024 the rest is available for all users. This is shown here as an example:
$ id
uid=1000(pbug) gid=1000(pbug) groups=1000(pbug), 0(wheel), 5(operator) 
$ nc -l 1023
nc: Permission denied
$ nc -l 1024
^C
$ 
When a server is listening on a certain port it's difficult to regulate who connects to this port. Early implementations messed with TCP Wrappers which allowed one to set up a simple whitelist or blacklist of who can connect. This didn't cover UDP though and it was still required that the program accepted the connection before closing it. This means that someone could stealth scan a port and know that it was listening. Firewalls allowed finer control and aren't as revealing over open ports. BSD has ipfw, ipfw2, ipf and pf as firewalls.
$ fstat | grep traceroute
pbug traceroute 18184 wd /usr 6310091 drwxr-xr-x r 2048
pbug traceroute 18184 0 / 84995 crw--w---- rw ttyp1
pbug traceroute 18184 1 / 84995 crw--w---- rw ttyp1
pbug traceroute 18184 2 / 84995 crw--w---- rw ttyp1
pbug traceroute 18184 3* internet raw icmp 0xfffffe800f85d678
pbug traceroute 18184 4* internet raw reserved 0xfffffe801bc73688
Raw sockets are restricted to the superuser (root) only. When writing to raw sockets you can manipulate the IP header which you cannot do with simple TCP or UDP sockets (setsockopt are an exception but require superuser (root) permissions). The traceroute program uses raw sockets in a UNIX system. It is possible to read from raw sockets, ie. if you specified a specific protocol you can read any packets that arrive for that protocol. In BSD and if the protocol is ICMP the ICMP types echo request (8), timestamp request (13) and address mask request (17) are not passed from the kernel to the socket. Similarely TCP and UDP protocols are not passed to a raw socket, these must use stream or dgram sockets or be read from the OSI datalink layer (see bpf).

Divert sockets

FreeBSD has a way to read/manipulate/write OSI layer 3 packets which have been diverted from the ipfw software. This type of socket is limited to FreeBSD.

Packet sockets

Linux has a way to read OSI layer 2 frames by means of SO_PACKET sockets. This is limited only to Linux.

How do I see Internet traffic

A packet sniffer is a tool that allows one to see packets carried through the network interface of a host computer.

Sockets sind ein API fĂr IPC (Inter Prozess Kommunikation) oder Netzwerk Kommunikation mit einem Prozess. FĂr IPC werden UNIX domain sockets benutzt, fuer Netzwerk Kommunikation werden INET sockets bevorzugt. INET sockets funktionieren auf OSI Schicht 3 und hoeher. Untere Schichten sind mit bpf dem Berkeley Packet filter manipulierbar. Sockets geben einem Prozess Deskriptoren mit Data oder Kontroll-Data die mit dem Kernel gehandelt werden.

UNIX domain sockets

Ein UNIX domain socket wird immer nur mit dem Dateisystem verbunden. Der Pfad mit dem es verbunden wird ist limitiert auf 103 Character (siehe /usr/include/sys/un.h) was ziemlich weniger ist als die UNIX Dateisystem Grenze von maximal 1023 Charaktern. Das heisst das ein UNIX domain socket in der naehe der Wurzeldatei gesetzt werden sollte, vielleicht in /tmp (wie sshd es macht). UNIX domain sockets sind beliebter in OpenBSD weil OpenBSD mit dem getpeereid() System Aufruf, die Rechte des Benutzers pruefen. Ein Socket im UNIX Dateisystem sieht so aus:
$ ls -l /tmp/ssh*
total 0
srwxr-xr-x  1 pbug  wheel  0 Oct  8 11:27 agent.1327
Beachten Sie bitte das 's' das der Indikator dafuer ist das dies ein UNIX domain socket ist.

INET sockets

Um mit dem Internet zu Kommunizieren muss ein Programm mit dem Kernel reden was einen eingebauten Internet-stack hat. Oft benutzte Protokolle mit denen man mit Sockets E/A betreiben kann sind TCP, UDP und ICMP (was in den "raw" modus Gruppe von Sockets faellt). Damit ein Programm ein TCP server wird werden diese folgenden System Aufrufe benutzt: socket(2), bind(2), listen(2), und accept(2). Wenn ein Programm ein TCP client wird dann werden die System aufrufe socket(2), und connect(2) benutzt. TCP und UDP haben Portnummern um Kommunikation mit einem Programm zu identifizeren. Auf einem Betriebssystem kann man das Netstat Dienstprogramm dazu benutzen um die Portnummern zu sehen. In UNIX nur root kann sich anwickeln (bind) and die Portnummern unter 1024, sonst ist der Rest erhaeltlich fuer alle Benutzer des Systems. Hier ist ein bind beispiel:
$ id
uid=1000(pbug) gid=1000(pbug) groups=1000(pbug), 0(wheel), 5(operator) 
$ nc -l 1023
nc: Permission denied
$ nc -l 1024
^C
$ 
Wenn ein Server auf einer bestimmten Portnummer lauscht ist es nicht leicht einzuteilen wer zu diesem Port sich Verbindet. Fruehe implementationen haben mit TCP Wrappers herum gespielt die es erlaubten nach einer weissen oder schwarzen Liste die Verbindung zu regulieren. Das hat natuerlich nicht mit UDP sockets funktioniert und mit UDP was es immer noch Pflicht das Paket aufzunehmen bevor es abgebrochen wurde. Das heisst das jemand einen Stealth Scan machen kann und weiss das es lauscht. Firewalls erlaubten eine feinere Kontrolle und sind nicht so offen gegenueber offenen Ports. BSD hat ipfw, ipfw2, ipf, pf und npf als Firewalls.
$ fstat | grep traceroute
pbug traceroute 18184 wd /usr 6310091 drwxr-xr-x r 2048
pbug traceroute 18184 0 / 84995 crw--w---- rw ttyp1
pbug traceroute 18184 1 / 84995 crw--w---- rw ttyp1
pbug traceroute 18184 2 / 84995 crw--w---- rw ttyp1
pbug traceroute 18184 3* internet raw icmp 0xfffffe800f85d678
pbug traceroute 18184 4* internet raw reserved 0xfffffe801bc73688
Raw sockets sind nur fuer den Superuser (root) erlaubt. Wenn man mit einem raw Socket schreibt kann man die IP Kopfzeilen (header) manipulieren was man nicht machen kann mit einfachen TCP oder UDP sockets (setsocktopt() ist ein ausfall aber man muss wiederrum root sein). Das Traceroute Programm benutzt RAW sockets in einem UNIX system. Es ist auch moeglich von einem RAW socket zu lesen, wobei man das Protokoll angibt und man kann dann dieses Protokoll lesen. In BSD wenn dieses protokoll ICMP ist, koennen nicht die ICMP typen echo request (typ 8), timestamp request (typ 13), und address mask request (typ 17) vom Kernel gelesen werden (es filtert diese). Auch bei TCP oder UDP protokollen kann man mit einem RAW socket nicht lesen, diese muessen einen SOCK_STREAM oder SOCK_DGRAM socket benutzen oder von der OSI Schicht 2 gelesen werde (siehe BPF).

Divert sockets

FreeBSD und OpenBSD haben jetzt einen Divert socket wobei man die OSI Schicht 3 lesen und schreiben kann. Man benutzt eine firewall um divert sockets dem Programm zu uebergeben. (dieses ist mit dem englischen nicht ueberein da, die zeiten sich geaendert haben).

Packet sockets

Mit Linux kann man OSI Schicht 2 Pakete/Frames lesen. Der SO_PACKET socket ist limitiert nur zu Linux.

Wie sehe ich Internet Verkehr?

Mit einem Paket Schnueffler (sniffer) kann man Pakete im Netz analysieren.