Skip to content

{ Tag Archives } *nix

sudo and su

Sudo is a standard way to give users some administrative rights without giving out the root password. Sudo is very useful in a multi user environment with a mix of server and workstations. Simply call the command with sudo:

sudo /etc/init.d/dhcpd restart            # Run the rc script as root
sudo -u sysadmin whoami                   # Run cmd as an other user

Configuration

Sudo is configured in /etc/sudoers and must only be edited with visudo. The basic syntax is (the lists are comma separated):

user hosts = (runas) commands          # In /etc/sudoers
  • users
    one or more users or %group (like %wheel) to gain the rights
  • hosts
    list of hosts (or ALL)
  • runas
    list of users (or ALL) that the command rule can be run as. It is enclosed in ( )!
  • commands
    list of commands (or ALL) that will be run as root or as (runas)

Additionally those keywords can be defined as alias, they are called User_Alias, Host_Alias, Runas_Alias and Cmnd_Alias. This is useful for larger setups. Here a sudoers example:

cat /etc/sudoers
# Host aliases are subnets or hostnames.Host_Alias   DMZ     = 212.118.81.40/28
Host_Alias   DESKTOP = work1, work2

# User aliases are a list of users which can have the same rights
User_Alias   ADMINS  = colin, luca, admin
User_Alias   DEVEL   = joe, jack, julia
Runas_Alias  DBA     = oracle,pgsql

# Command aliases define the full path of a list of commands
Cmnd_Alias   SYSTEM  = /sbin/reboot,/usr/bin/kill,/sbin/halt,/sbin/shutdown,/etc/init.d/
Cmnd_Alias   PW      = /usr/bin/passwd [A-z]*, !/usr/bin/passwd root # Not root pwd!
Cmnd_Alias   DEBUG   = /usr/sbin/tcpdump,/usr/bin/wireshark,/usr/bin/nmap

# The actual rules
root,ADMINS  ALL     = (ALL) NOPASSWD: ALL    # ADMINS can do anything w/o a password.
DEVEL        DESKTOP = (ALL) NOPASSWD: ALL    # Developers have full right on desktops
DEVEL        DMZ     = (ALL) NOPASSWD: DEBUG  # Developers can debug the DMZ servers.

# User sysadmin can mess around in the DMZ servers with some commands.
sysadmin     DMZ     = (ALL) NOPASSWD: SYSTEM,PW,DEBUG
sysadmin     ALL,!DMZ = (ALL) NOPASSWD: ALL   # Can do anything outside the DMZ.
%dba         ALL     = (DBA) ALL              # Group dba can run as database user.

# anyone can mount/unmount a cd-rom on the desktop machines
ALL          DESKTOP = NOPASSWD: /sbin/mount /cdrom,/sbin/umount /cdrom

The su utility requests appropriate user credentials via PAM and switches to that user ID (the default user is the superuser). A shell is then executed.

PAM is used to set the policy su(1) will use. In particular, by default only users in the wheel group can switch to UID 0 (root). This group requirement may be changed by modifying the pam_group

section of /etc/pam.d/su. See pam_group for details on how to modify this setting.
By default, the environment is unmodified with the exception of USER, HOME, and SHELL. HOME and SHELL are set to the target login’s default values. USER is set to the target login, unless the target login has a
user ID of 0, in which case it is unmodified. The invoked shell is the one belonging to the target login. This is the traditional behavior of su. Resource limits and session priority applicable to the original user’s login class (see login.conf(5)) are also normally retained unless the target login has a user ID of 0.

The options are as follows:

  • -f
    If the invoked shell is csh(1), this option prevents it from reading the “.cshrc” file.
  • -l
    Simulate a full login. The environment is discarded except for HOME, SHELL, PATH, TERM, and USER. HOME and SHELL are modified as above. USER is set to the target login. PATH is set to /bin:/usr/bin. TERM is imported from your current environ- ment. Environment variables may be set or overridden from the
    login class capabilities database according to the class of the target login. The invoked shell is the target login’s, and su will change directory to the target login’s home directory. Resource limits and session priority are modified to that for the target account’s login class.
  • -
    (no letter) The same as -l.
  • -m
    Leave the environment unmodified. The invoked shell is your login shell, and no directory changes are made. As a security precaution, if the target user’s shell is a non-standard shell (as defined by getusershell(3)) and the caller’s real uid is non- zero, su will fail.
  • -s
    Set the MAC label to the user’s default label as part of the user credential setup. Setting the MAC label may fail if the MAC label of the invoking process is not sufficient to transition to the user’s default MAC label. If the label cannot be set, su will fail.
  • -c
    class Use the settings of the specified login class. Only allowed for the superuser.

