Linux 使用技巧
不定期更新
两个linux文件互传
1 | $ scp -r linux-2.6.26 root@(目标ip)IP:/usr/src/(假设放到/usr/src路径) |
文件查找
将当前目录及其子目录下所有文件后缀为 .c 的文件列出来:
1 | $ find . -name "*.c" |
find默认不查找软链接的文件夹, 所以, 可以加
-L
解决这个问题, 这个很重要!!!!!!
将一个本地程序做成一个本地服务程序
1 | $ socat tcp-l:2333,reuseaddr,fork exec:./pwn1 |
服务端口在2333,使用nc 127.0.0.1 2333连接
获取ubuntu当前内核的源码
进入lauchpad, 分清你的系统名称, 比如 20.04 叫 Focal Fossa, 系统当前版本:
1 | $ uname -r |
那么我们在页面的以下部分会看到以下部分的内容
Active series and milestones
22.04 “Jammy” series - development
Milestones: jammy-updates, ubuntu-22.04, ubuntu-22.04-beta, ubuntu-22.03, ubuntu-22.02, ubuntu-22.04-feature-freeze, ubuntu-22.01, ubuntu-21.12, and ubuntu-21.1121.10 “Impish” series - current
Milestones: impish-updates20.04 “Focal” series - supported <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Milestones: focal-updates and ubuntu-20.04.418.04 “Bionic” series - supported
Milestones: bionic-updates16.04 “Xenial” series - supported
Milestones: xenial-updates14.04 “Trusty” series - supported
Milestones: ubuntu-14.04.6
点击其中的focal series
, 会出现搜索框, 我们就搜索linux-image-5.13.0-35-generic
, 会得到如下几个结果:
1 | linux-image-5.13.0-35-generic: <<<<<<<<<<<<<<<<<<<<<<< |
选择第一个, 出现如下:
1 | Signed kernel image generic |
此处我选Source package下的链接. 之后选择downloads里的tar.xz文件即可.
这个版本的文件里包含的是一个下载脚本, 并没有包含完整的src文件. 所以还是需要想想其它办法直接获得文件最好.
快速转换图片格式,修改分辨率
1 | $ convert -resize 100x100 src.jpg dst.jpg |
修改文件的用户
1 | 查看归属: |
使用audit记录创建的程序
audit是记录linux审计信息的内核模块。
他记录系统中的各种动作和事件,比如系统调用,文件修改,执行的程序,系统登入登出和记录所有系统中所有的事件。audit还可以将审计记录写入日志文件。
如果想记录新创建的process, 可以直接修改/etc/audit/audit.rules
, 添加一行-a task,always
, 之后通过cat /var/log/audit/audit.log|grep EXECVE
来筛选你想要的记录.
比如
1 | $ auditctl -a exit,always -F arch=b64 -S execve 添加execve检测 |
更多可以参考linux监控工具audit
修改terminal的显示路径
1 | $ vim ~/.bashrc |
找到
1 | ## If this is an xterm set the title to user@host:dir |
将PS1那行修改为(其实就是把w换成W)
1 | PS1=”[\u@\h:\W]\\$” |
添加环境变量
1 | export PATH=$PATH:/home/victorv |
创建terminal的快捷键
如果是在kali,terminal是没有快捷键的,到设置的keyboard里面,添加自定义快捷键,键值为
1 | gnome-terminal |
或者安装nautilus-open-terminal
gdb改变汇编代码显示方式
1 | (gdb) set disas intel |
设置反汇编代码使用的指令集,可选择 intel 指令集或 AT&T指令集.
usb驱动相关
1 | 查找usb驱动 |
centos安装内核header
1 | yum install kernel-devel-$(uname -r) kernel-headers-$(uname -r) |
如果遇到没有搜索结果, 可以做如下操作:
- 查看当前版本
1
2[root@centos~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) - 修改文件
/etc/yum.repos.d/CentOS-Vault.repo
, 添加当前版本的以下信息: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[C(Your Version Number)-base] 比如 [C5.6-base]
name=CentOS-(Your Version Number) - Base
baseurl=http://vault.centos.org/(Your Version Number)/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
enabled=1
[C(Your Version Number-updates]
name=CentOS-(Your Version Number) - Updates
baseurl=http://vault.centos.org/(Your Version Number)/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
enabled=1
示例:
[C7.4.1708-base]
name=CentOS-7.4.1708 - Base
baseurl=https://vault.centos.org/7.4.1708/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1
[C7.4.1708-updates]
name=CentOS-7.4.1708 - Updates
baseurl=https://vault.centos.org/7.4.1708/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1
之后再试一次install即可.
更多参考Finding Old kernel-devel Packages For CentOS
添加sudoer 并且取消密码
1 | superuser ALL=(ALL) NOPASSWD:ALL |
创建ssh服务
1 | $ yum -y install openssh-server openssh-clients |
创建ftp 服务
1 | sudo yum install vsftpd |
ubuntu修改内核调试启动项
1 | vi /etc/default/grub |
在屁股后面添加 kgdboc=ttyS1,115200
1 | grep menu /boot/grub/grub.cfg |
第一个数字1代表第二行的submenu,第二个3代表submenu的第四个(从0开始)
挂起一个进程
ctrl+z
fg 恢复
打包、解压文件
解包使用x,打包使用c
tar.xz
解包:tar zxvf file.tar.xz 或者:xz -d file.tar.xz && tar xvf file.tar
打包:tar zcvf file.tar.xz
.tar
解包:tar xvf FileName.tar
打包:tar cvf FileName.tar DirName
(注:tar是打包,不是压缩!)
———————————————
.gz
解压1:gunzip FileName.gz
解压2:gzip -d FileName.gz
压缩:gzip FileName
.tar.gz 和 .tgz
解压:tar zxvf FileName.tar.gz
压缩:tar zcvf FileName.tar.gz DirName
———————————————
.bz2
解压1:bzip2 -d FileName.bz2
解压2:bunzip2 FileName.bz2
压缩: bzip2 -z FileName
.tar.bz2
解压:tar jxvf FileName.tar.bz2
压缩:tar jcvf FileName.tar.bz2 DirName
———————————————
.bz
解压1:bzip2 -d FileName.bz
解压2:bunzip2 FileName.bz
压缩:未知
.tar.bz
解压:tar jxvf FileName.tar.bz
压缩:未知
———————————————
.Z
解压:uncompress FileName.Z
压缩:compress FileName
.tar.Z
解压:tar Zxvf FileName.tar.Z
压缩:tar Zcvf FileName.tar.Z DirName
———————————————
.zip
解压:unzip FileName.zip
压缩:zip FileName.zip DirName
———————————————
.rar
解压:rar x FileName.rar
压缩:rar a FileName.rar DirName
———————————————
.lha
解压:lha -e FileName.lha
压缩:lha -a FileName.lha FileName
———————————————
.rpm
解包:rpm2cpio FileName.rpm | cpio -div
———————————————
.deb
解包:ar p FileName.deb data.tar.gz | tar zxf -
———————————————
.tar .tgz .tar.gz .tar.Z .tar.bz .tar.bz2 .zip .cpio .rpm .deb .slp .arj .rar .ace .lha .lzh .lzx .lzs .arc .sda .sfx .lnx .zoo .cab .kar .cpt .pit .sit .sea
解压:sEx x FileName.*
压缩:sEx a FileName.* FileName
单独重新编译一个内核模块
当我们想修改内核某个模块,又不想重新make all的时候,可以这样操作.
1 | make drivers/net/ethernet/intel/e1000/e1000.ko |
或者
1 | make drivers/net/ethernet/intel/e1000/ |
gcc 汇编
1 | .intel_syntax noprefix # intel 汇编格式 |
一个简单的 t.s 文件:
1 | .intel_syntax noprefix |
t.c:
1 |
|
如果是t.cpp
, 记得使用extern "C" int test(void);
编译链接:
1 | gcc -c t.s |
如果是编写so文件, 记得在Makefile的命令中添加-fstack-protector-all
, 否则, 会有个execute stack
的flag在里面, 导致dlopen失败.
更多参考GNU assembler, How do I compile the asm generated by GCC?, GNU Assembler Examples
编写linux驱动与汇编相关的tips
如果想给驱动内联一个汇编文件的函数, 可以如下:
Makefile:
1 | obj-m += Anyname.o |
main.c:
1 |
|
test.S: 这里后缀必须是大写的S, 额外的格式参考arch/x86/net/bpf_jit.S
1 | .intel_syntax noprefix #声明 intel 格式 |
如果想禁止gcc编译的驱动给某个函数优化, 可以使用void __attribute__((optimize("O0"))) test(void)
, 这样这个函数就不会被优化了.
也可以采取如下方式:
1 | #pragma GCC push_options |
额外的tips, 如果存在if(var&0x80000000)
这样的操作, var一定不要用int
类型, 要用无符号! gcc会把判断直接优化成0!!!!!!!!
gcc 内联汇编语法示例
1 | __asm__ ("movl %eax, %ebx\n\t" |
1 | int a=10, b; |
“=r”(b) 的含义, 将输出放入倒变量b里(%0 代表第一个变量, 此处第一个变量是输出里的b, 所以%0就是b, %1是 a). r代表使用任意寄存器, 如果是其它寄存器, 参考如下:
1 | +---+--------------------+ |
如果是使用内存, 用 m.
其它指示标识:
- “m” : A memory operand is allowed, with any kind of address that the machine supports in general.
- “o” : A memory operand is allowed, but only if the address is offsettable. ie, adding a small offset to the address gives a valid address.
- “V” : A memory operand that is not offsettable. In other words, anything that would fit the
m’ constraint but not the
o’constraint.- “i” : An immediate integer operand (one with constant value) is allowed. This includes symbolic constants whose values will be known only at assembly time.
- “n” : An immediate integer operand with a known numeric value is allowed. Many systems cannot support assembly-time constants for operands less than a word wide. Constraints for these operands should use ’n’ rather than ’i’.
- “g” : Any register, memory or immediate integer operand is allowed, except for registers that are not general registers.
x86指令独有:
1 | 1. "r" : Register operand constraint, look table given above. |
特殊符合含义:
- “=“ : 将结果写入到指定位置
- “&“ : Means that this operand is an earlyclobber operand, which is modified before the instruction is finished using the input operands. Therefore, this operand may not lie in a register that is used as an input operand or as part of any memory address. An input operand can be tied to an earlyclobber operand if its only use as an input occurs before the early result is written.
示例:
1 | static inline char * strcpy(char * dest,const char *src) |
当我们不希望编译器优化掉我们的某些特殊循环判断时:
1 | asm ("l1:\tmovb (%0), %%al\n\t" |
禁用gcc的某个函数优化
1 | #pragma GCC push_options |
log至文件中
有时候看不到printf, 需要log到文件里, 就经常需要查阅怎么写, 比较烦人. 记录一下.
1 |
|
centos7 代理yum
1 | yum install epel-release -y |
修改配置vi /etc/proxychains.conf
:
1 | [ProxyList] |
代理yum
1 | $> proxychains4 yum install bpftrace |
Centos6 国内源
因为centos6.x停止维护了, 所以要找个能用的源很麻烦, 偶尔要设置又容易忘了, 在此记录一下
下载
1
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
编辑
/etc/yum.repos.d/CentOS-Base.repo
, 在vim里更改版本:%s/6.10/6.8/g
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53# CentOS-Base.repo
[base]
name=CentOS-vault-6.10 - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/os/$basearch/
http://mirrors.aliyuncs.com/centos-vault/6.10/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
#released updates
[updates]
name=CentOS-vault-6.10 - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/updates/$basearch/
http://mirrors.aliyuncs.com/centos-vault/6.10/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
#additional packages that may be useful
[extras]
name=CentOS-vault-6.10 - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/extras/$basearch/
http://mirrors.aliyuncs.com/centos-vault/6.10/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-vault-6.10 - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos-vault/6.10/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
#contrib - packages by Centos Users
[contrib]
name=CentOS-vault-6.10 - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/contrib/$basearch/
http://mirrors.aliyuncs.com/centos-vault/6.10/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos-vault/6.10/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6如果是centos7的, 记得还需要改
RPM-GPG-KEY-CentOS-6
为RPM-GPG-KEY-CentOS-7
接着:yum clean all
, yum makecahe
.
Centos 7.5更新7.9
- 下载centos 7.9的ISO 下载链接
- 把文件拷贝进去
- 挂载iso
1
2$ mkdir /mnt/cdrom
$ mount ./CentOS-7-x86_64-DVD-2009.iso /mnt/cdrom - 更改源
/etc/yum.repo.d/CentOS-Media.repo
1
2
3
4
5
6[c7-media]
name=CentOS Media
baseurl=file:///mnt/cdrom/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 - 更新
yum --disablerepo=* --enablerepo=c7-media update
Centos 6.x 安装python2.7
yum install openssl-devel
- 下载源码: python 2.7.13源码, 解压源码
tar Jxf Python-2.7.13.tar.xz
./configure --prefix=/usr/local/python27 --enable-shared
make && make install
,ln -s /usr/local/python27/bin/python /usr/bin/python27
echo "/usr/loca/python27/lib" >>/etc/ld.so.conf
, 执行ldconfig
- 直接运行
python27
, 如果正常运行就ok.
bash 语法
引用命令行参数
直接引用
$1,$2….
如果超过9, ${10}
${1:-8} 如果不存在1位置的变量, 默认给8
${parameter:?word} 可以默认给字符串
flags
```bash
while getopts u:a:f: flag#选项后面的冒号表示该选项需要参数
do
case “${flag}” in
u) username=${OPTARG};;#参数存在$OPTARG中
a) age=${OPTARG};;
f) fullname=${OPTARG};;
esac
done
echo “Username: $username”;
echo “Age: $age”;
echo “Full Name: $fullname”;
1 |
|
$* :和$@相同,但”$*“ 和 “$@”(加引号)并不同,”$*“将所有的参数解释成一个字符串,而”$@”是一个参数数组
shift operator
1 | i=1; |
if
1 | if commands; then |
Operation | Effect |
---|---|
[ ! EXPR ] | True if EXPR is false. |
[ ( EXPR ) ] | Returns the value of EXPR. This may be used to override the normal precedence of operators. |
[ EXPR1 -a EXPR2 ] | True if both EXPR1 and EXPR2 are true. |
[ EXPR1 -o EXPR2 ] | True if either EXPR1 or EXPR2 is true. |
[ -a FILE ] |
True if FILE exists. |
[ -b FILE ] |
True if FILE exists and is a block-special file. |
[ -c FILE ] |
True if FILE exists and is a character-special file. |
[ -d FILE ] |
True if FILE exists and is a directory. |
[ -e FILE ] |
True if FILE exists. |
[ -f FILE ] |
True if FILE exists and is a regular file. |
[ -g FILE ] |
True if FILE exists and its SGID bit is set. |
[ -h FILE ] |
True if FILE exists and is a symbolic link. |
[ -k FILE ] |
True if FILE exists and its sticky bit is set. |
[ -p FILE ] |
True if FILE exists and is a named pipe (FIFO). |
[ -r FILE ] |
True if FILE exists and is readable. |
[ -s FILE ] |
True if FILE exists and has a size greater than zero. |
[ -t FD ] |
True if file descriptor FD is open and refers to a terminal. |
[ -u FILE ] |
True if FILE exists and its SUID (set user ID) bit is set. |
[ -w FILE ] |
True if FILE exists and is writable. |
[ -x FILE ] |
True if FILE exists and is executable. |
[ -O FILE ] |
True if FILE exists and is owned by the effective user ID. |
[ -G FILE ] |
True if FILE exists and is owned by the effective group ID. |
[ -L FILE ] |
True if FILE exists and is a symbolic link. |
[ -N FILE ] |
True if FILE exists and has been modified since it was last read. |
[ -S FILE ] |
True if FILE exists and is a socket. |
[ FILE1 -nt FILE2 ] |
True if FILE1 has been changed more recently than FILE2 , or if FILE1 exists and FILE2 does not. |
[ FILE1 -ot FILE2 ] |
True if FILE1 is older than FILE2 , or is FILE2 exists and FILE1 does not. |
[ FILE1 -ef FILE2 ] |
True if FILE1 and FILE2 refer to the same device and inode numbers. |
[ -o OPTIONNAME ] |
True if shell option “OPTIONNAME” is enabled. |
[ -z STRING ] |
True of the length if “STRING” is zero. |
[ -n STRING ] or [ STRING ] |
True if the length of “STRING” is non-zero. |
[ STRING1 == STRING2 ] | True if the strings are equal. “=” may be used instead of “==” for strict POSIX compliance. |
[ STRING1 != STRING2 ] | True if the strings are not equal. |
[ STRING1 < STRING2 ] | True if “STRING1” sorts before “STRING2” lexicographically in the current locale. |
[ STRING1 > STRING2 ] | True if “STRING1” sorts after “STRING2” lexicographically in the current locale. |
[ ARG1 OP ARG2 ] | “OP” is one of -eq , -ne , -lt , -le , -gt or -ge . These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers. |
编译Gnutls
基于Centos7
- 先编译gmplib, 安装后,
export GMP_CFLAGS="-I/usr/local/include" GMP_LIBS="-L/usr/local/lib -lgmp"
- 编译安装nettle,
./configure --prefix=/usr --enable-static --enable-mini-gmp
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:/usr/local/lib/pkgconfig/
设置pkg-config
的查找路径- 设置lib路径, 编辑
/etc/ld.so.conf
, 添加一行/usr/local/lib
, 执行ldconfig -v
- 编译gnutls:
./configure --without-p11-kit
编译samba最新版
在安装好gnutls后, 安装需要的组件:
1 | $ yum install python36-dns |
上面的组件安装完成后, 就可以配置编译了:
./configure --enable-debug --prefix=/home/vv/install-smb-4.17.2 --with-shared-modules='!vfs_snapper'
Linux Ptrace的权限问题
如果想避免同用户使用ptrace调试, 可以设置prctl(PR_SET_DUMPABLE, 1LL, 0LL, 0LL, 0LL)
, 参考
与之相关的还有/proc/sys/kernel/yama/ptrace_scope
, 这个也影响了ptrace的使用.
OpenSSL技巧
如果想修改原始的send数据, SSL_CTX_set_msg_callback(ctx, message_cb);
, 这个回调就是在send前触发.
如果想发送任意的加密数据, 可以使用ssl->method->ssl_write_bytes(ssl, SSL3_RT_ALERT, data, data_length, &written);
去发送.
dtls server示例:
1 |
|
openssl renegotiate
当完成tls握手后, 如果你想触发renegotiate操作, 可以如下操作:
1 | int ret = SSL_renegotiate(ssl); |
SSL_renegotiate
设置重协商标志, SSL_do_handshake
发送加密的hello request请求. 如果client正常处理, 就应该返回一个加密的hello消息. 后续, 调用SSL_read
来触发新的握手协商. 需要注意的时, 如果连接里有未读取的数据, 需要读到client回执的hello消息时才会触发握手操作!
如果只是想简单测试, 可以用openssl程序实现, 下面的命令加载一个openssl server, 读取密钥和证书, 监听443.
1 | > openssl.exe s_server -accept 443 -cert D:\tmp\cert.pem -key D:\tmp\key.pem |
输入r
然后按Enter
, 就可以让server给client发送重协商操作.
参考:
An Introduction to OpenSSL Programming, Part II of II, 这个文章很老旧了, 代码会有不对的地方.
schannel renegotiate的实现
1 | int Renegotiation(SOCKET Server_Socket) { |
这段代码应该是这么写的, 但是我自己跑的时候, 在第二次的AcceptSecurityContext
会遇到解密错误, 暂时不清楚原因, 但是网络上又找不到相关的实现做一个参考, 所以列出来希望对人有帮助.
参考:
Schannel and Session Renegotiation
Renegotiating an Schannel Connection
cmake
获取某个git项目下的依赖git项目: git submodule update --init --recursive
cmake -DXXXX=ON ./
表示开启XXXX特性, 生成makefile. XXXX特性由CMakeLists.txt定义, 形式如下:
1 | option(suppress_header_searches "do not try to find headers - used when compiler check will fail" OFF) |
表示接受参数-Dsuppress_header_searches=ON
, 默认是OFF
.
生成makefile后, 执行cmake --build .
编译