Friday, August 5, 2011

SE linux

SELinux is available in all major distros by default. During the installation of Linux [RedHat/Fedora], you will be prompted to enable or disable SELinux along with the firewall option.

In Fedora/Redhat, SELinux can be enabled or disabled by editing the file “/etc/selinux/config “. There are 3 modes for the parameter SELINUX. They are:

SELINUX = disabled
SELINUX = permissive
SELINUX = enforcing

The values ‘disabled‘ and ‘permissive‘ are mainly used to disable SELinux in a server.

The value “disabled” will completely switch off SELinux. All operations will be allowed to work normally and the policies of security attributes will not have any effect on the services or files.

The value “permissive” will allow all operations, but logs those which can be denied using the policy. All warnings get logged, thereby allowing the audit of each process interaction for creating corresponding policy rules.

IP location

You can find the IP address location from the following


============
http://www.maxmind.com/app/locate_demo_ip
============

sar

Sar
=================
1,root@testserver [~]# sar -u
Linux 2.6.18-92.1.18.el5.028stab060.8 (testserver.com) 08/03/11

00:00:01 CPU %user %nice %system %iowait %steal %idle
00:10:01 all 0.04 0.00 0.02 0.00 0.00 99.94
00:20:01 all 0.04 0.00 0.01 0.00 0.00 99.95
00:30:01 all 0.03 0.00 0.01 0.00 0.00 99.95

%user: Percentage of CPU utilization that occurred while executing at the user level (application).
%nice: Percentage of CPU utilization that occurred while executing at the user level with nice priority.
%system: Percentage of CPU utilization that occurred while executing at the system level (kernel).
%iowait: Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
%idle: Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.
==============

2,root@testserver [~]# sar -c
Linux 2.6.18-92.1.18.el5.028stab060.8 (testserver.com) 08/03/11

00:00:01 proc/s
00:10:01 67.49
00:20:01 23.24
00:30:01 14.26
00:40:01 14.31


To view process creation statistics, enter:

==============

3,root@testserver [~]# sar -b
Linux 2.6.18-92.1.18.el5.028stab060.8 (testserver.com) 08/03/11

00:00:01 tps rtps wtps bread/s bwrtn/s
00:10:01 0.00 0.00 0.00 0.00 0.00
00:20:01 0.00 0.00 0.00 0.00 0.00

tps
Total number of transfers per second that were issued to physical devices. A transfer is an I/O request to a physical device. Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size.

rtps
Total number of read requests per second issued to physical devices.

wtps
Total number of write requests per second issued to physical devices.

bread/s
Total amount of data read from the devices in blocks per second. Blocks are equivalent to sectors and therefore have a size of 512 bytes.

bwrtn/s
Total amount of data written to devices in blocks per second.
==============
sar -r

The -r argument shows free memory and swap space over time.
00:00:01 kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad
00:10:01 7729888 434288 5.32 0 0 0 0 0.00 0
00:20:01 7731636 432540 5.30 0 0 0 0 0.00 0
00:30:01 7728988 435188 5.33 0 0 0 0 0.00 0
00:40:01 7732808 431368 5.28 0 0 0 0 0.00 0
00:50:01 7734200 429976 5.27 0 0 0 0 0.00 0
Average: 7731504 432672 5.30 0 0 0 0 0.00 0
=====================



cd /var/log/sa/
-rw-r--r-- 1 root root 279024 Aug 1 23:50 sa01
-rw-r--r-- 1 root root 279024 Aug 2 23:50 sa02
-rw-r--r-- 1 root root 17664 Aug 3 01:20 sa03

sar -q -f sa01 | less

====================
root@testserver [~]# sar -P ALL
Linux 2.6.18-92.1.18.el5.028stab060.8 (testserver.com) 08/03/11