The -l (or -) and -m options are mutually exclusive; the last one specified overrides any previous ones.

If the optional args are provided on the command line, they are passed to the login shell of the target login. Note that all command line arguments before the target login name are processed by su itself, everything
after the target login name gets passed to the login shell.

By default (unless the prompt is reset by a startup file) the superuser prompt is set to # to remind one of its awesome power.

ENVIRONMENT
Environment variables used by su:

  • HOME
    Default home directory of real user ID unless modified as specified above
  • PATH
    Default search path of real user ID unless modified as specified above.
  • TERM
    Provides terminal type which may be retained for the substituted user ID.
  • USER
    The user ID is always the effective ID (the target user ID) after an su unless the user ID is 0 (root).
  • FILES
    /etc/pam.d/su PAM configuration for su.

EXAMPLES

su man -c catman

Runs the command catman as user man. You will be asked for man’s password unless your real UID is 0.
su man -c ‘catman /usr/share/man /usr/local/man /usr/X11R6/man’ Same as above, but the target command consists of more than a single word and hence is quoted for use with the -c option being
passed to the shell. (Most shells expect the argument to -c to be a single word). su -c staff man -c, catman /usr/share/man /usr/local/man /usr/X11R6/man Same as above, but the target command is run with the resource limits of the login class staff. Note: in this example, the first -c option applies to su while the second is an argument to the shell being invoked.

su -l foo   #Simulate a login for user foo.
su - foo    #Same as above.
su -        #Simulate a login for root.
Also tagged

入侵*NIX类服务器详解

FROM: 网络

在网上有许多网友提出这样的问题:究竟网站的主页是如何被黑的?黑客们到底是如何入侵服务器的?

在讨论这部分知识前,读者需要知道入侵网站是非法的;但是在网络上找到网站的入侵漏洞并通知该网站是受到欢迎的。为什么要这样寻找入侵漏洞或入侵哪,闻名的黑客H ackalot 说过“入侵网站是利用所学的知识来学习新的知识的一种办法”,这也就是中国人所常说的“温故而知新”。

原理
尽管为服务器设计软件的软件工程师们想方设法提高系统的安全性,然而由于系统管理员的水平参差不齐或安全意识底下,往往给黑客提供了入侵的机会。

其实每一个黑客都有自己独到的方法。笔者对于入侵网站服务器的资料收集了很多,但是因为实际情况的不同,往往造成许多方法的失效;由此可见,每一个网站的情况都不同,需要入侵者区分对待。假设深圳的线路比北京的线路要好的多,从而给了词典穷举很大的方便,深圳用户就可以依靠这个优势在线攻击口令,作为北京的用户就需要优先考虑其它办法了。针对这么多的入侵手段,笔者参考 Hackalot 先生这位黑客界名人的一篇文章给大家介绍一下入侵网站的基本步骤。

分析一部分的主页被黑的事例可以发现使用入侵者最热衷于入侵 Web服务器FTP服务器,因为相对来说这是最简单的两种途径。在假设读者对 UNIX 系统和 WEB SERVER 的知识不曾了解的情况下,笔者给出下面的步骤。

A. 了解要入侵的系统

现在网络上用作服务器的操作系统以 UNIX 和 Linux 为主流,假如要入侵这些系统则必须对它们有一个了解。

大部份在 DOS 上使用的指令在 UNIX 及 Linux 上都有对应的指令(因为早期的dos开发借鉴了UNIX),以下列出在使用 SHELL帐号 (shell account)时最主要的一些指令对应的dos指令:

  • help = help
  • cp = copy
  • mv = move
  • ls = dir
  • rm = del
  • cd = cd

要看谁同也在该系统上用户可以键入who指令,要知道系统上某一位使用者的资料, 可以键入finger

B. 破解密码

