lsof ile Açık Dosyaların Tesbiti

Ekim 26, 2011 § Yorum yok § Kalıcı bağlantı

İsmi k, bash, init ile başlayan proseslerin açtığı dosyalar:

 lsof -c k
 lsof -c bash
 lsof -c init

pkrumins kullanıcının başlattığı veya apache prosesinin açtığı dosyalar

lsof -u pkrumins -c apache

pkrumins kullanıcının başlattığı ve apache prosesinin açtığı dosyalar

lsof -a -u pkrumins -c bash

root kullanıcısı tarafından açılmamış açık dosyalar

lsof -u ^root

30297 pidli proses tarafından açılmış dosyalar

 lsof +p 30297

/tmp dizini ve altındaki dizinleri/dosyaları kullanan proseslerin açtığı dosyalar.

lsof +D /tmp

Bütün internet soketlerini ve 80. portu kullanan programları öğrenmek:

 lsof -i
 lsof -i :80
 lsof -i :smtp

Bütün internet ve Unix soketlerini listelemek

 lsof -i -U

TCP ve UDP proseslerini listelemek

lsof -i tcp
lsof -i udp
lsof -i udp:53
lsof -i tcp:80

lsof komutunu her saniye çalıştırmak

lsof -r 1

www.akadia.com sunucunusa UPD ile 123 portu üzerinden bağlanan prosesler

 lsof -iUDP@www.akadia.com:123

md5sum

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

md5sum x.iso >> md5sums.txt
md5sum y.iso >> md5sums.txt
cat md5sums.txt 
b42ae2a31fd39ebd59358cb7027f8304  x.iso
b44d0e2abdd53b0bde6f624060eb07f0  y.iso

md5sum kontrolü yapmak

md5sum -c md5sum.txt
x.iso: OK
y.iso: OK

Recursive md5sum kontrolü yapmak

find ./backup -type f -print0 | xargs -0 md5sum > /checksums_backup.md5
cd /orjinal
md5sum -c checksums_backup.md5

Process bar ile dosya kopyalamak

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