00:00:01 CPU %user %nice %system %iowait %steal %idle
00:10:01 all 0.04 0.00 0.02 0.00 0.00 99.94
00:10:01 0 0.02 0.00 0.01 0.00 0.00 99.97
00:10:01 1 0.01 0.00 0.01 0.00 0.00 99.99
00:10:01 2 0.05 0.00 0.02 0.00 0.00 99.94
00:10:01 3 0.04 0.00 0.01 0.00 0.00 99.95
00:10:01 4 0.03 0.00 0.02 0.00 0.00 99.95
00:10:01 5 0.07 0.04 0.02 0.02 0.00 99.85
00:10:01 6 0.08 0.00 0.02 0.01 0.00 99.89
00:10:01 7 0.02 0.00 0.01 0.00 0.00 99.96
====================
root@testserver [~]# sar -R
Linux 2.6.18-92.1.18.el5.028stab060.8 (testserver.com) 08/03/11

00:00:01 frmpg/s bufpg/s campg/s
00:10:01 1.46 0.00 0.00
00:20:01 0.73 0.00 0.00
00:30:01 -1.10 0.00 0.00

-R
Report memory statistics. The following values are displayed:

frmpg/s

Number of memory pages freed by the system per second. A negative value represents a number of pages allocated by the system. Note that a page has a size of 4 kB or 8 kB according to the machine architecture.

bufpg/s

Number of additional memory pages used as buffers by the system per second. A negative value means fewer pages used as buffers by the system.

campg/s

Number of additional memory pages cached by the system per second. A negative value means fewer pages in the cache.
====================
root@testserver [/var/log/sa]# sar -q
Linux 2.6.18-92.1.18.el5.028stab060.8 (testserver.com) 08/03/11

00:00:01 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
00:10:01 1 88 0.00 0.00 0.00

This reports the run queue size and load average of last 1 minute, 5 minutes, and 15 minutes. “1 3″ reports for every 1 seconds a total of 3 times.
The “blocked” column displays the number of tasks that are currently blocked and waiting for I/O operation to complete.
====================
sar -w

00:00:01 cswch/s
00:10:01 1958.80

his reports the total number of context switches per second.

===========
root@testserver [/var/log/sa]# sar -v
Linux 2.6.18-92.1.18.el5.028stab060.8 (testserver.com) 08/03/11

00:00:01 dentunusd file-sz inode-sz super-sz %super-sz dquot-sz %dquot-sz rtsig-sz %rtsig-sz
00:10:01 528134 510 196314 0 0.00 0 0.00 0 0.00
00:20:01 531924 1 197341 0 0.00 0 0.00 0 0.00

================

sar -n DEV Report network statistics.

IFACE
Name of the network interface for which statistics are reported.

rxpck/s
Total number of packets received per second.

txpck/s
Total number of packets transmitted per second.

rxbyt/s
Total number of bytes received per second.

txbyt/s
Total number of bytes transmitted per second.

rxcmp/s
Number of compressed packets received per second (for cslip etc.).

txcmp/s
Number of compressed packets transmitted per second.

rxmcst/s
Number of multicast packets received per second.
========================



II: SAR: It dispalys the cpu utility

=========================
From the sar command we will get the following informations:

1. CPU utilization
2. Memory paging and its utilization
3. Network I/O, and transfer statistics
4. Process creation activity
5. All block devices activity
6. Interrupts/sec etc.
=========================

contents of sar command /usr/sbin/sar
symbolic link to the sar command. /bin/sar
log file /var/log/sa
==========================

tcpdump

*. tcpdump command is also called as packet analyzer.
*. TCPdump is a tool we can use for packet analysis.
*. TCP dump is software that allows us to see inside the traffic
activity that occurs on a network. TCPdump is a Unix tool used to gather data from the
network, decipher the bits, and display the output in a human readable format (granted it does
take a little bit of instruction to learn the TCPdump language).


Commands:
====================================
1. To select an interface type:

tcpdump -i eth0

where eth0 is the interface
=====================================

2. To select the type of traffic you want to watch you can just specify after your interface. For
now we want to see TCP traffic.

tcpdump -i etho tcp
====================================

3.TCPdump gives us the option to dump the records into binary format to read later with
TCPdump. We do this using the –w filename option.

TCPdump -i eth0 -F myfilter.txt -w LSOoutput

And to read that file back in we use the –r filename option, gee that makes sense; read = –r
& write = –w.

TCPdump -i eth0 -F myfilter.txt -r LSOoutput

====================================

4.Reading TCPdump Output