在UNIX操作系统中, 所有系统使用者的密码都存放在一个文件中,这个文件存放在/etc/passwd这个文件里,这个文件里的帐号的密码都已经都已经经过重新编译(DES 加密),并且都是单向编译(one-way encrypted)。

但是还是有些程序可以得到这些原始的密码。笔者向大家推荐一个破解密码的程序Cracker Jack,它也是一个使用字典来对字典文件进行穷举的软件。首先 Cracker Jack 会把字典文件里的每一个值进行编译,然后将编译过的值与密码文件中的内容进行比较,得到相同的结果就会报告对应的未经编译密码。这个软件巧妙的绕过密码无法反编译的限制,使用穷举比较获得密码。使用这种原理获得密码的工具有许多,读者可以到网络上去搜寻一下。

C. 获得密码文件

这是最困难的一部分。很明显,假如管理员有那么一个密码文件的话,当然不会放在那里让其它人舒舒适服的拿到的。入侵者必须找到好方法以不进入系统的方式拿到密码文件。这里笔者向大家介绍两种方法,大家可以试试,有可能会成功。

1)
tc目录在FTP服务上不会被锁住,入侵可以用FTP client程序使用 anoymouse 匿名帐号登陆,然后检查一下/etc/passwd是否为匿名设置了被读取的权限,假如有马上备份下来使用软件解码。

2)
一些系统中,cgi-bin目录下会有个叫PHF的文件,假如预备入侵的服务器上有的话那就要方便的多了。因为 PHF答应使用者对网站系统里的文件作远端读取,以此为据,用户可以使用浏览器抓取p asswd文件,只要在浏览器地址栏中键入URL:http://xxx.xxx.xxx/cgi-bin/phf?Qalias=x
/bin/cat /etc/passwd
,其中xxx.xxx.xxx 是要入侵的网站名。

假如这两种方法都行不通的话,那入侵者必须实施其它的办法了。

在有些情况下入侵者找到的密码文件的第二部分是X!或者*,那么说明该密码文件已经被锁死,这是系统管理员使用的加强安全的手段之一。但是将密码文件完全隐藏起来的情况是不太有的。通常情况下都会有未经锁死的密码文件备份在系统中,这样入侵者就可以加以利用,比如:入侵者通常会寻找/ etc/shadow 目录或类似的目录,看能否找到密码文件的备份。

D. 建立自己的shell帐号

经过二、三两个要害的步骤入侵者终于拿到了要害的密码文件,并且破解出了密码。现在可以运行TELNET程序,登陆主机了。当连上服务器时服务器会向你显示自己的一些信息,通常是UNIX、linux, aix, irix, ultrix, bsd 甚至是 DOS 和VAX/Vms;然后是 Login 提示符出现在屏幕上,这时键入得来的帐号和密码即可登陆系统。此时入侵者就可以利用自己的UNIX知识做自己喜欢做的事了。

最后对一份密码文件做一个分析,该文件内容如下:

root:1234aaab:0:1:Operator:/:/bin/csh
nobody:*:12345:12345::/:
daemon:*:1:1::/:
sys:*:2:2::/:/bin/csh
sun:123456hhh:0:1:Operator:/:/bin/csh
bin:*:3:3::/bin:
uucp:*:4:8::/var/spool/uucppublic:
news:*:6:6::/var/spool/news:/bin/csh
audit:*:9:9::/etc/security/audit:/bin/csh
sync::1:1::/:/bin/sync
sysdiag:*:0:1:Old System
Diagnostic:/usr/diag/sysdiag:/usr/diag/sysdiag/sysdiag
sundiag:*:0:1:System
Diagnostic:/usr/diag/sundiag:/usr/diag/sundiag/sundiag
tom:456lll45uu:100:20::/home/tom:/bin/csh
john:456fff76Sl:101:20:john:/home/john:/bin/csh
henry:AusTs45Yus:102:20:henry:/home/henry:/bin/csh
harry:SyduSrd5sY:103:20:harry:/home/harry:/bin/csh
steven:GEs45Yds5Ry:104:20:steven:/home/steven:/bin/csh
::0:0:::

其中以 : 分成几个栏位,比如:tom:456lll45uu:100:20:tomchang:/home/tom:/bin/csh的含义是:

  • User Name: tom
  • Password: 456lll45uu
  • User NO: 100
  • Group NO: 20
  • Real Name: tom chang
  • Home Dir: /home/tom
  • Shell: /bin/csh