$ rsync -r -v --progress -e ssh root@remote-server:~/pictures /home/user/
receiving file list ...
366 files to consider
pictures/IMG_1141.jpg
      3849245 100%   32.30kB/s    0:01:56 (xfer#30, to-check=335/366)
pictures/IMG_1142.jpg
     4400662 100%   32.21kB/s    0:02:13 (xfer#31, to-check=334/366)
pictures/IMG_1172.jpg
     2457600  71%   32.49kB/s    0:00:29

netstat ile port analizi

Ekim 25, 2011 § 1 yorum § Kalıcı bağlantı

Bütün açık portları görmek (dinleyen ve veri gönderen)

# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
udp        0      0 *:bootpc                *:*

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     6135     /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     5140     /var/run/acpid.socket

TCP porları için

# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
tcp        0      0 localhost:ipp           *:*                     LISTEN
tcp        0      0 *:smtp                  *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN

UDP portları için


# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 *:bootpc                *:*
udp        0      0 *:49119                 *:*
udp        0      0 *:mdns                  *:*

Sadece dinleyen soketlerin tesbiti

# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:ipp           *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN
udp        0      0 *:49119                 *:*

Portların istatistiklerini görmek için

# netstat -s
Ip:
    11150 total packets received
    1 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    11149 incoming packets delivered
    11635 requests sent out
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
Tcp:
    582 active connections openings
    2 failed connection attempts
    25 connection resets received
Udp:
    1183 packets received
    4 packets to unknown port received.
.....

Portları kullanan programların PID bilgisi için

# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        1      0 ramesh-laptop.loc:47212 192.168.185.75:www        CLOSE_WAIT  2109/firefox
tcp        0      0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox

Verileri sayısal olarak görmek için

# netstat -an

veya
# netsat -a --numeric-ports

# netsat -a --numeric-hosts

# netsat -a --numeric-users

Sürekli olarak netstat verilerine bakmak için

# netstat -c
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED
tcp        1      1 ramesh-laptop.loc:52564 101.11.169.230:www      CLOSING
tcp        0      0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED
tcp        1      1 ramesh-laptop.loc:42367 101.101.34.101:www      CLOSING
^C

Çekirdeğin routing verisini görmek için

# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     *               255.255.255.0   U         0 0          0 eth2
link-local      *               255.255.0.0     U         0 0          0 eth2
default         192.168.1.1     0.0.0.0         UG        0 0          0 eth2

Bir programın kullandığı portu tesbit etmek

# netstat -ap | grep ssh
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        1      0 dev-db:ssh           101.174.100.22:39213        CLOSE_WAIT  -
tcp        1      0 dev-db:ssh           101.174.100.22:57643        CLOSE_WAIT  -

Netcat Kullanımı

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

Netcat network için isviçre çakısı gibidir.
Netcat temel olarak verilen porttan ilgili sunucuya bağlanır, input olarak verilen bilgiyi bu sunucuya gönderir, sunucunun cevabını ise ekrana (stdout’a) basar.

Örneğin google.com sunucusuna bağlanalim.

[xxx@xxxxx ~]$ nc www.google.com 80
HEAD / HTTP/1.0
[enter]
HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.com.tr/?gfe_rd=cr&ei=Z-uCU8XoI4Pe8geRnoG4Aw
Content-Length: 262
Date: Mon, 26 May 2014 07:21:11 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic

Aynı işi aşağıdaki gibi de yapabiliriz.

[xxxx@xxxxxx ~]$ echo -e "HEAD / HTTP/1.0\n" | nc www.google.com 80
HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.com.tr/?gfe_rd=cr&ei=POuCU5_5I9Da8ge8_ICIDw
Content-Length: 262
Date: Mon, 26 May 2014 07:20:28 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic

Netcat’i dinleyici servis olarak çalıştırmak için “-l ” kullanılabilir.
Böylece çift yönlü olarak sunucunun ilgili portuna direct edilmiş bilgiler bağlanan sunucunun ekranına basılılır.

# IP adresi 10.10.10.10 olan bilgisayarda
[xxxx@xxxxxx ~]# uptime| nc -l 6666
# şimdi başka bir bilgisayardan, 10.10.10.10 bilgisayarının 6666 portuna bağlanalim:
[xxxx@xxxxxx ~]$ nc 10.10.10.10 6666
 10:44:26 up 37 min,  4 users,  load average: 0.00, 0.02, 0.07

Bunun güzel bir uygulaması olan chat server olarak netcat kullanmak için

# IP adresi 10.10.10.10 olan bilgisayarda
$ nc -l -p 12345
veya
$ nc -l 12345

# şimdi dinleyen 10.10.10.10 bilgisayarının 12345 portuna bağlanalim:
 
$ nc 10.10.10.10 12345

İki bilgisayar arasında dosya göndermek


# Dinleyen sunucu -> bağlanan sunucu yönüde transfer
# IP adresi 192.168.1.10 olan bilgisayarda
$ cat file | nc -l -p 6666
 
# Başka bir bilgisayardan
$ nc 192.168.1.10 6666 > file

# Bağlanan sunucu -> Dinleyen sunucu yönüde transfer 
# 192.168.1.10 adresli bilgisayardan
$ nc -l -p 6666 -q 10 >output.txt
 
# Başka bir bilgisayardan
$ cat input.txt| nc 192.168.1.10 6666 

-q 10 ile 10 saniye sonra kapanması isteniyor.

A bilgisayarındaki /data dizinindeki dosyaları B bilgisayarına göndermek

# IP adresi 192.168.1.10 olan bilgisayarda
$ tar -cf - /data | nc -l -p 6666
 
# 192.168.1.10 ipli bilgisayara bağlanalim
$ nc 192.168.1.10 6666 | tar -xf -

# Diğer bilgisayardan 10.10.10.2 ipli bilgisayara erişip 12345 portundan veri gönderilip 12346 porundan veri alınabilir.

# 10.10.10.2 ipli bilgisayarda
$ nc -l 12345 | nc www.linuxwiki.net 80 | nc -l 12346
# Başka bir bilgisayardan
$ nc 10.10.10.2 12345
linux
^C
$ nc 10.10.10.2 12346
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>501 Method Not Implemented</title>
</head><body>
<h1>Method Not Implemented</h1>
<p>GET to /default.shtml not supported.<br />
</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache Server at just135.justhost.com Port 80</address>
</body></html>

Netcat ile herhangi bir process sunucu görevi görebilir.
Bunu sağlamak için -e kullanmak yeterli

$ nc -l -p 12345 -e /bin/bash

$ nc localhost 12345
ls -las
total 4288
   4 drwxr-xr-x 15 pkrumins users    4096 2009-02-17 07:47 .
   4 drwxr-xr-x  4 pkrumins users    4096 2009-01-18 21:22 ..
   8 -rw-------  1 pkrumins users    8192 2009-02-16 19:30 .bash_history
   4 -rw-r--r--  1 pkrumins users     220 2009-01-18 21:04 .bash_logout
   ...

Netcat ile ilgili portu dinleyen program hakkında bilgi edinebiliriz.


[XXXX@XXXX ~]$ nc -vvv localhost 22
Connection to localhost 22 port [tcp/ssh] succeeded!
SSH-2.0-OpenSSH_5.3

Protocol mismatch.

Aynı mantıkla netcat port scanner olarak da kullanılabilir.

[XXXX@XXXX ~]$ nc -vvv -z targetip 1-65535

Not: Netcat için bazı paketlerde kısıtlamalar getirilmiştir.
Orjinal kaynaktan yüklemek için
* Microsoft
http://www.vulnwatch.org/netcat/nc111nt.zip

* Linux/Unix/BSD
http://www.vulnwatch.org/netcat/nc110.tgz

* GNU
http://netcat.sourceforge.net/

ngrep ile networkü süzmek

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

ngrep network izleme aracıdır.

eth0 interfacinde “fatih” ifadesini içeren paketleri aramak.
İçinde fatih olmayan paketler için ”#” yazılır.

ngrep -d eth0 fatih

İçinde fatih olmayan paketler için ”#” yazılmasını istemiyorsak quiet(-q) modda çalıştıralım..

ngrep -q -d eth0 fatih

ngrep tooluna filitre koymak için

ngrep -q -d eth0 facebook "tcp port 80"
   interface: eth0 (10.1.21.0/255.255.255.0)
   filter: (ip or ip6) and ( tcp port 80 )
   match: facebook

ngrep e regexp de girilebilir

ngrep -q -d eth0 *hack* "tcp port 80"

birden fazla regexp girmek için

ngrep -q -d eth0 '*hack*| vivek' "tcp port 80"

-i ile case insensitive arama yapılabilir. -W daha temiz rapor oluşturmaya yarar.

ngrep -i 'game*|chat|recipe' -W byline > bad_user.txt
 
 
interface: wlan0 (192.168.0.0/255.255.255.0)
match: game*|chat|recipe
###############################
T 192.168.0.100:33035 -> 66.249.85.104:80 [AP]
GET<nobr> <wbr></nobr>/search?hl=en&safe=off&q=online+games&btnG=Search<nobr>&<wbr></nobr> meta= HTTP/1.1.
Host: www.google.co.in.
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.6) Gecko/20050419 OpenLX/1.7.
6-1.olx.
Accept: text/xml,application/xml,application/xhtml+xml,te<nobr>x<wbr></nobr> t/html;q=0.9,text/plain;q=0.8,
image/png,*/*;q=0.5.
Accept-Language: en-us,en;q=0.5.
Accept-Encoding: gzip,deflate.
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7.
Keep-Alive: 300.
Connection: keep-alive.
Referer: http://www.google.co.in/search?hl=en&q=hello&btnG<nobr>=<wbr></nobr> Google+Search&meta=.
Cookie: PREF=ID=7c5bf916f28d16c7:FF=4:LD=en:NR=10:TM=1118<nobr>3<wbr></nobr> 48709:LM=1118348731:S=20ZkQG0Y
sMDDsXsW. 

E-posta trafiğini izlemek için

ngrep -i 'rcpt to|mail from' tcp port smtp

interface: wlan0 (192.168.0.0/255.255.255.0)
filter: ip and ( tcp port smtp )
match: rcpt to|mail from
T 192.168.0.100:1043 -> 200.40.174.30:25 [AP]
MAIL From: SIZE=192..
T 192.168.0.100:1043 -> 200.40.174.30:25 [AP]
RCPT To:..

-q :sessiz mod(sadece match göster), -t zamanı YYYY/MM/DD HH:MM:SS.UUUUUU formatında göster, -wi login gelimesini case insensitive bul, port 23 ile telnet trafiğini dinle

ngrep -q -t -wi "login" port 23

Harici diskleri mount etmek

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

mount -t ntfs-3g /dev/sdc1 /media/sdc1 -o force

Dosyalara satır numarası vermek

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

Satır başına satır numarası girer

file1:
Jack Wallen
Jessica Wallen
Johnny Wallen
Jeri Wallen
 
$nl file1 
 
1 Jack Wallen
2 Jessica Wallen
3 Johnny Wallen
4 Jeri Wallen

xargs kullanımı

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

find . -name 'postgre*'  | xargs grep -c 172.16.1.2
./postgresql-Sat.log:12998168
./postgresql-Fri.log:34109768
./postgresql-Sun.log:10844160
./postgresql-Thu.log:12965014
./postgresql-Mon.log:23323058
./postgresql-Tue.log:31159448
./postgresql-Wed.log:12933239

find . -name 'postgre*'  | xargs wc -l
   14364681 ./postgresql-Sat.log
   40218997 ./postgresql-Fri.log
   12648770 ./postgresql-Sun.log
   17589348 ./postgresql-Thu.log
   31839835 ./postgresql-Mon.log
   38824194 ./postgresql-Tue.log
   18600810 ./postgresql-Wed.log

Video edit etmek

Ekim 25, 2011 § Yorum yok § Kalıcı bağlantı

Dosyaları aviye çevirmek:

ffmpeg -i deneme5.mpg -f avi -vcodec mpeg1video -b 1000k -acodec mp3 -ab 32k deneme5avi.mpg

Avi dosyalarını birleştirmek

avimerge -i deneme5avi.mpg deneme6avi.mpg -o son.avi

vlc ile çevirmek

vlc Machete.2010.1080P.X264.MKV.AC3.NLSubs.NLUPPER.mkv :sout='#transcode{vcodec=mp2v,vb=4096,acodec=mp2a,ab=192,scale=1,channels=2,deinterlace,audio-sync}:std{access=file, mux=ps,dst="/home/fatih/Desktop/son.mp4"}' vlc://quit

mencoder ile video dönüştürmek:
Öncelikle
optimal_bitrate = 50 * 25 * width * height / 256, başdaki 50, 40 ile 60 arasında olabilir.

Not:msmpeg4v2 kodegi Windows için herhangi bir program kurmadan gösterilebilir. mpeg4 kodegi Windows’da Divx kurulumunu gerektirir.

opt="vbitrate=2160000:mbd=2:keyint=132:v4mv:vqmin=3:lumi_mask=0.07:dark_mask=0.2:scplx_mask=0.1:tcplx_mask=0.1:naq"
mencoder -oac mp3lame -lameopts cbr:mode=2:br=128 -ovc lavc -lavcopts vcodec=mpeg4:vpass=1:$opt -o /dev/null input.avi
mencoder -oac mp3lame -lameopts cbr:mode=2:br=128 -ovc lavc -lavcopts vcodec=mpeg4:vpass=2:$opt -o output.avi input.avi

mpeg4 için(mpeg4 ile karşılaştırılınca %75 daha fazla zaman almakta ve output %50 daha büyük olmaktadır):

opt2="vbitrate=2160000:mbd=2:keyint=132:vqblur=1.0:cmp=2:subcmp=2:dia=2:mv0:last_pred=3"
mencoder -oac mp3lame -lameopts cbr:mode=2:br=128 -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=1:$opt2 -o /dev/null input.avi
mencoder -oac mp3lame -lameopts cbr:mode=2:br=128 -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=2:$opt2 -o output.avi input.avi

Windows gibi encoder desteği zayıf işletim sistemleri için mpeg1 videoya çevirmek:

mencoder input.avi -of mpeg -mpegopts format=mpeg1:tsaf:muxrate=2000     -o output.mpg -oac lavc -lavcopts acodec=mp2:abitrate=224 -ovc lavc     -lavcopts vcodec=mpeg1video:vbitrate=2160000:keyint=15:mbd=2:aspect=4/3

ayrıntı
http://www.mplayerhq.hu/DOCS/HTML/en/index.html
http://wiki.videolan.org/Transcode
http://tom.zickel.org/vlcmp4/

Neredeyim ben!?

Linux Notları kategorisinde geziniyorsun.