Ну, вот я и добрался до своего ненаглядного рутера WL500gP. За последние пару дней на плечи ему свалилось
немало:
- SSH
- 4 Gb Flash
- Установка пакетов
- syslog-ng
- Создание пользователей
- VSFTPD
- Samba 3
- Cron
- Firewall
Все это поверх прошивки от Олега. Рекомендую глянуть на инструкцию по установке. Итак, по порядку.
Подготовка
Возможность управлять загрузкой рутера по-сути одна - файлы /usr/local/sbin/post-*, которые выполняются
в таком порядке:
- post-firewall
Выполняется сразу после инициализации сетевых устройств. Скрипту система передает параметры - WAN-интерфейс,
WAN-адрес, Bridge-интерфейс, Bridge-адрес:
/usr/local/sbin/post-firewall vlan1 wan.ip.ad.dr br0 lan.ip.ad.dr
- post-boot
Параметрами не интересовался.
- pre-mount
Параметрами не интересовался. Выполняется только, если есть что монтировать.
- post-mount
Параметрами не интересовался. Выполняется только, если есть что монтировать.
- pre-shutdown (при перегрузке рутера)
Параметрами не интересовался.
Я создал файл /usr/local/sbin/post-include с полезными переменными такого содержания:
#!/bin/sh
self=`basename $0`
log="/usr/bin/logger -t $self"
Изначально каждый файл /usr/local/sbin/post-* у меня выглядит так:
#!/bin/sh
. "`dirname $0`/post-include"
$log 'Begin'
# initialization here
$log 'End'
А /usr/local/sbin/pre-shutdown так:
#!/bin/sh
. "`dirname $0`/post-include"
[ -x /opt/bin/logger ] && log="/opt/bin/logger -p Syslog.Alert"
$log 'Shutting down system.'
# BEGIN services termination
if [ `pidof vsftpd` -ne "" ]; then
$log 'Terminating vsftpd'
/bin/kill `pidof vsftpd`
fi
if [ `pidof smbd` -ne "" ]; then
$log 'Terminating smbd'
/bin/kill `pidof smbd`
fi
if [ `pidof nmbd` -ne "" ]; then
$log 'Terminating nmbd'
/bin/kill `pidof nmbd`
fi
if [ -f /var/run/lighttpd.pid ]; then
$log 'Terminating lighhttpd'
/bin/kill `cat /var/run/lighttpd.pid`
fi
if [ -f /opt/var/run/cron.pid ]; then
$log 'Terminating cron'
/bin/kill `cat /opt/var/run/cron.pid`
fi
if [ -f /var/run/syslog-ng.pid ]; then
$log 'Terminating syslog-ng'
/bin/kill `cat /var/run/syslog-ng.pid`
fi
if [ -f /var/run/dropbear.pid ]; then
$log 'Terminating dropbear'
/bin/kill `cat /var/run/dropbear.pid`
fi
# END services termination
if [ -d /tmp/harddisk/opt ]; then
$log 'Unmounting FlashFS'
/bin/umount /tmp/harddisk
/bin/umount /opt
fi
pre-shutdown аккуранто останавливает все службы и в конце размонтирует файловую
систему.
Для сохранения файлов не с флешки в ПЗУ устройства необходимо выполнить следующую команду:
flashfs save && flashfs commit && flashfs enable. Я постоянно забывал сохраняться и после
перегрузки набивал изменения снова. Для сохранения файлов в других каталогах необходимо добавить пути в файл
/usr/local/.files.
Исполняемым файлам не забывать делать chmod +x file.
SSH
Выполнить:
mkdir -p /usr/local/etc/dropbear
dropbearkey -t dss -f /usr/local/etc/dropbear/dropbear_dss_host_key
dropbearkey -t rsa -f /usr/local/etc/dropbear/dropbear_rsa_host_key
Для автоматического запуска SSH сервера (dropbear) нужно добавить в файл /usr/local/sbin/post-boot
следующие строчки:
$log 'Starting dropbear SSH server'
/usr/sbin/dropbear 1>/dev/null 2>&1
В конце:
flashfs save && flashfs commit && flashfs enable
Также необходимо добавить парочку правил в файрвол. Для этого правим /usr/local/sbin/post-firewall:
$log 'SSH rules'
iptables -I INPUT 5 -i $1 -p tcp -d $2 --dport 22 -m state --syn --state NEW -m limit --limit 2/minute --limit-burst 1 -j LOG --log-prefix "SSH Brute force attack: " --log-level 6
iptables -I INPUT 6 -i $1 -p tcp -d $2 --dport 22 -m state --syn --state NEW -m limit --limit 2/minute --limit-burst 1 -j ACCEPT
iptables -I INPUT 7 -i $1 -p tcp -d $2 --dport 22 -m state --syn --state NEW -j DROP
4 Gb Flash
Флешка нужна для установки дополнительного софта, ну, и для хранения файлов, конечно. Также устройство может
загружаться с флешки! Но мне это пока не нужно.
Для пробы имелась 512 Mb флешка, на которую ядро сильно ругалось. Пришлось ставить 4 Gb Pretec. Поскольку из самого
устройства разбить на разделы не получилось, я использовал Acronis Disk Director 10. Получилось два раздела - Linux
swap на 100 с копейками мегабайт и Linux ext3 - остальное.
Через веб морду я отключил встроенный FTP и Samba. В результате не загружались необходимые модули ядра - scsi_mod,
sd_mod, usb-storage (как последствие - не выполнялись скрипты pre-mount и post-mount). Их
нужно загрузить самостоятельно в /usr/local/sbin/post-boot:
/sbin/insmod scsi_mod
/sbin/insmod sd_mod
/sbin/insmod usb-storage
Что в результате:
Установка пакетов
В системе имеется простенький менеджер пакетов - ipkg. По умолчанию весь софт ставится в /opt, поэтому
нужно, чтобы в /opt был смонтирован корень ext3. Для этого изменяем
/usr/local/sbin/post-mount (вставляем в самое начало после $log 'Begin') :
[ -d /tmp/harddisk/opt ] && $log 'Mounting /opt' && /bin/mount -o bind,sync,noatime,rw /tmp/harddisk /opt
Выполнить (все это описано здесь):
ipkg.sh update
ipkg.sh install ipkg-opt
ipkg update
echo "/etc/ipkg.conf" >> /usr/local/.files
flashfs save && flashfs commit && flashfs enable
Ставим необходимый софт:
ipkg install adduser
ipkg install bash
ipkg install coreutils
ipkg install findutils
ipkg install grep
ipkg install less
ipkg install mc
ipkg install screen
ipkg install util-linux
ipkg install vim
ipkg install which
ipkg install wget-ssl
syslog-ng
syslogd я сразу меняю на syslog-ng. Ставим:
ipkg install syslog-ng
cd /opt/etc
ln -s syslog-ng/syslog-ng.conf syslog-ng.conf
Добавляем в /usr/local/sbin/post-mount:
if [ -x /opt/sbin/syslog-ng ]; then
$log 'Replacing syslogd'
[ -x /opt/bin/unlink ] && /opt/bin/unlink /tmp/syslog.log && /opt/bin/ln -s /opt/var/log/all.log /tmp/syslog.log
kill `/bin/pidof syslogd`
fi
Это, естественно, должно выполнятся до запуска syslog-ng.
\
Не забываем также про остановку системы, файл /usr/local/sbin/pre-shutdown (как можно позже, но перед
размонтированием):
if [ -f /var/run/syslog-ng.pid ]; then
$log 'Terminating syslog-ng'
/bin/kill `cat /var/run/syslog-ng.pid`
fi
rc.unslung и init.d
Многие пакеты закидывают скрипт запуска службы в /opt/etc/init.d/ с названием SXYService.
Нужен механизм, который будет выполнять их в правильном порядке. Создаем исполняемый файл
/opt/etc/init.d/rc.unslung с таким содержимым:
#!/bin/sh
# Start all init scripts in /opt/etc/init.d
# executing them in numerical order.
#
for i in /opt/etc/init.d/S??* ;do
[ ! -f "$i" ] && continue
[ ! -x "$i" ] && continue
case "$i" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set start
. $i
)
;;
*)
# No sh extension, so fork subprocess.
$i start
;;
esac
done
Добавляем вызов /opt/etc/init.d/rc.unslung в конец /usr/local/sbin/post-mount:
[ -x /opt/etc/init.d/rc.unslung ] && $log 'Running rc.unslung' && /opt/etc/init.d/rc.unslung | $log
Запуск syslog-ng из init.d
Теперь происходит автоматически благодаря /opt/etc/init.d/S01syslog-ng.
Настройка syslog-ng
Мой syslog-ng помимо файлов логирует еще и на рабочую машину, где стоит Kiwi Syslog в качестве приемника. Просто
привожу конфиг /opt/etc/syslog-ng.conf:
#
# Syslog-ng example configuration for for NSLU2 Unslung 3.x
#
# Copyright (c) 1999 anonymous
# Copyright (c) 1999 Balazs Scheidler
# $Id: syslog-ng.conf 5159 2007-01-12 20:37:44Z fcarolo $
#
# Syslog-ng configuration file, compatible with default Debian syslogd
# installation.
#
options {
long_hostnames(off);
sync(0);
stats(0);
create_dirs(yes);
time_reopen(10);
log_fifo_size(2000);
};
source src {
pipe("/proc/kmsg" log_prefix("kernel: ") );
unix-stream("/dev/log");
internal();
};
#source net { udp(); };
destination authlog { file("/opt/var/log/auth.log"); };
destination syslog { file("/opt/var/log/syslog"); };
destination cron { file("/opt/var/log/cron.log"); };
destination daemon { file("/opt/var/log/daemon.log"); };
destination kern { file("/opt/var/log/kern.log"); };
destination lpr { file("/opt/var/log/lpr.log"); };
destination user { file("/opt/var/log/user.log"); };
destination uucp { file("/opt/var/log/uucp.log"); };
destination ppp { file("/opt/var/log/ppp.log"); };
destination mail { file("/opt/var/log/mail.log"); };
destination mailinfo { file("/opt/var/log/mail.info"); };
destination mailwarn { file("/opt/var/log/mail.warn"); };
destination mailerr { file("/opt/var/log/mail.err"); };
destination newscrit { file("/opt/var/log/news/news.crit"); };
destination newserr { file("/opt/var/log/news/news.err"); };
destination newsnotice { file("/opt/var/log/news/news.notice"); };
destination debug { file("/opt/var/log/debug"); };
destination messages { file("/opt/var/log/messages"); };
destination udp_machinename { udp( your.ip.add.rr port(514) template("$DATE $FULLHOST [$FACILITY.$LEVEL] $MESSAGE\n") template_escape(no) ); };
destination alllog { file("/opt/var/log/all_$R_YEAR-$R_MONTH-$R_DAY.log" template("$DATE $FULLHOST [$FACILITY.$LEVEL] $MESSAGE\n") template_escape(no) ); };
filter f_auth { facility(auth); };
filter f_authpriv { facility(auth, authpriv); };
filter f_syslog { not facility(authpriv, mail); };
filter f_cron { facility(cron); };
filter f_daemon { facility(daemon); };
filter f_kern { facility(kern); };
filter f_lpr { facility(lpr); };
filter f_mail { facility(mail); };
filter f_user { facility(user); };
filter f_uucp { facility(cron); };
filter f_ppp { facility(local2); };
filter f_news { facility(news); };
filter f_debug { not facility(auth, authpriv, news, mail); };
filter f_messages { level(info..warn) and not facility(auth, authpriv, mail, news); };
filter f_emergency { level(emerg); };
filter f_info { level(info); };
filter f_notice { level(notice); };
filter f_warn { level(warn); };
filter f_crit { level(crit); };
filter f_err { level(err); };
log { source(src); filter(f_authpriv); destination(authlog); };
log { source(src); filter(f_syslog); destination(syslog); };
log { source(src); filter(f_cron); destination(cron); };
log { source(src); filter(f_daemon); destination(daemon); };
log { source(src); filter(f_kern); destination(kern); };
log { source(src); filter(f_lpr); destination(lpr); };
log { source(src); filter(f_mail); destination(mail); };
log { source(src); filter(f_user); destination(user); };
log { source(src); filter(f_uucp); destination(uucp); };
log { source(src); filter(f_mail); filter(f_info); destination(mailinfo); };
log { source(src); filter(f_mail); filter(f_warn); destination(mailwarn); };
log { source(src); filter(f_mail); filter(f_err); destination(mailerr); };
log { source(src); filter(f_news); filter(f_crit); destination(newscrit); };
log { source(src); filter(f_news); filter(f_err); destination(newserr); };
log { source(src); filter(f_news); filter(f_notice); destination(newsnotice); };
log { source(src); filter(f_debug); destination(debug); };
log { source(src); filter(f_messages); destination(messages); };
log { source(src); filter(f_ppp); destination(ppp); };
log { source(src); destination(udp_machinename); };
log { source(src); destination(alllog); };
Создание пользователей
Добавляю командой adduser (можно и напрямую править /etc/passwd и
/etc/group). Не забываю сделать
echo "/etc/passwd" >> /usr/local/.files
echo "/etc/group" >> /usr/local/.files
flashfs save && flashfs commit && flashfs enable
Домашние папки создаю в /opt/home.
Часть 2: Hacking WL500gP Part 2