Linux Güvenliği C. Ahmet Mercan Güvenlik - Temel Güvenlik Açıkları - Port Tracing, Ping, SSH Saldırıları, Linux'ta Güvenlik, Firewall Yapısı ve Basitce SeLinux
Öneri RedHat Security Guide RedHat 7: https://access.redhat.com/documentation/en- US/Red_Hat_Enterprise_Linux/7/html/Security _Guide/index.html RedHat 6: https://access.redhat.com/documentation/en- US/Red_Hat_Enterprise_Linux/6/html/Security _Guide/index.html
Öneri Top 20 OpenSSH Server Best Security Practices http://www.cyberciti.biz/tips/linux-unix-bsdopenssh-server-best-practices.html
Güvenlik Bilgisayar Güvenliği: Fiziksel Güvenlik Yazılımsal / Elektronik Güvenlik Ağ Güvenliği İşletim Sistemi Güvenliği Güvenlik Modeli: Sadece Dışa karşı Güvenlik (Yumurta Modeli) Çok katmanlı Güvenlik
Güvenlik Dünyada iki tip kurum vardır: Hacklendigini bilenler, ve bilmeyenler
İnsan Paradoksu Birçok Güvenlik Tedbiri, Saldırganlardan çok diğer insanların hayatını zorlaştırır. İnsanlar ise hayatlarını kolaylaştırmak için güvenliği kolayca ihmal ederler. Güvenlik tedbirleri hayatı zorlaştırdıkca, insanlar daha fazla güvenlik ihmali yapar. Bu sebeble mümkün olduğunca insanların hayatını zorlaştırmadan güvenlik sağlanmalıdır.
Yapılacaklar Listesi 'Hack' lenmeden Önce: Önleyici Güvenlik 'Hack' lenirken: Olaylardan Haberdar Olma
Önleyici Güvenlik Güvenli ama Basit bir Yapının Planlanması Sağlıklı Durumun Bilinmesi Bilinen Açıkların Giderilmesi Gereksiz Programların Kaldırılması Gereksiz İzinlerin Kapatılması Sızma (penetrasyon) Testleri Yedekleme Docker, puppet vb. Otomatik ayarlama araçları
Olaylardan Haberdar Olma İzleme (monitoring) Ganglia, nagios Kayıt Tutma (logging) Yeni Bulunan Açıklardan Haberdar Olma http://www.cert.org/ http://www.securityfocus.com/ Hacklendim mi?
Ağ (Ethernet) Paketi Ethernet Paketinin kabaca yapısı Alıcının Mac Adresi Switch için gerekli Göndericinin Mac Adresi IP Paketi IP Paketinin kabaca yapısı Alıcının IP Adresi Göndericinin IP Adresi TCP Paketi Router için gerekli TCP Paketinin kabaca yapısı Alıcının Port Numarası Göndericinin Port Numarası Uygulama Paketi Bilgisayar için gerekli
Port Numarası Gelen Paketler dns paketi Udp Port 53 Ssh paketi Tcp Port 22 html paketi Tcp Port 80? paketi Tcp Port 2973 Smtp paketi Tcp Port 25 Karşılığı Yok Geri döner! Port 22 Açık Port 80 Açık Port 2973 Açık Karşılığı Yok Geri döner! Ssh Sunucu Web Sunucu Kullanıcıya ait Program Bilgisayar
Ping icmp protokolu ile uzaktaki bilgisayardan bir karşılık vermesini istemek için kullanılır. Genelde bilgisayar açık mı? ve ağ erişimimiz var mı? sorularını cevaplamak için kullanılır. Ancak bu hizmet firewall ile kapatılmış olabilir.
Ping $ ping www.google.com PING www.google.com (173.194.66.104): 56 data bytes 64 bytes from 173.194.66.104: icmp_seq=0 ttl=46 time=62.337 ms 64 bytes from 173.194.66.104: icmp_seq=1 ttl=46 time=64.747 ms 64 bytes from 173.194.66.104: icmp_seq=2 ttl=46 time=64.651 ms ^C
netstat Bağlı bulunduğumuz bilgisayarın ağ bağlantılarını ve açık portları gösterir. -a tüm protokolleri göster -n isim/adres yerine sayısal olarak IP/port yaz. -p portla ilgili programın adını da göster. -l sadece listening portları göster. -u sadece udp portları göster. -t sadece tcp portları göster.
netstat -pna Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4593/mysqld tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 4447/sshd tcp 0 0 127.0.0.1:2207 0.0.0.0:* LISTEN 4409/python
nmap Uzaktaki bilgisayarların açık portları hakkında bilgi verir. Çok çeşitli tarama metodları ile bilgi toplayabilir. -sp şeklinde tarama metodu verilir. Tek Makine yada IP bloğu tarayabilir. -O ile işletim sistemini tahmin etmeye çalışır.
nmap # nmap 10.128.1.20 Starting Nmap 4.11 Interesting ports on ex.ample.com (10.128.1.20): Not shown: 1677 closed ports PORT STATE SERVICE 22/tcp open ssh 111/tcp open rpcbind 652/tcp open unknown
nmap işletim sistemi tespiti # nmap -O 10.128.1.20 Starting Nmap 4.11 Interesting ports on ex.ample.com (10.128.1.20): Not shown: 1677 closed ports PORT STATE SERVICE 22/tcp open ssh 111/tcp open rpcbind 652/tcp open unknown
TCP Wrappers Her servis TCP wrappers'ı desteklemez. /etc/hosts.deny dosyasında ALL:ALL /etc/hosts.allow dosyasında sshd: 10.128.2.5 192.168.17.1 sendmail : 192.168.1.0/255.255.255.0 ALL: localhost
Denyhosts: Otomatik TCP Wrappers Ssh için yanlış şifre denemelerini izleyip, otomatik engelleme yapar. Ayarlanması: cd /usr/share/denyhosts cp denyhosts.cfg-dist denyhosts.cfg Sonra denyhosts.cfg dosyasını değiştirin. denyhosts.py purge komutu ile engellenen IPler silinir.
Kuvvetli Şifre Günümüzde ağ ve bilgisayar hızları arttığı için saldırganlar milyonlarca şifreyi kolayca deneyebilmekteler. Kullanıcı adının bilinen isimlerden oluşmaması tek başına önleyici bir uygulamadır. ahmet, admin, mysql, user, a gibi basit kullanıcı adları yerine kullanılmamalıdır. mercana, jsmith gibi kullanıcı adı ve soyadından oluşturulan adlar tercih edilmelidir.
Kuvvetli Şifre Şifre belirlerken sözlükte bulunabilecek ifadelerden kaçınılmalıdır. En yaygın şifre 123456 Sadece Harf ve Sayılardan oluşan şifre güvenli değildir. Şifrenin hatırlamayacağınız derecede zor olması da bir güvenlik sorunudur. Şifrenin bir Cümleden (harflerin arasına sayı ve işaretler koyarak) oluşturulması iyidir: Dx2Go 1_oLuR
Kuvvetli Şifreye Zorlamak /etc/pam.d/passwd dosyasına pwquality açılır: password required pam_pwquality.so retry=3 /etc/security/pwquality.conf dosyasında: minlen=8 şifre uzunluğu minclass=4 şifrede k.harf,b.harf,sayı,diğer olsun maxsequence=3 şifrede 123 / abc gibi dizi uzunluğu maxrepeat=3 şifrede 111 / aaa gibi tekrar adedi Şifrelere zaman sınırı koymak için: system-config-users
Ssh Ayarlamaları /etc/ssh/sshd_config dosyasında: Protocol 2 PermitRootLogin no AllowUsers *@10.8.2.2 mercan DenyUsers httpd PermitEmptyPasswords no Şifresiz ssh güvenliği artıracak şekilde kullanılabilir.
Port Numarası Gelen Paketler dns paketi Udp Port 53 Ssh paketi Tcp Port 22 html paketi Tcp Port 80? paketi Tcp Port 2973 Smtp paketi Tcp Port 25 Karşılığı Yok Geri döner! Port 22 Açık Port 80 Açık Port 2973 Açık Karşılığı Yok Geri döner! Ssh Sunucu Web Sunucu Kullanıcıya ait Program Bilgisayar
Firewall Güvenlik Duvarı dns paketi Udp Port 53 Ssh paketi Tcp Port 22 html paketi Tcp Port 80? paketi Tcp Port 2973 Smtp paketi Tcp Port 25 Firewall Firewall Karşılığı Yok Geri döner! Port 22 Açık Port 80 Açık Port 2973 Açık Karşılığı Yok Geri döner! Ssh Sunucu Web Sunucu Kullanıcıya ait Program Bilgisayar
firewalld firewall-config programı ile grafik arayüz açılır. firewall-cmd ile komut satırından ayarlamalar yapılabilir: # firewall-cmd --permanent --zone="dmz" --addrich-rule='rule family="ipv4" source address="37.247.108.75" reject'
Firewalld'i kapatmak Firewalld kapatılıp, iptables çalıştırabiliriz: # systemctl disable firewalld # systemctl stop firewalld # yum install iptables-services # systemctl start iptables # systemctl start ip6tables # systemctl enable iptables # systemctl enable ip6tables
iptables: kural eklemek iptables -A <kural-grubu> -j <işlem> Kural-grubu (chain) olarak başlangıçta INPUT, OUTPUT, FORWARD gelir ve bunlar mecburen olmalıdırlar. INPUT bilgisayara gelen paketler için olan komutlar bu gruba aittir. OUTPUT bilgisayardan giden paketler için olan komutlar için. FORWARD bilgisayarın yönlendirme yapması (router olarak çalışması) için olan komutlar için. -j ile verilen işlemler ise ACCEPT, DROP, ve REJECT.
iptables: genel kural vermek iptables -P <kural-grubu> <işlem> İlave bir şart belirtmeden tüm paketlere uygulanacak işlem. Mesela, tüm routing işlemlerini durdurmak: # iptables -P FORWARD DROP Tüm giden paketlere izin vermek: # iptables -P OUTPUT ACCEPT
iptables: kaydetmek Normalde iptables komutu ile yapılan değişiklikler çalışan iptables içindir, reboot ile gider. Kaydetmek için: /sbin/iptables-save Bu komutla /etc/sysconfig/iptables dosyası yeniden oluşturulur. Eski /etc/sysconfig/iptables ise /etc/sysconfig/iptables.save adıyla saklanır.
iptables: kural eklemek Mesela gelen 80. port paketlerine izin vermek için: # iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT Burada -p protokol, -m module, --dport destination port vermek için. Belirli bir IP için izin vermek için: # iptables -A INPUT -s 192.168.0.4 -j ACCEPT Bir IP grubuna izin vermek: # iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT # iptables -A INPUT -s 192.168.0.0/255.255.255.0 -j ACCEPT
iptables: Örnek # iptables -P INPUT ACCEPT # iptables -F # iptables -A INPUT -i lo -j ACCEPT # iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # iptables -A INPUT -p tcp --dport 22 -j ACCEPT # iptables -P INPUT DROP # iptables -P FORWARD DROP # iptables -P OUTPUT ACCEPT http://wiki.centos.org/howtos/network/iptables
iptables: Örnek Mevcut durumu görmek için: # iptables -L -v Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- lo any anywhere anywhere 0 0 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination http://wiki.centos.org/howtos/network/iptables
SELinux Linux çekirdeğindeki bir Zorunlu erişim kontrol mekanizması (mandatory access control mechanism) uygulamasıdır. Dosyalar ve programlar için tanımlı izinlerin dışına çıkılmasını engeller. Bu sayede saldırganlar bir sebeble erişim sağlasalar da selinux'da tanımlı olmayan işleri yapamazlar. Tabiki adminler de yapamaz! Problem?
SELinux Httpd sendmail kullanabilir mi? httpd_can_sendmail --> off / on Httpd diğer kullanıcıların ev dizinlerini okuyabilir mi? httpd_read_user_content --> off / on
SELinux çalışıyor mu? # sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed
SELinux ayritili durum (fakat tum dizinler gozukmuyor!) # sestatus -v SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 28 Process contexts: Current context: Init context: /usr/sbin/sshd File contexts: Controlling terminal: /etc/passwd /etc/shadow /bin/bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 system_u:system_r:init_t:s0 system_u:system_r:sshd_t:s0-s0:c0.c1023 unconfined_u:object_r:user_devpts_t:s0 system_u:object_r:passwd_file_t:s0 system_u:object_r:shadow_t:s0 system_u:object_r:shell_exec_t:s0
SELinux açma / kapama: /etc/selinux/config dosyasını değiştirip, sistemi reboot et: # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - SELinux is fully disabled. SELINUX=disabled
SELinux'u reboot etmeden açma/kapama ve durumu görme: # setenforce Permissive # getenforce Permissive # setenforce Enforcing # getenforce Enforcing
type=syscall msg=audit(1410944310.867:28504): arch=c000003e syscall=6 success=no exit=13 a0=7fff6fef3fb0 a1=7fff6fef3d00 a2=7fff6fef3d00 a3=6e696d64412f736e items=0 ppid=28432 pid=29548 auid=4294967295 uid=48 SELinux logları Normalde /var/log/messages yada yoksa /var/log/audit/audit.log dosyasında log var: type=avc msg=audit(1410944310.867:28502): avc: denied { search } for pid=29548 comm="httpd" name="adminlinks" dev="dm-1" ino=16324 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=dir type=syscall msg=audit(1410944310.867:28502): arch=c000003e syscall=6 success=no exit=-13 a0=7fff6fef1e40 a1=7fff6fef1d30 a2=7fff6fef1d30 a3=34 items=0 ppid=28432 pid=29548 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="httpd" exe="/usr/sbin/httpd" subj=system_u:system_r:httpd_t:s0 key=(null) type=avc msg=audit(1410944310.867:28503): avc: denied { search } for pid=29548 comm="httpd" name="adminlinks" dev="dm-1" ino=16324 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=dir type=syscall msg=audit(1410944310.867:28503): arch=c000003e syscall=6 success=no exit=-13 a0=7fff6fef3fb0 a1=7fff6fef3ea0 a2=7fff6fef3ea0 a3=34 items=0 ppid=28432 pid=29548 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="httpd" exe="/usr/sbin/httpd" subj=system_u:system_r:httpd_t:s0 key=(null) type=avc msg=audit(1410944310.867:28504): avc: denied { getattr } for pid=29548 comm="httpd" path="/var/www/html/w/extensions/adminlinks" dev="dm-1" ino=16324 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=dir
SELinux Kullanıcı Tanımları # semanage login -l Login Name SELinux User MLS/MCS Range Service default unconfined_u s0-s0:c0.c1023 * root unconfined_u s0-s0:c0.c1023 * system_u system_u s0-s0:c0.c1023 *
LABEL PID TTY TIME CMD system_u:system_r:init_t:s0 1? 00:03:21 systemd system_u:system_r:kernel_t:s0 2? 00:00:01 kthreadd system_u:system_r:kernel_t:s0 3? 00:00:12 ksoftirqd/0 system_u:system_r:syslogd_t:s0 453? 00:00:28 systemd-journal system_u:system_r:lvm_t:s0 476? 00:00:00 lvmetad system_u:system_r:udev_t:s0-s0:c0.c1023 487? 00:00:00 systemd-udevd Process izinlerini görmek # ps -ez
Dosya izinlerini görmek, değiştirmek # ls -Z /var/www/html/ drwxr-xr-x. www 1001 unconfined_u:object_r:httpd_sys_content_t:s0 AdminLinks drwxr-xr-x. www 1001 unconfined_u:object_r:httpd_sys_content_t:s0 Cite drwxr-xr-x. www 1001 unconfined_u:object_r:httpd_sys_content_t:s0 ConfirmEdit drwxr-xr-x. www 1001 unconfined_u:object_r:httpd_sys_content_t:s0 Gadgets drwxr-xr-x. www 1001 unconfined_u:object_r:httpd_sys_content_t:s0 ImageMap drwxr-xr-x. www 1001 unconfined_u:object_r:httpd_sys_content_t:s0 InputBox httpd_sys_content_t izinini (label) eklemek için: # chcon -R -t httpd_sys_content_t '/var/www/html/adminlinks'
SELinux boolean ayarlarını görmek # getsebool -a abrt_anon_write --> off abrt_handle_event --> off abrt_upload_watch_anon_write --> on antivirus_can_scan_system --> off antivirus_use_jit --> off auditadm_exec_content --> on authlogin_nsswitch_use_ldap --> off authlogin_radius --> off authlogin_yubikey --> off awstats_purge_apache_log_files --> off boinc_execmem --> on cdrecord_read_content --> off cluster_can_network_connect --> off cluster_manage_all_files --> off cluster_use_execmem --> off...
SELinux boolean ayarlarını değiştirmek setsebool -P allow_httpd_sys_script_anon_write on setsebool -P allow_httpd_sys_script_anon_write off SELinux default dosya ayarlarını geri döndürmek: # restorecon -Rv /var/www/html
SELinux hata mesajlarını daha anlamlı görmek: audit2allow -w -a type=avc msg=audit(1417768671.288:1364): avc: denied { write } for pid=31132 comm="httpd" name=c4b0736c6574696d2d73697374656d692e706e67 dev="dm-1" ino=351201 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=dir Was caused by: Unknown - would be allowed by active policy Possible mismatch between this policy and the one under which the audit message was generated. ones. Possible mismatch between current in-memory boolean settings vs. permanent type=avc msg=audit(1417768670.147:1359): avc: denied { write } for pid=31132 comm="httpd" name=c4b0736c6574696d2d73697374656d692e706e67 dev="dm-1" ino=351201 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=dir Was caused by: Unknown - would be allowed by active policy
Hata mesajlarından selinux ayarlarını değiştirmek: # tail -n 5 /var/log/audit/audit.log audit2allow -M postgreylocal # grep smtpd_t /var/log/audit/audit.log audit2allow -M postgreylocal # semodule -i postgreylocal.pp