Here is an example record:
20:08:41.313149 rootwars.org.1086 > 66.102.9.104.80: S
1192278531:1192278531(0) win 1638
-
a) 20:08:41.313149 This is the time stamp in the format of two digits for hours, two
digits for minutes, two digits for seconds, and six digits for fractional parts of a
second.

b) rootwars.org This is the source host name. The default behavior is to resolve the
hostname but you can turn it off with the TCPdump –n option. If you don’t see a
DNS name the IP will appear. Something like IP COMPUTERNAME.
c) 1086 This is the source port number or port service.
> This is a marker to indicate direction flow going from source to destination.
66.102.9.104 This is the desintation host name or IP address.
d) 80 This is the desination port number or maybe it will be translated ad HTTP.
e) S This is the TCP Flag. The S represents a SYN Flag (see the next section).
f) 1192278531:1192278531(0) This is the beginning TCP sequence number: ending
TCP sequence number (data bytes). Sequence nubers are used by TCP to order the
data received. The initial sequence number (ISN) is selected as a unique number to
mark the first byte of data. The ending sequence number is the beginning sequence
plus the number of bytes being sent with this TCP segment. In this case there were
zero bytes sent, the beginning and ending sequence numbers are the same.
win 1638 This is the receiving buffer size in bytes of rootwars.org for this connection.
======================================

5. TCP Flags in TCPdump


1. SYN - S - Session establishment request which is the first part of any TCP connection (3 way handshake).

2. ACK - ack - Ack flag is generally used to acknowledge the receipt of data from the sender. Might be in conjunction
with other flags.
3. FIN - F - Fin flag is generally used to indicate the sender’s intention to gracefully terminate the sending host’s
connection to the receiving

4. RESET - R - Reset flag is generally used to indicate the sender’s intention to immediately abort the existing
connection wit the receiving

5. PUSH - P - Push flag is generally used to immediately “push” data from the sending host to the receiving host. This is for
applications like telnet

6. URGENT - urg - Urgent flag is generally used to mean that there is “urgent” data that takes precedence over other data.

7. Placeholder - . - If the connections does not have a SYN, FIN, RESET,or PUSH flag set, a
placeholder (a period: .)will be found after the destination port

========================================

When you execute tcpdump command without any option, it will capture all the packets flowing through all the interfaces. -i option with tcpdump command, allows you to filter on a particular ethernet interface.

Some examples for tcpdump command :

1.tcpdump -i eth1 : In this example, tcpdump captured all the packets flows in the interface eth1 and displays in the standard output.

2.tcpdump -c 2 -i eth0 : When you execute tcpdump command it gives packets until you cancel the tcpdump command. Using -c option you can specify the number of packets to capture.This tcpdump command captured only 2 packets from interface eth0.

3.Capture the packets and write into a file using tcpdump -w

: tcpdump allows you to save the packets to a file, and later you can use the packet file for further analysis.
-w option writes the packets into a given file. The file extension should be .pcap, which can be read by any network protocol.

4. Display Captured Packets in ASCII using tcpdump -A

: The following tcpdump syntax prints the packet in ASCII.
$ tcpdump -A -i eth0

5.Display Captured Packets in HEX and ASCII using tcpdump -XX

: Some users might want to analyse the packets in hex values. tcpdump provides a way to print packets in both ASCII and HEX format.
$tcpdump -XX -i eth0

6. Capture packets with IP address using tcpdump -n

: In all the above examples, it prints packets with the DNS address, but not the ip address. The following example captures the packets \ and it will display the IP address of the machines involved.
: $ tcpdump -n -i eth0

7. Capture packets with proper readable timestamp using tcpdump -tttt

: $ tcpdump -n -tttt -i eth0

8. Read packets longer than N bytes

: $ tcpdump -w g_1024.pcap greater 1024

9. Receive only the packets of a specific protocol type

: You can receive the packets based on the protocol type. You can specify one of these protocols — fddi, tr, wlan, ip, ip6,arp,rarp, decnet, tcp and udp. The following example captures only arp packets flowing through the eth0 interfac

: $ tcpdump -i eth0 arp

10. Read packets lesser than N bytes

