1、安装条件
首先需要root
2、使用的工具
android源码external下,本身就有两个sshd服务端,dropbear和openssh。dropbear是一个轻量级的ssh,所以我们使用这个(其实另外一个原因是因为openssh一直没配成功,不知道账户密码在android下要怎么设置)
源码中,dropbear默认是没有编译出ssh相关的程序,只编译出库,所以需要修改一下Android.mk文件:增加以下这两段,来编译出我们需要的dropbear和dropbearkey。
############################################################
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=\
dbutil.c buffer.c \
dss.c bignum.c \
signkey.c rsa.c random.c \
queue.c \
atomicio.c compat.c fake-rfc2553.c
LOCAL_SRC_FILES+=\
common-session.c packet.c common-algo.c common-kex.c \
common-channel.c common-chansession.c termcodes.c \
tcp-accept.c listener.c process-packet.c \
common-runopts.c circbuffer.c \
loginrec.c
LOCAL_SRC_FILES+=\
svr-kex.c svr-algo.c svr-auth.c sshpty.c \
svr-authpasswd.c svr-authpubkey.c svr-authpubkeyoptions.c svr-session.c svr-service.c \
svr-chansession.c svr-runopts.c svr-agentfwd.c svr-main.c svr-x11fwd.c \
svr-tcpfwd.c svr-authpam.c
LOCAL_STATIC_LIBRARIES := libtommath libtomcrypt
LOCAL_SHARED_LIBRARIES := \
libutils \
libcutils \
libc
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE := dropbear
LOCAL_C_INCLUDES += $(LOCAL_PATH)/libtommath
LOCAL_C_INCLUDES += $(LOCAL_PATH)/libtomcrypt/src/headers
LOCAL_CFLAGS += -DDROPBEAR_SERVER -DANDROID_CHANGES
#LOCAL_CFLAGS += -DSFTPSERVER_PATH='"$(LOCAL_PATH)/sftp-server"'
include $(BUILD_EXECUTABLE)
############################################################
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=\
dbutil.c buffer.c \
dss.c bignum.c \
signkey.c rsa.c random.c \
queue.c \
atomicio.c compat.c fake-rfc2553.c
LOCAL_SRC_FILES+=\
dropbearkey.c gendss.c genrsa.c
LOCAL_STATIC_LIBRARIES := libtommath libtomcrypt
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE := dropbearkey
LOCAL_C_INCLUDES += $(LOCAL_PATH)/libtommath
LOCAL_C_INCLUDES += $(LOCAL_PATH)/libtomcrypt/src/headers
LOCAL_CFLAGS += -DDROPBEAR_SERVER
include $(BUILD_EXECUTABLE)
endif # TARGET_SIMULATOR != true
############################################################
3、登录方式
这里支持两种登录方式,一种是使用密钥登录,另一种是使用密码登录(我们平时用这种比较多)
(1)使用密钥登录:
创建秘钥:
客户端使用的是SecureCRT,由于使用私钥登录,首先需要创建密钥对;
步骤: “工具”->“创建公钥…”,密钥类型“DSA”(“RSA”应该也可以),通行短语就留空(否则登录需要输入密码),密钥长度默认1024位,选择“OpenSSH密钥格式”,选择一个文件夹保存密钥,同时会生成私钥Identity和公钥Identity.pub;
然后需要将公钥Identity.pub拷贝到/etc/.ssh/authorized_keys,权限为755。
(2)使用密码登录:
因为在android中,我们的root用户是没有密码的,所以一直不能登录(不允许空密码的情况下),要不就是直接不用密码都可以登录(允许空密码)。这种情况下,SSH就不能满足我们的安全性需求。
需要修改一点源码:
/* check for empty password - need to do this again here * since the shadow password may differ to that tested * in auth.c */
//del by wusc
/*if (passwdcrypt[0] == '\0') { dropbear_log(LOG_WARNING, "User '%s' has blank password, rejected", ses.authstate.pw_name); send_msg_userauth_failure(0, 1); return; }*/
/* check if client wants to change password */
changepw = buf_getbool(ses.payload);
if (changepw) {
/* not implemented by this server */
dropbear_log(LOG_WARNING,">>>>>>>>>>>>>>>>>>>>>>>>client wants to change password");
send_msg_userauth_failure(0, 1);
return;
}
password = buf_getstring(ses.payload, &passwordlen);
//del by wusc
/* the first bytes of passwdcrypt are the salt */
/* testcrypt = crypt((char*)password, passwdcrypt); m_burn(password, passwordlen); m_free(password);*/
//edify by wusc:这里xxxxx是测试用的密码,所以在这写死了,可以自己修改为从配置文件读取。
if (strcmp(password, "xxxxx") == 0 ) {
/* successful authentication */
dropbear_log(LOG_NOTICE,
"Password auth succeeded for '%s' from %s",
ses.authstate.pw_name,
svr_ses.addrstring);
send_msg_userauth_success();
} else {
dropbear_log(LOG_WARNING,
">>>>>>>>>>>>>>>>>>>>>>>>Bad password attempt for '%s' from %s",
ses.authstate.pw_name,
svr_ses.addrstring);
dropbear_log(LOG_WARNING,"Bad password: %s",password);
send_msg_userauth_failure(0, 1);
}
m_burn(password, passwordlen);
m_free(password);
这样修改后,就可以使用root登录,密码就是xxxxx。
另外源码一个在一些用户名不对的情况下,会多次调用
send_msg_userauth_failure(0, 1);这样会导致客户端程序收到两次拒绝,所以在第一次输入错误用户名的情况下,修改为正确的用户名密码,第一次登陆也会报错。
把多余的函数调用去掉即可。
另外,像默认的key地址,端口等等配置,都在options.h中有定义。
4、使用方法
首先,需要用
dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key,权限也要改为755
创建dss key
接下来看下dropbear使用方法,
(1)如果使用密码登录:dropbear -E -F -v (运行在前端并打开log,以便debug)
客户端输入用户名密码即可。
(2)如果使用秘钥登录:dropbear -E -F -v -s (-s是禁止密码登录),这时SecureCRT会提示你选公钥,就是刚才我们创建的那一对,然后填写同行短语即可。
dropbear -h
Dropbear sshd v0.53.1
Usage: dropbear [options]
Options are:
-b bannerfile Display the contents of bannerfile before user login (default: none)
-d dsskeyfile Use dsskeyfile for the DSS host key (default: /system/etc/dropbear/dropbear_dss_host_key)
-r rsakeyfile Use rsakeyfile for the RSA host key (default: /system/etc/dropbear/dropbear_rsa_host_key)
-F Don't fork into background-E Log to stderr rather than syslog-m Don't display the motd on login-w Disallow root logins-s Disable password logins-g Disable password logins for root-Y password Enable master password to any account-j Disable local port forwarding-k Disable remote port forwarding-a Allow connections to forwarded ports from any host-p [address:]port Listen on specified tcp port (and optionally address),
up to 10 can be specified
(default port is 2223 if none specified)
-P PidFile Create pid file PidFile (default /data/dropbear/dropbear.pid)
-i Start for inetd-W (default 24576, larger may be faster, max 1MB)-K (0 is never, default 0)-I (0 is never, default 0)-v verbose (compiled with DEBUG_TRACE)
5、开机启动
将其配到init.rc中即可,etc下相关的文件,在.mk文件中,在编译时将其拷贝到对应的目录下即可。
service start_sshd /system/xbin/dropbear -v
class main
group root root
oneshot