读者可以发现以上诸如nobody, daemon, sys, bin, uucp, news, audit, sysdiag、sundiag 等的密码栏位都是*,也就是说这些帐号的密码都已锁死,无法直接利用。

值得注重的是,许多系统在首次安装后会有一些缺省帐号和密码,这给投机主义的黑客带来方便,以下就是一些UNIX下缺省的帐号和密码。

ACCOUNT PASSWORD  
root root  
sys sys /system /bin
bin sys /bin
mountfsys mountfsys  
adm adm  
uucp uucp  
nuucp anon  
anon anon  
user user  
games games  
install install  
reboot 供“command login”使用
demo demo  
umountfsys umountfsys  
sync sync  
admin admin  
guest guest  
daemon daemon  

其中 root, mountfsys, umountfsys, install(有时候 sync 也是) 等都是root级别的帐号, 也就是拥有了sysop (系统管理员)的权限。

最后有必要介绍一下UNIX的日志文件。很多入侵者不希望侵入的电脑追踪他们,那到底如何做那。

系统管理员主要依靠系统的 LOG,即我们时常所说的日志文件来获得入侵的痕迹及入侵者进来的IP和其他信息。当然也有些管理员使用第三方工具来记录侵入电脑的信息,这里主要讲的是一般 UNIX 系统里记录入侵踪迹的文件。

UNIX 系统有多个版本,各个系统有不同的LOG文件,但大多数都应该有差不多的存放位置,最普通的位置就是下面的这几个:

  • /usr/adm 早期版本的UNIX
  • /var/adm 新一点的版本使用这个位置
  • /var/log 一些版本的Solaris,Linux BSD,Free BSD使用这个位置
  • /etc 大多数UNIX版本把utmp放在此处,一些也把wtmp放在这里,这也是 syslog.conf的位置

下面列举一些文件的功能,当然他们也根据入侵的系统不同而不同。

  • acct 或 pacct 记录每个用户使用的命令记录
  • access_log 主要使用来服务器运行了NCSA HTTPD,这个记录文件会有什么站点连接过你的服务器
  • aculog 保存着你拨出去的MODEMS记录
  • lastlog 记录了用户最近的登陆记录和每个用户的最初目的地,有时是最后不成功登陆的记录;
  • loginlog 记录一些不正常的登陆记录
  • messages 记录输出到系统控制台的记录,另外的信息由syslog来生成
  • security 记录一些使用UUCP系统企图进入限制范围的事例
  • sulog 记录使用su命令的记录
  • utmp 记录当前登录到系统中的所有用户,这个文件伴随着用户进入和离开系统而不断变化
  • utmpx UTMP的扩展
  • wtmp
  • syslog 最重要的日志文件,使用syslogd守护程序来获得

日志信息:

  • /dev/log 一个UNIX域套接字,接受在本地机器上运行的进程所产生的消息
  • /dev/klog 一个从UNIX内核接受消息的设备
  • 514 端口 一个INTERNET套接字,接受其他机器通过UDP产生的syslog消息
  • Uucp 记录的UUCP的信息,可以被本地UUCP活动更新,也可有远程站点发起的动作修改,信息包括发出和接受的呼叫,发出的请求,发送者,发送时间和发送主机
  • lpd-errs 处理打印机故障信息的日志
  • ftp日志 执行带 -l 选项的ftpd能够获得记录功能
  • httpd日志 HTTPD服务器在日志中记录每一个WEB访问记录
  • history日志 这个文件保存了用户最近输入命令的记录
  • vold.log 记录使用外接媒介时碰到的错误记录
Also tagged

经典*nix命令

id              #显示用户的uid,gid,groups信息
echo $LOGNAME   #显示登录的用户名
bc              #计算表达式,输入2+3 回车计算出5; quit 退出
df -k           #以K字节显示文件系统使用情况

一行命令杀进程inetd
Kill 命令

kill -1 `ps -e | grep inetd |awk '{print $1}'`
netstat -a | grep bat #检查 batserv 程序是否处于监听状态。

more 显示文件的时候,想退出按 q 键,到最前按 g< 键,h 键显示帮助,到最后按 G

