3 案例3:使用diff和patch工具打补丁
3.1 问题
本案例要求优化提升常见网络服务的安全性,主要完成以下任务操作:
使用diff对比文件差异
使用diff生成补丁文件
使用patch命令为旧版本打补丁
- 1
- 2
- 3
3.2 方案
程序是人设计出来的,总是会有这样那样的问题与漏洞,目前的主流解决方法就是为有问题的程序打补丁,升级新版本。
在Linux系统中diff命令可以为我们生成补丁文件,然后使用patch命令为有问题的程序代码打补丁。
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:对比单个文件差异
1) 编写两个版本的脚本,一个为v1版本,一个为v2版本。
[root@svr5 ~]# cat test1.sh //v1版本脚本
#!/bin/bash
echo "hello wrld"
[root@svr5 ~]# cat test2.sh //v2版本脚本
#!/bin/bash
echo "hello world"
echo "test file"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2) 使用diff命令语法
使用diff命令查看不同版本文件的差异。
[root@svr5 ~]# diff test1.sh test2.sh //查看文件差异
@@ -1,3 +1,3 @@
#!/bin/bash
-echo "hello world"
-echo "test"
+echo "hello the world"
+echo "test file"
[root@svr5 ~]# diff -u test1.sh test2.sh //查看差异,包含头部信息
--- test1.sh 2018-02-07 22:20:02.723971251 +0800
+++ test2.sh 2018-02-07 22:20:13.358760687 +0800
@@ -1,3 +1,3 @@
#!/bin/bash
-echo "hello world"
-echo "test"
+echo "hello the world"
+echo "test file"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
diff制作补丁文件的原理:告诉我们怎么修改第一个文件后能得到第二个文件。
这样如果第一个版本的脚本有漏洞,我们不需要将整个脚本都替换,仅需要修改有问题的一小部分代码即可,diff刚好可以满足这个需求!
像Linux内核这样的大块头,一旦发现有一个小漏洞,我们不可能把整个内核都重新下载,全部替换一遍,而仅需要更新有问题的那一小部分代码即可!
diff命令常用选项:
-u 输出统一内容的头部信息(打补丁使用),计算机知道是哪个文件需要修改
-r 递归对比目录中的所有资源(可以对比目录)
-a 所有文件视为文本(包括二进制程序)
-N 无文件视为空文件(空文件怎么变成第二个文件)
-N选项备注说明:
A目录下没有txt文件,B目录下有txt文件
diff比较两个目录时,默认会提示txt仅在B目录有(无法对比差异,修复文件)
diff比较时使用N选项,则diff会拿B下的txt与A下的空文件对比,补丁信息会明确说明如何从空文件修改后变成txt文件,打补丁即可成功!
步骤二:使用patch命令对单文件代码打补丁
1)准备实验环境
[root@svr5 ~]# cd demo
[root@svr5 demo]# vim test1.sh
#!/bin/bash
echo "hello world"
echo "test"
[root@svr5 demo]# vim test2.sh
#!/bin/bash
echo "hello the world"
echo "test file"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-
生成补丁文件
[root@svr5 demo]# diff -u test1.sh test2.sh > test.patch
3)使用patch命令打补丁
在代码相同目录下为代码打补丁
[root@svr5 demo]# yum -y install patch
[root@svr5 demo]# patch -p0 < test.patch //打补丁
patching file test1.sh
//patch -pnum(其中num为数字,指定删除补丁文件中多少层路径前缀)
//如原始路径为/u/howard/src/blurfl/blurfl.c
//-p0则整个路径不变
//-p1则修改路径为u/howard/src/blurfl/blurfl.c
//-p4则修改路径为blurfl/blurfl.c
//-R(reverse)反向修复,-E修复后如果文件为空,则删除该文件
[root@svr5 demo]# patch -RE < test.patch //还原旧版本,反向修复
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
步骤三:对比目录中所有文件的差异
1) 准备实验环境
[root@svr5 ~]# mkdir demo
[root@svr5 ~]# cd demo
[root@svr5 demo]# mkdir {source1,source2}
[root@svr5 demo]# echo "hello world" > source1/test.sh
[root@svr5 demo]# cp /bin/find source1/
[root@svr5 demo]# tree source1/ //source1目录下2个文件
|-- find
`-- test.sh
[root@svr5 demo]# echo "hello the world" > source2/test.sh
[root@svr5 demo]# echo "test" > source2/tmp.txt
[root@svr5 demo]# cp /bin/find source2/
[root@svr5 demo]# echo "1" >> source2/find
[root@svr5 demo]# tree source2/ //source1目录下3个文件
|-- find
|-- test.sh
`-- tmp.txt
//注意:两个目录下find和test.sh文件内容不同,source2有tmp.txt而source1没有该文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
2)制作补丁文件
[root@svr5 demo]# diff -u source1/ source2/
//仅对比了文本文件test.sh;二进制文件、tmp都没有对比差异,仅提示,因为没有-a和-N选项
[root@svr5 demo]# diff -Nu source1/ source2/
//对比了test.sh,并且使用source2目录的tmp.txt与source1的空文件对比差异。
[root@svr5 demo]# diff -Nua source1/ source2/
//对比了test.sh、tmp.txt、find(程序)。
- 1
- 2
- 3
- 4
- 5
- 6
步骤四:使用patch命令对目录下的所有代码打补丁
1)使用前面创建的source1和source2目录下的代码为素材,生成补丁文件
[root@svr5 ~]# cd demo
[root@svr5 demo]# diff -Nuar source1/ source2/ > source.patch
- 1
- 2
2)使用patch命令为代码打补丁
[root@svr7 demo]# ls
source1 source2 source.patch
[root@svr7 demo]# cat source.patch //对比的文件有路径信息
--- source1/test.sh 2018-02-07 22:51:33.034879417 +0800
+++ source2/test.sh 2018-02-07 22:47:32.531754268 +0800
@@ -1 +1 @@
-hello world
+hello the world
[root@svr7 demo]# cd source1
[root@svr7 source1]# patch -p1 < ../source.patch
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了329 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 3万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm attented bt-button personal-watch" data-report-click="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>
3 案例3:使用diff和patch工具打补丁
3.1 问题
本案例要求优化提升常见网络服务的安全性,主要完成以下任务操作:
使用diff对比文件差异
使用diff生成补丁文件
使用patch命令为旧版本打补丁
- 1
- 2
- 3
3.2 方案
程序是人设计出来的,总是会有这样那样的问题与漏洞,目前的主流解决方法就是为有问题的程序打补丁,升级新版本。
在Linux系统中diff命令可以为我们生成补丁文件,然后使用patch命令为有问题的程序代码打补丁。
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:对比单个文件差异
1) 编写两个版本的脚本,一个为v1版本,一个为v2版本。
[root@svr5 ~]# cat test1.sh //v1版本脚本
#!/bin/bash
echo "hello wrld"
[root@svr5 ~]# cat test2.sh //v2版本脚本
#!/bin/bash
echo "hello world"
echo "test file"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2) 使用diff命令语法
使用diff命令查看不同版本文件的差异。
[root@svr5 ~]# diff test1.sh test2.sh //查看文件差异
@@ -1,3 +1,3 @@
#!/bin/bash
-echo "hello world"
-echo "test"
+echo "hello the world"
+echo "test file"
[root@svr5 ~]# diff -u test1.sh test2.sh //查看差异,包含头部信息
--- test1.sh 2018-02-07 22:20:02.723971251 +0800
+++ test2.sh 2018-02-07 22:20:13.358760687 +0800
@@ -1,3 +1,3 @@
#!/bin/bash
-echo "hello world"
-echo "test"
+echo "hello the world"
+echo "test file"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
diff制作补丁文件的原理:告诉我们怎么修改第一个文件后能得到第二个文件。
这样如果第一个版本的脚本有漏洞,我们不需要将整个脚本都替换,仅需要修改有问题的一小部分代码即可,diff刚好可以满足这个需求!
像Linux内核这样的大块头,一旦发现有一个小漏洞,我们不可能把整个内核都重新下载,全部替换一遍,而仅需要更新有问题的那一小部分代码即可!
diff命令常用选项:
-u 输出统一内容的头部信息(打补丁使用),计算机知道是哪个文件需要修改
-r 递归对比目录中的所有资源(可以对比目录)
-a 所有文件视为文本(包括二进制程序)
-N 无文件视为空文件(空文件怎么变成第二个文件)
-N选项备注说明:
A目录下没有txt文件,B目录下有txt文件
diff比较两个目录时,默认会提示txt仅在B目录有(无法对比差异,修复文件)
diff比较时使用N选项,则diff会拿B下的txt与A下的空文件对比,补丁信息会明确说明如何从空文件修改后变成txt文件,打补丁即可成功!
步骤二:使用patch命令对单文件代码打补丁
1)准备实验环境
[root@svr5 ~]# cd demo
[root@svr5 demo]# vim test1.sh
#!/bin/bash
echo "hello world"
echo "test"
[root@svr5 demo]# vim test2.sh
#!/bin/bash
echo "hello the world"
echo "test file"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-
生成补丁文件
[root@svr5 demo]# diff -u test1.sh test2.sh > test.patch
3)使用patch命令打补丁
在代码相同目录下为代码打补丁
[root@svr5 demo]# yum -y install patch
[root@svr5 demo]# patch -p0 < test.patch //打补丁
patching file test1.sh
//patch -pnum(其中num为数字,指定删除补丁文件中多少层路径前缀)
//如原始路径为/u/howard/src/blurfl/blurfl.c
//-p0则整个路径不变
//-p1则修改路径为u/howard/src/blurfl/blurfl.c
//-p4则修改路径为blurfl/blurfl.c
//-R(reverse)反向修复,-E修复后如果文件为空,则删除该文件
[root@svr5 demo]# patch -RE < test.patch //还原旧版本,反向修复
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
步骤三:对比目录中所有文件的差异
1) 准备实验环境
[root@svr5 ~]# mkdir demo
[root@svr5 ~]# cd demo
[root@svr5 demo]# mkdir {source1,source2}
[root@svr5 demo]# echo "hello world" > source1/test.sh
[root@svr5 demo]# cp /bin/find source1/
[root@svr5 demo]# tree source1/ //source1目录下2个文件
|-- find
`-- test.sh
[root@svr5 demo]# echo "hello the world" > source2/test.sh
[root@svr5 demo]# echo "test" > source2/tmp.txt
[root@svr5 demo]# cp /bin/find source2/
[root@svr5 demo]# echo "1" >> source2/find
[root@svr5 demo]# tree source2/ //source1目录下3个文件
|-- find
|-- test.sh
`-- tmp.txt
//注意:两个目录下find和test.sh文件内容不同,source2有tmp.txt而source1没有该文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
2)制作补丁文件
[root@svr5 demo]# diff -u source1/ source2/
//仅对比了文本文件test.sh;二进制文件、tmp都没有对比差异,仅提示,因为没有-a和-N选项
[root@svr5 demo]# diff -Nu source1/ source2/
//对比了test.sh,并且使用source2目录的tmp.txt与source1的空文件对比差异。
[root@svr5 demo]# diff -Nua source1/ source2/
//对比了test.sh、tmp.txt、find(程序)。
- 1
- 2
- 3
- 4
- 5
- 6
步骤四:使用patch命令对目录下的所有代码打补丁
1)使用前面创建的source1和source2目录下的代码为素材,生成补丁文件
[root@svr5 ~]# cd demo
[root@svr5 demo]# diff -Nuar source1/ source2/ > source.patch
- 1
- 2
2)使用patch命令为代码打补丁
[root@svr7 demo]# ls
source1 source2 source.patch
[root@svr7 demo]# cat source.patch //对比的文件有路径信息
--- source1/test.sh 2018-02-07 22:51:33.034879417 +0800
+++ source2/test.sh 2018-02-07 22:47:32.531754268 +0800
@@ -1 +1 @@
-hello world
+hello the world
[root@svr7 demo]# cd source1
[root@svr7 source1]# patch -p1 < ../source.patch
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了329 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 3万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm attented bt-button personal-watch" data-report-click="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>