: You can receive only the packets lesser than n number of bytes using a filter ‘less’ through tcpdump command

: $ tcpdump -w l_1024.pcap less 1024

11.Receive packets flows on a particular port using tcpdump port:

: If you want to know all the packets received by a particular port on a machine, you can use tcpdump command as shown below.

: $ tcpdump -i eth0 port 22

12. Capture packets for particular destination IP and Port:

: The packets will have source and destination IP and port numbers. Using tcpdump we can apply filters on source or destination IP and port number. The following command captures packets flows in eth0, with a particular destination ip and port number 22.

: $ tcpdump -w xpackets.pcap -i eth0 dst 10.181.140.216 and port 22

13. Capture TCP communication packets between two hosts

: If two different process from two different machines are communicating through tcp protocol, we can capture those packets using tcpdump as shown below.
: $tcpdump -w comm.pcap -i eth0 dst 16.181.170.246 and port 22

14. tcpdump Filter Packets – Capture all the packets other than arp and rarp

: In tcpdump command, you can give “and”, “or” and “not” condition to filter the packets accordingly.

: $ tcpdump -i eth0 not arp and not rarp
==================

some examples:

tcpdump -npi eth0
9941 2011-08-02 13:58:03 tcpdump -nnpi eth0 not dst port 22
9942 2011-08-02 13:58:47 tcpdump -nnpi eth0 not dst port 22 and src port 22
9943 2011-08-02 13:59:23 tcpdump -nnpi eth0 and not dst port 22 and src port 22
9944 2011-08-02 13:59:31 tcpdump -nnpi eth0 and not dst port 22
9945 2011-08-02 13:59:44 tcpdump -nnp and not dst port 22
9946 2011-08-02 13:59:51 tcpdump -nnp not dst port 22
9947 2011-08-02 14:00:54 tcpdump -nnp not dst port 22 and not src port 22
9948 2011-08-02 14:01:23 tcpdump -nnp -tttt -A not dst port 22 and not src port 22

tcpdump -nnp -tttt -A not dst port 22 and not src port 22 -S

=============================================

tcpdump command examples

tcpdump -nni eth0

tcpdump -nni eth0 host 10.0.0.100

tcpdump -nni eth0 dst host 10.0.0.100 and proto tcp

tcpdump -nni eth0 src net 10.0.0.0/24 and

proto tcp and portrange 1-1024
-nn = don’t use DNS to resolve IPs and display port numbers

-i = interface to watch: lo or eth0 or venet0 (virtual machines)
dst = watch only traffic destined to a net, host, or port
src = watch only traffic whose src is a net, host, or port
net = specifies a network 10.0.0.0/24
host = specifies a host,10.0.0.100
port = specifies a port also portrange
roto = protocol ie tcp udp icmp


tcpdump -s0 -A -nni eth0 dst host 10.0.0.100
tcpdump -s0 -A -nni eth0 dst host 10.0.0.100 and dst port 80

tcpdump -s0 -A -nni eth0 dst host 10.0.0.100 and dst port 80 and

src net 10.100.0/24

tcpdump -s0 -A -nni eth0 dst net 10.0.0.0/24

tcpdump -s0 -A -nni venet0 not port 22 and dst host 10.0.0.100

and not src net 10.20.20.0/24 and not host 10.10.10.10

and src net 10.50.0.0/24
-s0 = Setting snaplen to 0 means use the required length

to catch whole packets.
-A = Print each packet (minus its link level header) in ASCII.


tcpdump -vv -c10000 -s0 -A -w hack3rcon.pcap -nni eth0 not port 22

-c = count of packets to display for exiting

-
vv = displays number of packets captured

-
w = Write the raw packets to file
# you can’t limit the size of the pcap, only the packets count
# use -c & -w together so you don’t fill up your HD.
tcpdump -s0 -A -nn -r hack3rcon.pcap and port 80

-r = read from file

