Linux

nautilus脚本应用实例之二:右键菜单挂载/卸载ISO

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

右键挂载选中的iso

#!/usr/bin/perl -w
 
# This script mount the selected iso(s).
 
use strict;
 
my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
 
my $foo = system("gksudo -u root -k -m 'enter your password for root terminal access' /bin/echo 'r00t'");
 
if( ! $foo )
{
	foreach my $file (@files)
	{
		if ( ! -f $file && ! -l $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not mount $file.    \n\n    Only regular files can be mounted.    ","200", "300");
			system (@dialog);
		}
		else
		{
			$_ = $file;
			my ( $dir, $filename ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ );
			if( ! -d "/media/$filename" )
			{
				system("sudo mkdir /media/$filename");
			}
			system("sudo mount -o loop -t iso9660 $file /media/$filename");
		}
	}
 
}

右键卸载选中的iso

#!/usr/bin/perl -w
 
# This script unmount the selected iso(s).
 
use strict;
 
my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
 
my $foo = system("gksudo -u root -k -m 'enter your password for root terminal access' /bin/echo 'r00t'");
 
if( ! $foo )
{
	foreach my $file (@files)
	{
		if ( ! -f $file && ! -l $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not unmount $file.    \n\n    Only regular files can be unmounted.    ","200", "300");
			system (@dialog);
		}
		else
		{
			$_ = $file;
			my ( $dir, $filename ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ ); 
 
			system("sudo umount /media/$filename");
			if( -d "/media/$filename" )
			{
				system("sudo rmdir /media/$filename");
			}
		}
 
	}
 
}

nautilus脚本应用实例之一:用meld比较选中的文件或文件夹

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

前两天推特上@peigen推荐了一款diff工具meld,和我之前使用的diffuse相比,最大的优点是可以进行文件夹比较。但存在的共同缺点就是如果直接使用nautilus中集成关联打开方式的方法,无法直接通过选中两个待比较的文件或文件夹右键直接进行比较,而总是打开两个应用程序实例。研究了一下nautilus的右键菜单定制方法,但总感觉功能不够强大,且无法完成这个看似非常简单的需求。

偶然间发现了这篇文章:http://ubuntu-tutorials.com/2006/12/29/right-click-to-launch-custom-scripts-with-nautilus-ubuntu-6061-610/

研究了一下,原来可以通过定制gnome-nautilus的右键菜单执行脚本来实现我的需求。下面附上我实现好的两个nautilus脚本

用meld比较选中的文件

#!/usr/bin/perl -w
 
# This script compares the selected file(s) with meld.
 
use strict;
 
my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
my $count = $#files + 1;
if( $count != 2 ) {
	my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Only 2 files can be compared at once. ","200", "300");
	system (@dialog);
 
}
else {
	foreach my $file (@files)
	{
		if ( ! -f $file && ! -l $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not compare $file.    \n\n    Only regular files can be diffed.    ","200", "300");
			system (@dialog);
		}
	}
 
	system("meld $files[0] $files[1]");
}

用meld比较选中的文件夹

#!/usr/bin/perl -w
 
# This script compares the selected dir(s) with meld.
 
use strict;
 
my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
my $count = $#files + 1;
if( $count != 2 ) {
	my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Only 2 directories can be compared at once. ","200", "300");
	system (@dialog);
 
}
else {
	foreach my $file (@files)
	{
		if ( ! -d $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not compare $file.    \n\n    Only directories can be diffed.    ","200", "300");
			system (@dialog);
		}
	}
 
	system("meld $files[0] $files[1]");
}

无需安装阿里旺旺即可聊天的方法

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

Linux下的IM方案有很多种,QQ可以使用WebQQ,GTalk、MSN等可以使用pidgin,飞信有libFetion、OpenFetion,唯独阿里旺旺比较麻烦,没有简单、可行的方案。Wine版的不太好用,开个虚拟机吧太麻烦。记得以前是可以用网页旺旺的,但可惜现在用起来比较麻烦,只能通过网页和淘宝店主临时聊天。那如果我想和我的好友(非淘宝卖家)聊天怎么办呢?

分析了一下网页版的阿里旺旺的启动链接,发现了一个有趣的参数,如下:

http://www.taobao.com/webww/?ver=1&&touid=cntaobao对方旺旺用户名&siteid=cntaobao&status=2&portalId=&gid=&itemsId=

网页版的旺旺可以在线保存聊天记录,保存时间根据你的等级来换算。

