{"id":599,"date":"2012-09-25T23:10:20","date_gmt":"2012-09-25T23:10:20","guid":{"rendered":"https:\/\/trouble.org\/?p=599"},"modified":"2012-09-25T23:10:20","modified_gmt":"2012-09-25T23:10:20","slug":"lsof-lite-iiiiii","status":"publish","type":"post","link":"https:\/\/trouble.org\/?p=599","title":{"rendered":"lsof lite (III\/III)"},"content":{"rendered":"<p>Finally one that looks at a process and tells you what ports its listening to.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">WPCM450 \/tmp]$ ps |grep ssh<br \/>\n&nbsp;1263 root &nbsp; &nbsp; &nbsp; 4532 S &nbsp; \/sbin\/sshd -g 60 <br \/>\n&nbsp;9730 root &nbsp; &nbsp; &nbsp; 9412 S &nbsp; sshd: root@pts\/0 &nbsp; &nbsp;<br \/>\n10571 root &nbsp; &nbsp; &nbsp; 3556 R &nbsp; grep ssh <br \/>\n[WPCM450 \/tmp]$ .\/lsof-net-pid.sh 1263<br \/>\nPID 1263 is listening on tcp6:22<br \/>\nPID 1263 is listening on tcp:22<\/div><\/div>\n<p>Small script.  Vicious haxx0r sed line in there (written by someone else, obviously!)<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"overflow:auto;white-space:nowrap;height:800px;\"><div class=\"bash codecolorer\">:<br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># busybox - find network ports being listened to by pid<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"st0\">&quot;X$1&quot;<\/span> == <span class=\"st0\">&quot;X&quot;<\/span> <span class=\"br0\">&#93;<\/span> ; <span class=\"kw1\">then<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw3\">echo<\/span> Usage: <span class=\"re4\">$0<\/span> pid<br \/>\n&nbsp; &nbsp;<span class=\"kw3\">exit<\/span> <span class=\"nu0\">1<\/span><br \/>\n<span class=\"kw1\">fi<\/span><br \/>\n<br \/>\n<span class=\"re2\">pid<\/span>=<span class=\"re4\">$1<\/span><br \/>\n<span class=\"re2\">fd<\/span>=<span class=\"st0\">&quot;\/proc\/<span class=\"es2\">$pid<\/span>\/fd&quot;<\/span><br \/>\n<br \/>\n<span class=\"co0\"># this gives inodes of sockets pid is listening to<\/span><br \/>\n<span class=\"re2\">inodes<\/span>=<span class=\"sy0\">`\/<\/span>bin<span class=\"sy0\">\/<\/span><span class=\"kw2\">ls<\/span> <span class=\"re5\">-l<\/span> <span class=\"re1\">$fd<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> socket: <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> <span class=\"st_h\">'{print $NF}'<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/socket:\\[\/\/'<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/\\]\/\/'<\/span><span class=\"sy0\">`<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"st0\">&quot;X<span class=\"es2\">$inodes<\/span>&quot;<\/span> == <span class=\"st0\">&quot;X&quot;<\/span> <span class=\"br0\">&#93;<\/span> ; <span class=\"kw1\">then<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw3\">echo<\/span> <span class=\"re1\">$pid<\/span> isn<span class=\"co3\">\\'<\/span>t listening to any external network ports I could <span class=\"kw2\">find<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw3\">exit<\/span> <span class=\"nu0\">2<\/span><br \/>\n<span class=\"kw1\">fi<\/span><br \/>\n<br \/>\n<span class=\"kw1\">for<\/span> inode <span class=\"kw1\">in<\/span> <span class=\"re1\">$inodes<\/span> ; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp;<span class=\"co0\"># echo $inode<\/span><br \/>\n&nbsp; &nbsp;<span class=\"co0\"># check out if any matches in \/proc\/net (busybox doesn't have per pid net entries)<\/span><br \/>\n<br \/>\n&nbsp; &nbsp;<span class=\"co0\"># this line will print out something like \/proc\/net\/tcp, 00000000000000000000000000000000:0016, 4535<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw2\">awk<\/span> <span class=\"st_h\">'$10 == '<\/span><span class=\"st0\">&quot;<span class=\"es2\">$inode<\/span>&quot;<\/span><span class=\"st_h\">' {print FILENAME, $2, $10}'<\/span> <span class=\"sy0\">\/<\/span>proc<span class=\"sy0\">\/<\/span>net<span class=\"sy0\">\/<\/span>?<span class=\"kw2\">cp<\/span><span class=\"sy0\">*<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">while<\/span> <span class=\"kw2\">read<\/span> proto port inode ; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"st0\">&quot;X<span class=\"es2\">$proto<\/span>&quot;<\/span> <span class=\"sy0\">!<\/span>= <span class=\"st0\">&quot;X&quot;<\/span> <span class=\"br0\">&#93;<\/span> ; <span class=\"kw1\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-n<\/span> <span class=\"st0\">&quot;PID <span class=\"es2\">$pid<\/span> is listening on &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-n<\/span> <span class=\"re1\">$proto<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"st_h\">'s@^.*net\/@@'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-n<\/span> <span class=\"st0\">&quot;:&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"co0\">#<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"co0\"># more busybox hoop jumping... sometimes like ice skating with roller skates<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"co0\">#<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"co0\"># the amazing sed+ stuff courtesy of http:\/\/stackoverflow.com\/questions\/3675012\/hex-to-dec-con<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"co0\">#<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re1\">$port<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> -F: <span class=\"st_h\">'{print $2}'<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"st_h\">'s,\\(..\\)\\(..\\)\\(..\\)\\(..\\),\\4\\3\\2\\1,g'<\/span> <span class=\"sy0\">|<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw2\">read<\/span> hex; <span class=\"kw3\">echo<\/span> $<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span> 0x<span class=\"co1\">${hex}<\/span> <span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw1\">fi<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw1\">done<\/span><br \/>\n<br \/>\n<span class=\"kw1\">done<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Finally one that looks at a process and tells you what ports its listening to. WPCM450 \/tmp]$ ps |grep ssh &nbsp;1263 root &nbsp; &nbsp; &nbsp; 4532 S &nbsp; \/sbin\/sshd -g 60 &nbsp;9730 root &nbsp; &nbsp; &nbsp; 9412 S &nbsp; sshd: root@pts\/0 &nbsp; &nbsp; 10571 root &nbsp; &nbsp; &nbsp; 3556 R &nbsp; grep ssh [WPCM450 \/tmp]$ [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,154,4,6],"tags":[158,157,334,155,156],"class_list":["post-599","post","type-post","status-publish","format-standard","hentry","category-code","category-ipmi-2","category-security","category-tech","tag-bmc","tag-busybox","tag-hack","tag-lsof","tag-shell"],"_links":{"self":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=599"}],"version-history":[{"count":11,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/599\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}