env         #查看环境变量
uname -a    #查看本机型号
hostname    #显示主机名
domainname  #显示主机域名

存储, 压缩文件

tar -zcvf
tar -cvf target.tar sourcefilelist  #打包
tar -xvf target.tar                 #解包
tar -tvf target.tar                 #查看包内文件目录结构
compress .tar                       #文件压缩为.z
uncompress .z文件                   #解压缩文件
file                                #查看当前目录下所有文件的格式 file * | grep commands
which 程序名                        #查看命令文件的位置,这个是一个单独的文件
type 程序名                         #查看命令文件的位置,这个是bash内置命令
script                              #从键入script开始,所有终端显示内容将被输入到一个文件里(默认为typescript),直至 exit 或者 Ctrl+D 退出script环境。这在希望记录大段输入以及输出内容时很有用。
compress filename                   #压缩文件
compressdir directory-name          #压缩目录
uncompress filename             #反压缩文件
uncompressdir directory-name        #反压缩目录

搜索含有某个指令的脚本:

find /etc 2>/dev/null | xargs grep 'awk ' 2>/dev/null

说明:

  • find /etc 查找/etc 目录下的所有文件,并显示文件名
  • 2>/dev/null 把所有的错误信息,如权限错误,不能访问等重定向倒看不到的虚拟设备,只显示有用信息。(1 代表"标准输出”,2 代表“标准错误输出”)
  • | 把前一个命令的输出以管道的方式传给后面的命令
  • xargs 对输入的每一个参数,循环执行后面的命令
  • grep 'awk'在指定文件中查找字符串,这里的输入文件时 xargs 命令传过来的,每次处理一个 find 输出的文件名
find . | xargs grep "abc" #全文检索,打印出来包含关键字的文件名和行数据

find . -exec grep -l "string" {} \;             #全文检索,只显示包含关键字的文件名
find . -name *.cpp grep -l "字符串" {} \;       #切换用户并使用该用户的环境配置:su - user
#删除一些旧的文件,但是遇到windows的长文件名时的删除方法
find ./ -mtime +300 | awk -F \0 '{printf("\"%s\"\n",$0)}' | xargs -n 1 rm -f
#查找某个目录下的具有包含某个字符串的文件并删除:
find .  -type f -exec grep "232" {} \; -exec rm {} \;
find .  -type f -exec grep -q "123" {} \; -exec rm {}\;
#删除当前计算机系统日期前2天产生的所有文件,包括子目录下所有目录和文件:
find . -type d -atime +2 -exec /bin/rm -r '{}' 2>/dev/null \
find . -type f -atime +2 -exec /bin/rm -r '{}' 2>/dev/null \
grep 字符串  *                          #在当前目录(不包括子目录)里搜索文件内容
find . | xargs grep aaa                 #在当前目录(包括子目录)里搜索文件内容
find . | xargs grep typedef.*TIASTRU    #同时包含词"typedef"和"TIASTRU"
find . -exec grep -l "string" {} \;     #在当前目录全文查找string串
find . -name file1.c

正则表达式

  • .* 匹配任意字符
  • .. 匹配2个字符
  • abc?(2|9)1 ?匹配0或1个2或9
  • abc*([0–9]) *匹配0或多个0-9
  • abc+([0–9]) +匹配1或多个0-9
  • no@(one|ne) @严格匹配one和ne
  • no!(thing|where) !不匹配thing或where

unix上查看本地的ip地址和网卡信息:

hostname        #显示机器名
cat /etc/hosts
netstat -in
chmod +x filename           #增加执行属性
rm -f file                  #删除文件
rm -rf directory            #删除文件夹
cd a*                       #就可以进入比如:appp的文件夹
ps -x                       #查看属于自己的进程
ps -ef | grep ap*           #查找以ap开头的进程
uname -a                    #显示操作系统os类型
ls -Rl | more     #以分页方式列出目录下所有文件子目录

查看系统中的使用者:

who or finger
su username       #进入其他帐号
who am i

vi模式下

vi *.sql                            #假如vi编辑多个文件,用:n切换下一个文件
ESC + :!sh or :!ksh                 #执行外部命令
ESC + :20,24 w file                 #将文件20-24行放到file文件中
vi -R                               #只读打开文件
q!                                  #离开并放弃保存
wq                                  #离开并保存
w                                   #保存但不离开
ESC+:232                            #代表跳到252行
ESC+:/banner                        #代表跳到查找包含banner词的行,用 n 还可继续查
ESC+: r                             #vi嵌入外部文件 r 文件路径
ESC+:! ls                           #vi执行外部命令
man ls                              #在线帮助man+命令
mv                                  #移动或更改文件、目录名称
pwd                                 #当前目录
cat/more  cat file1 | more
kill PID                            #杀死进程
df          #查看磁盘中可以使用的自由空间
df -t       #查看使用百分比例
diff           #比较两个文件

more 或 vi 向下翻页用 CTRL+D,向上翻页用CTRL+B

l -rt           #显示最近更新的文件

unix默认shell采用ksh
在命令行下,使用时先按“ESC”键

  • j 向后查找
  • k 向前查找
  • / 查找关键字,回车匹配查找,按n显示下一个满足条件命令行,该功能类似vi中操作

用 ESC+k 或 j 上翻或下翻显示历史命令的时候,如何编辑命令呢
k 上翻,l 光标后移,x 删除光标所在的字符,向上箭头把光标移到最后,i 输入新字符

关机

shutdown -y 0
sprintf(szYhtId, "%08s", szTemp);       #字符串不够8位左补0:不要忘了s
szYhtid = atoi(yhtid);                  #转换为INT类型

本地工作站与远端工作站之间文件传输必须拥有远端工作站之帐(号及密码,才可进行传输工作)
执行格式:ftp hostname or ftp ip_address
Example :

ftp doc                 #与远端工作站 doc ,进行文件传输
Name (doc:user-name): 输入帐号
Password (doc:user-name): 输入密码

FTP 命令

  • help 列出 ftp 文件传输,可使用之任何命令。
  • !ls 列出本地工作站,目前目录下之所有文件名。
  • !pwd 列出本地工作站,目前所在之工作目录位置。
  • ls 列出远端工作站目前目录下之所有文件名。
  • dir 列出远端工作站目前目录下之所有文件名(略同於 UNIX 的 ls -l 指令).
  • dir . | more 同上,但每页会暂停(可能不适用 Unix 以外的 ftp)。
  • pwd 列出远端工作站目前所在之目录位置。
  • cd dir1 更改远端工作站之工作目录位置至 dir1 之下。
  • get file1 将远端工作站之文件 file1 ,拷贝到本地工作站中。
  • put file2 将本地工作站之文件 file2 ,拷贝到远端工作站中。
  • mget *.c 将远端工作站中,副文件名为 c 之所有文件,拷贝到本地工作站中。
  • mput *.txt 将本地工作站中,副文件名为 txt 之所有文件,拷贝远端工作站中。
  • prompt 切换交谈式指令(使用 mput/mget 时不用每个文件皆询问yes/no)。
  • quit 结束 ftp 工作。
  • bye 结束 ftp 工作。
  • lcd 切换本地目录

注:
1. 从PC与工作站间的文件传输也可通过在 PC端的 FTP指令进行文件传输,指令用法与上所述大致相同。
2. ![cmd[args]] 在本地机中执行交互shell、exit回到ftp环境,如!ls *.zip

例:

ftp -n -i -v << !
open 172.10.1.11
user test test
prompt
as
quote type b 9
lcd /home/usr/test/yht
cd /home/test/test
mput *
bye
!

注:prompt指设置交互提示,这里关闭

Also tagged

VI 设置

用chpass显示用户信息

#Changing user information for root.
Login: root
Password: $1$W3Iu11I.$OrkaGSgxkaxJqSowHllDu.
Uid [#]: 0
Gid [# or name]: 0
Change [month day year]:
Expire [month day year]:
Class:
Home directory: /root
Shell: /usr/local/bin/bash
Full Name: Charlie &
Office Location:
Office Phone:
Home Phone:
Other information:

在Home directory:这项中可知道该用户目录,如这里是 /root

cd /root

在下面看是否有.exrc文件,如果没有,自己建立一个,.exrc文件是设置vi的环境
如这个.exrc文件中内容:注意:不要像在命令模式中输入那样,不需要前面的冒号

set nu
set autoindent
set showmode
Also tagged