另外,已经通过网页旺旺聊天的用户会自动保存在最近联系人列表里,登录阿里旺旺网页版后,单击右下旺旺小水滴,再单击您要与其聊天的旺旺帐号,就可以打开与好友的聊天窗口了。更多关于阿里旺旺网页版的帮助可以在登录阿里旺旺网页版后,点击帮助。

附上一个界面截图

阿里旺旺网页版界面截图












原创文章,转载请注明: 转载自猪在笑 [ http://www.huangwei.me/blog/ ]
本文链接地址: http://www.huangwei.me/blog/2010/08/27/wangwang-linux-im/



[CodeSnippet]Perl实现Gtk任务栏的消息提示

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

依赖的Perl第三方lib为Gtk2::Notify

Ubuntu下安装:

sudo apt-get install libgtk2-notify-perl

代码如下:

#!/usr/bin/perl -w
use Gtk2::Notify -init, "Test Perl Notify";
use Encode;
 
my $summary = "summary here";
my $message = "message here";
my $icon = "/usr/share/icons/hicolor/128x128/apps/fwbuilder.png";
my $attach_widget = undef;
my $notification = Gtk2::Notify->new($summary, $message, $icon, $attach_widget);
$notification->show;

效果如图:

perl-gtk2-libnotify screenshot

后记:

有这么一款Firefox扩展,可以实现Firefox下载完成的任务栏消息提示。查看了一下它的代码,发现是用python实现的libnotify接口。只需要改一下xpi文件中的install.rdf就可以在3.6版本的Firefox上安装了。扩展安装地址:https://addons.mozilla.org/en-US/firefox/addon/9622/

按照这个方法,其实很多Web应用程序都可以借此实现Linux下的任务栏消息提示集成。有时间的话我来写一个WebQQ的消息提示扩展玩玩。


[CodeSnippet]Perl抓包分析之DNS debug

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

依赖的Perl第三方库在Ubuntu上安装:

  • libnet-pcap-perl
  • libnet-dns-perl

如果还需要其他Perl第三方库,可以通过apt-file(Ubuntu Lucid默认没有安装)来检索所需要的pm文件在哪个deb包。

例如:

$ apt-file search Net::DNS::Packet
 libnet-dns-perl: /usr/share/man/man3/Net::DNS::Packet.3pm.gz
$ apt-file search Net::Pcap
 libnet-pcap-perl: /usr/share/man/man3/Net::Pcap.3pm.gz

Wireshark虽然也可以抓包分析,但是用可编程的脚本语言来辅助分析可以省去很多肉眼比对的工作。

下面直接上代码:

#!/usr/bin/perl -w
#
# Copyright 2003, Brian Hatch, released under the GPL
#
# watch_dns:
#   A program to watch for inbound DNS queries, and print the
#   source, destination, and requested domain name of the queries.
#
# You'll need to fill this in with your actual IP address
# (If we didn't restrict the destination IP address, we'd
# catch all our outbound queries too.)
#
# Original Downloaded from:
# http://www.hackinglinuxexposed.com/articles/20030730.html
#
# Modified by huangwei.me 2010-08-02
 
# 本机IP地址定义
my $MY_IP_ADDRESS='10.2.1.83';
# 非特权权限的uid/gid,可以用id命令查看当前用户的uid和gid
my $UNPRIV="1001";
# 自行指定监听网卡,留空将使用系统默认探测到的网卡
my $MY_DEV="vmnet8";
#
# No changes required hereafter
#
use Net::Pcap;
use Net::DNS::RR;
use FileHandle;
use English;  # $UID等价于$<, $EUID等价于$>
use strict;
 
STDOUT->autoflush(1);
 
while ( 1 ) {
 
	my $pid = fork();
	if ( ! defined $pid ) { die "Unable to fork.  Yikes." };
 
	if ( $pid ) {
		# Parent process (running as root) will wait for
		# child.  If child exits, we'll create another one.
		wait();
		sleep(1);  # To keep us from respawning too fast if necessary.
	} else {
		print "Child starting\n";
 
		# Child process will do actual sniffing.
		# First, create our packet capturing device
		my($pcap_t) = create_pcap();
 
		unless ( $pcap_t ) {
			die "Unable to create pcap";
		}
 
		# Let's stop running as root.  Since we already
		# have our pcap descriptor, we can still use it.
		$EGID="$UNPRIV $UNPRIV";	# setgid and setgroups()
		$GID=$UNPRIV;
		$UID=$UNPRIV; $EUID=$UNPRIV;
 
		# Capture packets forever.
		Net::Pcap::loop($pcap_t, -1, \&process_pkt, 0);
 
		# Technically, we shouldn't get here since the loop
		# is infinite (-1), but just in case, close and exit.
		Net::Pcap::close($pcap_t);
		exit 1;
	}
}
 
sub create_pcap {
	my $promisc = 1;   # enter promiscuous mode or not.
	my $snaplen = 1500; # the maximum number of bytes to capture from each packet
 
	my $to_ms = 0;			# timeout
	my $opt=1;                          # Sure, optimisation is good...
	my($err,$net,$mask,$dev,$filter_t);
 
	my $filter = "udp dst port 53 or udp src port 53"; # pcap capture filter
 
	# Look up an appropriate device (eth0 usually)
	$dev = $MY_DEV || Net::Pcap::lookupdev(\$err);
	print "sniffing on ", $dev, "\n";
	$dev or die "Net::Pcap::lookupdev failed.  Error was $err";
 
	if ( (Net::Pcap::lookupnet($dev, \$net, \$mask, \$err) ) == -1 ) {
		die "Net::Pcap::lookupnet failed.  Error was $err";
	}
 
	# Actually open up our descriptor
	my $pcap_t = Net::Pcap::open_live($dev, $snaplen, $promisc, $to_ms, \$err);
	$pcap_t || die "Can't create packet descriptor.  Error was $err";
 
	if ( Net::Pcap::compile($pcap_t, \$filter_t, $filter, $opt, $net) == -1 ) {
		die "Unable to compile filter string '$filter'\n";
	}
 
	# Make sure our sniffer only captures those bytes we want in
	# our filter.
	Net::Pcap::setfilter($pcap_t, $filter_t);
 
	# Return our pcap descriptor
	$pcap_t;
}
 
# Routine to process the packet -- called by Net::Pcap::loop()
# every time an appropriate packet is snagged.
sub process_pkt {
	my($user_data, $hdr, $pkt) = @_;
 
	my($src_ip) = 26;           # start of the source IP in the packet
	my($dst_ip) = 30;           # start of the dest IP in the packet
	my($udp) = 42;              # start of UDP pkt payload
	my($domain_start) = 55;     # start of the domain in the packet
	my($data);
 
	# extract the source IP addr into dotted quad form.
	my($source) = sprintf("%d.%d.%d.%d",
		ord( substr($pkt, $src_ip, 1) ),
		ord( substr($pkt, $src_ip+1, 1) ),
		ord( substr($pkt, $src_ip+2, 1) ),
		ord( substr($pkt, $src_ip+3, 1) ));
 
	# extract the destination IP addr into dotted quad form.
	my($destination) = sprintf("%d.%d.%d.%d",
		ord( substr($pkt, $dst_ip, 1) ),
		ord( substr($pkt, $dst_ip+1, 1) ),
		ord( substr($pkt, $dst_ip+2, 1) ),
		ord( substr($pkt, $dst_ip+3, 1) ));
 
	$data = substr($pkt, $domain_start);
 
	$data =~ s/0.*//g;             # strip off everything after the domain
	$data =~ s/[^-a-zA-Z0-9]/./g;    # change the domain component separators
	# back int to dots.
 
	print "$source -> $destination: $data\n" if ( $source and $destination and $data);
	dump_dns(parse_dns($pkt, $udp));
}
 
# dump dns query & response in details
sub dump_dns {
	my $dns = $_;
	$dns->print;
 
}
 
sub parse_dns {
	my($pkt, $udp) = @_;
	my $udp_payload = substr($pkt, $udp);
	my $dns = Net::DNS::Packet->new(\$udp_payload);
	return $dns;
}

SSH安全加固一二三

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

今天车东大侠在GReader上分享了一篇文章《为什么要用公钥/私钥而不是密码去做SSH身份验证》,联系到最近越来越恶劣的网络环境,我越发觉得有必要把我的一些SSH安全方法分享一下,以降低SSH通道被恶意破坏的概率,提高攻击者的攻击代价。

0. 系统自动更新

这个世界上没有什么漏洞会比一个系统级的远程溢出漏洞更可怕的了!虽然此类漏洞如今是越来越少被公开,越来越少被发现。但本着不怕一万,就怕万一的安全指导原则。小概率、高危险的漏洞只能依靠及时的系统更新来帮助我们修复了。

除此之外,应用软件级别的漏洞也可以通过系统自动更新来杜绝。

以下的安全加固主要是从应用程序配置的角度来谈SSH的安全加固,配置既包括SSH本身的配置参数,也包括使用第三方软件来对SSH进行额外的安全配置。

1. 更换默认的SSH端口

有句大俗话,叫”不怕贼偷就怕贼惦记”。对于信息安全来说,就是“只要给我足够的时间,任何系统都会被发现漏洞,并被攻破”。如果使用默认的22端口来使用SSH,其主要不安全因素体现在2点:

(1) 互联网上每时每刻都有人在做端口扫描,在nmap的默认扫描设置里,22端口作为常用端口会在默认的扫描选项条件下被用于端口扫描、连接尝试、操作系统探测等。所以,如果你使用默认的22端口,就是等于告诉“贼”:“来吧,我在用SSH服务。试试看暴力破解我的口令?”

(2) 有一个英文单词叫censorship,有一种技术叫DPI,有一种设备叫IDS。造句就是:censorship会使用基于DPI技术的IDS来偷窥你!如果你使用默认的22端口,那么IDS会非常开心,因为这等于是帮助它节省了DPI的开销!

2. sshd设置中的不安全因素

# 建议更换默认端口
Port 22
# 建议只监听需要的端口,默认是监听所有可用的本机IP地址
#ListenAddress ::
#ListenAddress 0.0.0.0
# 建议只使用SSH协议版本2
Protocol 2
# 建议使用更长的服务器密钥长度,如2048
ServerKeyBits 768
# Authentication:
# 默认登录超时时间一般为120秒,建议缩短该时间到30
LoginGraceTime 120
# 建议禁止Root用户直接登录,将yes改为no
PermitRootLogin yes
# 如果你不需要基于口令的登录方式,可以将yes改为no
#PasswordAuthentication yes

3. 使用第三方安全加固工具

目前来说,针对SSH协议的最轻便的攻击手段就是暴力口令破解。因此,使用第三方安全加固工具的主要目的也就是对抗暴力破解攻击。现有的第三方SSH暴力破解对抗工具从技术实现手段来说,主要有两大类。分别是基于日志审计触发机制和基于PAM的插件机制。基于日志审计触发机制的代表工具分别是fail2bandenyhosts,但这两款工具都曾经暴露出一些漏洞,其中主要的漏洞原理都是日志注入技术。相比较而言,pam_abl是更为安全的一种防止暴力破解口令攻击的工具。

最后推荐Google “ssh security best practice

第一条结果就非常值得仔细阅读:http://www.cyberciti.biz/tips/linux-unix-bsd-openssh-server-best-practices.html

参考资料

  1. http://www.rackaid.com/resources/how-to-harden-or-secure-ssh-for-improved-security/
  2. http://www.rackaid.com/resources/rise-in-ssh-brute-force-attacks/
  3. http://www.ossec.net/main/attacking-log-analysis-tools



为什么要用公钥/私钥而不是密码去做SSH身份验证


[已验证]qemu和vmware冲突

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

已验证冲突的平台环境

OS: Ubuntu 10.04.1 LTS 32bit

Platform: thinkpad x201i

VMWare: 7.0.0 build-203739

qemu: QEMU PC emulator version 0.12.3 (qemu-kvm-0.12.3), Copyright (c) 2003-2008 Fabrice Bellard

具体症状

在终端运行qemu后出现:

kvm: unhandled exit 5
kvm_run returned -22

或者

kvm: unhandled exit 7
kvm_run returned -22

其他-1

在Ubuntu下用zerowine自动化分析Windows下的程序行为,发现qemu总是异常僵死。尝试了多种环境配置和不同的启动参数均未果,最终发现了原因之所在。

其他-2

在Ubuntu 10.04上用apt-get安装kvm-pxe时会出现apt-get错误,如下:

dpkg: error processing /var/cache/apt/archives/kvm-pxe_5.4.4-1ubuntu1.1_all.deb (–install):
unable to open ‘/usr/share/kvm/pxe-e1000.bin.dpkg-new’: No such file or directory

通过apt-file命令(需要另外安装,默认lucid没有安装这个程序)可以找到所缺失的这个文件在哪个deb包里!

$ apt-file search pxe-e1000.bin
kvm-pxe: /usr/share/kvm/pxe-e1000.bin
kvm-pxe: /usr/share/qemu/pxe-e1000.bin

但是只要一用apt-get install去安装就会报上面的错误。Google后得知,这是一个迄今为止未得到官方修复的bug

原因就在于系统在安装qemu和kvm的时候,已经建立了一个链接,由/usr/share/kvm指向/usr/share/qemu!

解决方法很简单,删除/usr/share/kvm这个链接就可以了。


用vim和cscope构建xul代码阅读的环境

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

一直没有找到一个好用的xul代码阅读工具,无奈,还是拾起我的vim。捣腾了一下,基本的函数定义跳转、调用查找、关键词跳转、引用查找都没有问题了。

1. vim的xul语法着色

下载并安装vim的语法定义文件:http://www.vim.org/scripts/script.php?script_id=1937

2. 使用cscope生成索引数据库文件

#!/bin/bash
find `pwd` -name "*.js" -o -name "*.xul" > cscope.files
cscope -Rb

3. 编辑.vimrc

autocmd FileType xul call XUL()
fun! XUL()
   set expandtab
   set tabstop=4
   set softtabstop=4
   set shiftwidth=4
   set expandtab
   set tags+=/usr/include/tags,/usr/local/include/tags
   set path+=/usr/local/include
 
   cs add /YOUR_ABSOLUTE_PATH_TO_CSCOPE.OUT/cscope.out
 
   "查找当前选中的C符号(函数名、宏、结构体、变量名等)
   nmap s :cs find s =expand("")
 
   "查找定义
   nmap g :cs find g =expand("")
 
   "查找调用本函数的所有函数
   nmap c :cs find c =expand("")
 
   "查找本函数调用的所有函数
   nmap d :cs find d =expand("")
 
   "查找当前选中的字符串
   nmap t :cs find t =expand("")
 
   "查找egrep模式字符串
   nmap e :cs find e =expand("")
 
   "查找当前选中的文件名
   nmap f :cs find f =expand("")
 
   "查找包含当前文件的所有文件
   nmap i :cs find i ^=expand("")$
endfun " endfun XUL

附上我的vim效果图


原创文章,转载请注明: 转载自猪在笑 [ http://www.huangwei.me/blog/ ]
本文链接地址: http://www.huangwei.me/blog/2010/06/29/vim-xul-cscope/



简单几条命令检测DDoS攻击

摘录自:http://www.lunarforums.com/vps_hosting_at_lunarpages/useful_linux_scripts_lsof_ps_fuser_netstat-t41474.0.html

# netstat -anp |grep ‘tcp\|udp’ | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n

 1 123.122.9.23
 1 64.233.169.101
 1 72.14.204.83
 2 72.14.204.17
 4 210.192.100.178
 6 0.0.0.0

# netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -nr

5 210.192.100.178
4 72.14.204.17
3 64.233.169.132
2 72.14.204.99
2 64.233.169.118
1 64.233.169.101
1 123.122.9.23

# netstat -ntu | grep -v TIME_WAIT | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -nr

4 72.14.204.17
3 210.192.100.178
1 64.233.169.101
1 123.122.9.23

# netstat -an | grep :80 | awk ‘{print $5}’ | cut -f1 -d”:” | sort | uniq -c | sort -n

1 64.233.169.118
1 74.125.53.101
2 64.233.169.132
3 72.14.204.99
7 210.192.100.178

[原创]解决Ubuntu下Firefox间歇性无响应的bug

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

测试环境:Firefox 3.6.2 zh_CN + Ubuntu 9.04

罪魁祸首:Foxyproxy Standard扩展

导致Firefox间歇性无响应的原理:Foxyproxy Standard扩展会在被启用状态下,强制在Firefox的配置项中添加一个配置项:

network.dns.disablePrefetch   True

该项设置在about:config页无法修改!无法删除!即使你直接编辑pref.js也无效!只要Foxyproxy一启用就会强制给你刷上这个设置!

我谨慎的表示怀疑这是一种脑残的设计!

我恶毒的推测这是一种对免费用户的QJ!让你付费购买它的Pro版本扩展!

其实,自从使用autoproxy自动代理配置脚本之后,Foxyproxy已经成为了鸡肋。现在更是严重的影响了我使用Firefox的心情!所以我的终极解决方法就是:

卸载Foxyproxy!使用自动代理配置脚本!



  • GTalk with me!

  • 订阅我的博客

  •  

    2010年九月
    « 八    
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  
  • Google朋友圈

  • 文章分类

  • 量子统计

  • Copyright © 1996-2010 猪在笑. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress
    Page 1 of 3123