Basic Usage Examples:
View Basic Network communication
tcpdump -nS (Don't resolve DNS names, print the absolute sequence numbers)

View Basic Network communication, with added verbosity
tcpdump -nnvvS (Don't resolve DNS or Port names, be more verbose when printing info, print the absolute sequence numbers)

View Network Communication Payloads in HEX
tcpdump -nnvvXS (Same as above, but this time prints the packets payload in HEX)

View Detailed Packet Information
tcpdmp -nnvvXSs 1514 (Same as above, this time we are specifying a packet length with -s 1514)


As you can see running the above on a busy network will produce loads of network traffic information.
This can be close to impossible to interpret as-is.. Tcpdump has a wonderfull thing called 'expressions'.
Using the tcpdump expressions we can remove all of the traffic we do not wish to see andonly view exactly
what we are looking for.




--------------------------------------.
0xIII TCPDump Expression: /
------------------------------------'

The true network ninja will have mastered these expressions to unleash the true power of tcpdump.
Tcpdump expressions come in three main types, those are as follows: type, dir and proto. The type
options beloging to these types are as follows: host, net and port.

The packet direction is specified by using dir, with this directive you can use the src, dst, src
or dst and src and dst options. Below are some examples of using each of these.

host - Looks for traffic based on the specified IP address, this can also be a valid dns name if the
-n options is not specified.
tcpdump host 192.168.1.1

src,dst - Looks for traffic from a specific source or destination.
tcpdump src 192.168.1.2
tcpdump dst 192.168.1.3

net - Looks for traffic from an entire CIDR range.
tcpdump net 192.168.1.0/24

proto - Looks for the type of traffic specified. proto does not need to be specified.
tcpdump tcp
tcpdump udp
tcpdump icmp

port - Looks for traffic to or from specified port. Port names can be specified by there name or numeric value.
tcpdump port 22 or tcpdump port ssh

src port, dst port - Looks for traffic based on the source or destination ports.
tcpdump src port 1025
tcpdump dst port 22

As you can see tcpdump expressions are fairly powerfull in breaking down the types of traffic we would like to see.
Now we will look into the real funky comadema that lies within tcpdump. Tcpdump has some cool features that will
allow you to combine these expresions to create even more detailed, and specific information related to traffic on
the wire. Tcpdump supports three different combinations to perform these advanced expressions, if your are a c0de
m0nkey then these will be nothing new to see... move along ....

what is loadaverage

It is the average number of processes waiting in queue to be executed by the CPU over the specified period of time.

First off, there are three load averages. From left to right there is, 1 minute load average, 5 minutes load average and 15 minutes load average.

Different Top commands

1. Top : to see the load

2. IfTop : This one will help you know who currently uses the bandwidth of your server. You will see the IP address and hostname of the other party as well as the data transfer rates in and out of these connections. Finally, at the bottom you have peaks, current rates and cummulative data transfers, in and out, and with the 1, 5 and 15 minutes averages…

3. MyTop : This one is fairly simple. It show top SQL requests made to the MySQL server, what the request is exacly (INSERT, UPDATE, DELETE, etc.), who is the user making the request, on which database and for how much time (in seconds) the query has been running.

4. This last one is simply to monitor the number of requests made to Apache, the quantity of data processed by Apache, the files currently being downloaded and such. Once again, it is a great tool to monitor live stats about your apache server.

suspicious process finding

We can use the following command to check the processes that may be using bash shells. When you get a server that seems to be hacked run the below script to check for suspicious porcesses. This will give the path to the scripts that are running curently. This command will give us the currnet working directory of all the process and it will be stored to the file /root/cwd. We can check thsi file for any suspicious process run from the user home directory. Eg given below.

root@navigator [/]# for i in `ps ax | awk {'print $1'} | grep -v PID`; do lsof -p $i | grep cwd; done > /root/cwd

root@navigator [/]# grep home /root/cwd | grep -v mail
bash 1038 root cwd DIR 8,8 4096 15270308 /home/bpcin/public_html
bash 7398 root cwd DIR 8,8 4096 7930070 /home/logger/public_html
crond 21970 madolphi cwd DIR 8,8 4096 95551493 /home/madolphi
php 21973 madolphi cwd DIR 8,8 4096 95551493 /home/madolphi
mysqld 22751 mysql cwd DIR 8,8 69632 78479361 /home/mysql
bash 24774 root cwd DIR 8,8 4096 41418821 /home/bytesil/public_html/images
crond 27996 madolphi cwd DIR 8,8 4096 95551493 /home/madolphi
php 28012 madolphi cwd DIR 8,8 4096 95551493 /home/madolphi
This will also contain the pop process for a mailbox.
eg:
pop3d 4789 morpheww cwd DIR 8,8 4096 8454332 /home/morpheww/mail/morpheusworldwide.com/operations
We can ignore this, so I used "grep -v mail". But you should check the all lines inside the file /root/cwd for a detailed check. If any malicious process is there with name mail, it will be skipped in the above command.


Let me explain some proceeses you see in this output. The processes running below are related to the cronjob of the user madolphi. You can see these processes are using the php and crond binaries. These are not dangerous.
crond 21970 madolphi cwd DIR 8,8 4096 95551493 /home/madolphi
php 21973 madolphi cwd DIR 8,8 4096 95551493 /home/madolphi
root@navigator [/home/bpcin/www]# crontab -lu madolphi
MAILTO="madolphi"
*/15 * * * * /usr/local/bin/php -q /home/madolphi/public_html/followunfollowscript.php
bash 1038 root cwd DIR 8,8 4096 15270308 /home/bpcin/public_html
The above line means a user with root permission has been ssh'ed into the server and is standng in the path /home/bpcin/public_html This could be you itslef.To make sure of this you can run the followng command.

ps aux | grep PID

Suspicious processes finding:
##################################################################
See this entry in the file /root/cwd
php 14362 homerec cwd DIR 8,8 4096 25101348 /home/homerec/public_html/my
root@navigator [/] cd /home/homerec/public_html/my/
root@navigator [/home/homerec/public_html/my]# ps ax | grep homerec
6555 pts/5 R+ 0:00 grep homerec
14362 ? S 0:02 /usr/bin/php /home/homerec/public_html/my/cp3.php
16559 ? S 0:02 /usr/bin/php /home/homerec/public_html/my/cp3.php
23391 ? S 0:02 /usr/bin/php /home/homerec/public_html/my/cp3.php


root@navigator [/home/homerec/public_html/my]# cat /home/homerec/public_html/my/php.ini
safe_mode = OFF
disable_functions = NONE

---> /home/homerec/public_html uses wordpress. php.ini is used to make the safe_mode Off.

root@navigator [/home/homerec/public_html/my]# head /home/homerec/public_html/my/cp3.php

/*||||||||||||||||||||||||||||||||||||||||||||*/
# Coded By Crazy_Hacker |
# Script: Cpanel + FTP Cracker |
# Site: www.0day.com |
# Forums: http://forums.0day.com/index.php |
/*|||||||||||||||||||||||||||||||||||||||||||*/


root@navigator [/home/homerec/public_html/my]# ll | grep php
-rw-r--r-- 1 homerec homerec 13063 May 6 10:47 cp3.php
-rw-r--r-- 1 homerec homerec 73 May 6 10:46 php.ini
-rw-r--r-- 1 homerec homerec 90553 May 6 10:47 phxdomain.php
##################################################################



##################################################################

Another entry:

bash 24774 root cwd DIR 8,8 4096 74842180 /home/amish/public_html

root@navigator [/home/amish/public_html]# lsof -p 24774
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 24774 root cwd DIR 8,8 4096 15305065
/home/uberscap/public_html/cache/high/wunderbar_emporium
bash 24774 root rtd DIR 8,6 4096 2 /
bash 24774 root txt REG 8,6 801512 880457 /bin/bash
##################################################################


##################################################################
bash 24774 root cwd DIR 8,8 4096 41418821 /home/bytesil/public_html/images

root@navigator [/home/waitwhat]# head
/home/bytesil/public_html/images/new.php
GIF89a;
// ketek90@gmail.com
// no malware on this code, you can check it by yourself ;-)
##################################################################

Difference betweeen 0.0.0.0 and 255.255.255.255

0.0.0.0 represents "any address". If you bind a listening socket to 0.0.0.0, you're telling the OS to accept connections on any ip address that the host has network adapters bound to.

255.255.255.255 (INADDR_BROADCAST) is the broadcast address for your LAN segment.