วันศุกร์ที่ 17 ธันวาคม พ.ศ. 2553

FreeBSD : Basic user management

ระบบที่ใช้ทดสอบ FreeBSD 8.1-RELEASE amd64

- การเพิ่มบัญชีผู้ใช้ด้วยคำสั่ง adduser

FreeBSD# adduser
Username: user1
Full name: User1 Fullname
Uid (Leave empty for default):
Login group [user1]:
Login group is user1. Invite user1 into other groups? []:
Login class [default]:
Shell (sh csh tcsh nologin) [sh]:
Home directory [/home/user1]:
Home directory permissions (Leave empty for default):
Use password-based authentication? [yes]:
Use an empty password? (yes/no) [no]:
Use a random password? (yes/no) [no]:
Enter password: (ขณะที่พิมพ์จะมองไม่เห็น)
Enter password again: (ขณะที่พิมพ์จะมองไม่เห็น)
Lock out the account after creation? [no]:
Username : user1
Password : *****
Full Name : User1 Fullname
Uid : 1002
Class :
Groups : user1
Home : /home/user1
Home Mode :
Shell : /bin/sh
Locked : no
OK? (yes/no): yes
adduser: INFO: Successfully added (user1) to the user database.
Add another user? (yes/no): no
Goodbye!

- เปลี่ยนรหัสผ่านของ user1

FreeBSD# passwd user1
Changing local password for user1
New Password: (ขณะที่พิมพ์จะมองไม่เห็น)
Retype New Password: (ขณะที่พิมพ์จะมองไม่เห็น)

- แสดงข้อมูลของ user1

FreeBSD# id user1
uid=1002(user1) gid=1002(user1) groups=1002(user1)

หรือ

FreeBSD# pw user show user1
user1:*:1002:1002::0:0:User1 Fullname:/home/user1:/bin/sh

- แก้ไขข้อมูล user1

FreeBSD# chpass user1
#Changing user information for user1.
Login: user1
Password: $1$oU.ybLTJ$V5GWxT1oXN/emCRnqgx.h0
Uid [#]: 1002
Gid [# or name]: 1002
Change [month day year]:
Expire [month day year]:
Class:
Home directory: /home/user1
Shell: /bin/sh
Full Name: User1 Fullname
Office Location:
Office Phone:
Home Phone:
Other information:

โดยจะอยู่ในรูปแบบของคำสั่ง vi


- ลบ user1 ออกจากระบบโดยคำสั่ง rmuser

FreeBSD# rmuser
Please enter one or more usernames: user1
Matching password entry:

user1:*:1002:1002::0:0:User1 Fullname:/home/user1:/bin/sh

Is this the entry you wish to remove? y
Remove user's home directory (/home/user1)? y
Removing user (user1): mailspool home passwd.

วันพฤหัสบดีที่ 16 ธันวาคม พ.ศ. 2553

Cisco, Security : banner exec และ banner motd

โจทย์ที่ต้องการ
ข้อความเตือนก่อนการ Login เข้าสู่ระบบ โดยต้องไม่บอกว่าเป็นระบบอะไร ทำอะไรอยู่ และข้อมูลอื่นๆ ที่อาจมีผลต่อความปลอดภัย

ข้อความแสดงรายละเอียดของระบบ หลังจากที่สามารถทำการ Login ได้แล้ว เช่น สถานที่ตั้งของระบบ หรือชื่อผู้ดูแลระบบ เป็นต้น

ระบบที่ใช้ทดสอบ Cisco 3660 on GNS3

ตัวอย่าง running-config

!
banner exec ^C


System Function : Core Router
System Location : Bangkok
System administrator : komkit.net

^C
banner motd ^C

!!!WARNING!!!
READ THIS BEFORE ATTEMPTING TO LOGON

This System is for the use of authorized users only. Individuals
using this computer without authority, or in excess of their authority,
are subject to having all of their activities on this system monitored
and recorded by system personnel. In the course of monitoring individuals
improperly using this system, or in the course of system maintenance,
the activities of authorized users may also be monitored. Anyone using
this system expressly consents to such monitoring and is advised that if
such monitoring reveals possible criminal activity, system personnel may
provide the evidence of such monitoring to law enforcement officials.

^C
!




ผลการทดสอบ

LocalRouter#telnet 10.0.0.1
Trying 10.0.0.1 ... Open


!!!WARNING!!!
READ THIS BEFORE ATTEMPTING TO LOGON

This System is for the use of authorized users only. Individuals
using this computer without authority, or in excess of their authority,
are subject to having all of their activities on this system monitored
and recorded by system personnel. In the course of monitoring individuals
improperly using this system, or in the course of system maintenance,
the activities of authorized users may also be monitored. Anyone using
this system expressly consents to such monitoring and is advised that if
such monitoring reveals possible criminal activity, system personnel may
provide the evidence of such monitoring to law enforcement officials.



User Access Verification

Username: komkit
Password:


System Function : Core Router
System Location : Bangkok
System administrator : komkit.net


RemoteRouter>

จากตัวอย่างจะเห็นว่า จะมีข้อความเตือนก่อนการ Login และเมื่อทำการ Login สำเร็จแล้วจึงจะเห็นข้อความอื่นๆ ที่ต้องการแสดงรายละเอียด ตามที่โจทย์ต้องการ

หมายเหตุ
หากทำการตั้งค่าของ banner motd ให้เป็นรายละเอียดของอุปกรณ์ อาจเป็นช่องทางให้ผู้บุกรุกรู้ถึงช่องทางในการบุกรุกระบบของเราได้ ดังนั้น banner motd จึงไม่ควรเป็นข้อความใดๆ ที่เป็นการเชื้อเชิญ หรือเปิดเผยข้อมูลของระบบ แต่ถ้าต้องการแสดงรายละเอียดของระบบก็ควรใส่ใน banner exec จะเหมาะสมกว่า

วันอังคารที่ 14 ธันวาคม พ.ศ. 2553

Linux : เปลี่ยนชื่อ Username และชื่อ Group

ในกรณีที่ต้องการเปลี่ยนชื่อ Login เช่น จากเดิมชื่อ user1 แต่ต้องการเปลี่ยนเป็น admin1 โดยที่ UID ยังคงเป็นหมายเลขเดิม สามารถทำได้ดังนี้

ระบบที่ใช้
OS : Ubuntu Linux 10.04.1 x64 LTS

ตรวจสอบ User เดิมด้วยคำสั่ง id
root@Linux:~# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)


ทำการเปลี่ยนชื่อ user1 ไปเป็น admin1 ด้วยคำสั่ง usermod

root@Linux:~# usermod -l admin1 user1

ทำการตรวจสอบ User

root@Linux:~# id user1
id: user1: No such user

root@Linux:~# id admin1
uid=1001(admin1) gid=1001(user1) groups=1001(user1)

จากการตรวจสอบจะพบว่า Login ชื่อ User1 ไม่มีในระบบแล้ว ซึ่งได้เปลี่ยนเป็น admin1 แทน และยังคงใช้ uid และ Group เดิมอยู่

จากนั้นหากต้องการเปลี่ยนชื่อ Group ด้วย โดยจากเดิมชื่อ Group คือ user1 เปลียนไปเป็น admin1 แต่ยังต้องการคง Group ID ใว้เหมือนเดิม สามารถทำได้ดังนี้

root@Linux:/home# groupmod -n admin1 user1

ทำการตรวจสอบอีกครั้ง
root@Linux:/home# id admin1
uid=1001(admin1) gid=1001(admin1) groups=1001(admin1)

จะเห็นว่าทั้ง Username และ Groupname เปลี่ยนไปเป็น admin1 ตามที่ต้องการแล้ว และยังคง uid และ group id เดิมไว้


หมายเหตุ
ทั้งนี้การเปลี่ยนชื่อ Login และ Group จะไม่มีผลต่อ Configuration อื่นๆ ดังนั้นครตามไปเปลี่ยนให้เรียบร้อยด้วย เช่น Home Directory ก็ยังคงเป็น /home/user11 เหมือนเดิม

admin1:x:1001:1001:User1,,,:/home/user1:/bin/bash

ซึ่งอาจต้องทำการเปลี่ยนตาม เพื่อให้สะดวกในการใช้งาน ดังตัวอย่าง

root@Linux:~# mv /home/user1 /home/admin1

root@Linux:~# usermod -d /home/admin1 admin1

จาก 2 คำสั่งข้างต้นจะทำการเปลี่ยน Home Directory ไปเป็น /home/admin1 ตามต้องการ

admin1:x:1001:1001:User1,,,:/home/admin1:/bin/bash

วันจันทร์ที่ 13 ธันวาคม พ.ศ. 2553

Linux, Secure Shell : ระบุเวลาในการใช้งาน SSH

ในกรณีที่ต้องการระบุเวลาในการใช้งาน SSH เช่น ต้องการอนุญาตให้ user1 สามารถ Login ผ่าน SSH ได้เฉพาะวันที่จันทร์ - ศุกร์ เวลา 08.00 - 15.00 เท่านั้น ส่วนวันและเวลานอกนั้นไม่อนุญาตให้ Login ได้ เป็นต้น ซึ่งในกรณีนี้จะใช้ความสามารถของ PAM เข้ามาช่วย

ระบบที่ใช้ทดสอบ
OS : Ubuntu Linux 10.04.1 x64 LTS

โจทย์ที่ต้องการ
อนุญาตให้ user1 สามารถ login ผ่าน SSH ได้ตามเวลาที่กำหนด คือ วันจันทร์ - ศุกร์ เวลา 08.30 - 15.00 เท่านั้น

เริ่มด้วยแก้ไขไฟล์ /etc/pam.d/sshd โดยเพิ่ม

account required pam_time.so

จากนั้นแก้ไขไฟล์ /etc/security/time.conf โดยเพิ่ม

sshd;*;user1;MoTuWdThFr0830-1755

จากนั้นทำการ restart SSH

root@Linux:~# /etc/init.d/ssh restart

จากนั้นทำการทดสอบใช้งาน จะพบว่า user1 ไม่สามารถ login ได้ ส่วน user อื่นยังสามารถใช้งานได้ตามปกติ

ตัวอย่าง Log ที่เกิดขึ้นจากการ login ของ user1 นอกเวลาที่กำหนดไว้

Dec 13 18:06:34 Linux sshd[3471]: Failed password for user1 from 172.17.12.51 port 62763 ssh2

ข้อสังเกตุ
หาก user1 ได้ทำการ login ในเวลาที่กำหนด แต่ยังคง Session ไว้จนเกินเวลาที่กำหนด ระบบจะไม่ตัด Session นั้นทิ้ง

วันพุธที่ 1 ธันวาคม พ.ศ. 2553

Shell Script : expect - programmed dialogue with interactive programs

ตัวอย่างการใช้งาน expect เพื่อใช้ในการ ssh หรือ telnet แบบอัตโนมัติไปยังอุปกรณ์ที่ต้องการ และสั่งให้อุปกรณ์นั้นทำงานตามคำสั่งที่กำหนดไว้ล่วงหน้า เสมือนว่าเราเป็นพิมพ์คำสั่งเอง (ประมาณว่าทำ bot)

ระบบที่ใช้ทดสอบ
Telnet Client : Ubuntu Linux โดยเครื่องนี้จะเป็น Bot และทำการ remote ไปยังเครื่องเป้าหมาย และพิมพ์คำสั่งต่าง ตามที่ได้กำหนดไว้ก่อนแล้ว
Telnet Server : Cisco Catalyst 3560 โดยเครื่องนี้จะถูก bot ทำการ remote เข้ามาเก็บข้อมูลที่ต้องการ

ตัวอย่างโจทย์ : สมมุติว่า ต้องการเก็บข้อมูล Mac address ที่อยู่บน Interface FastEthernet 0/1 ของ Switch เป้าหมายทุกๆ 1 ชั่วโมง และเป็นแบบอัตโนมัติ

วิธีการแบบ Manual : ทำการ Telnet ไปยัง Switch ที่ต้องการ แล้วใช้คำสั่ง show mac-address-table interface fastEthernet 0/1 แล้วเก็บ Output ที่ได้ โดยวิธีการนี้ ผู้ได้รับมอบหมายต้อง Telnet เองทุกๆ 1 ชั่วโมง ดังตัวอย่าง

komkit@Linux:~$ telnet 192.168.0.1
Trying 192.168.0.1...
Connected to 192.168.0.1.
Escape character is '^]'.
User Access Verification

Username: komkit
Password:
Switch> show mac-address-table interface fastEthernet 0/1
Mac Address Table
-------------------------------------------

Vlan Mac Address Type Ports
---- ----------- -------- -----
1 001a.a026.466b DYNAMIC Fa0/1
1 001a.a026.466d DYNAMIC Fa0/1
Total Mac Addresses for this criterion: 2

Switch> exit
Connection closed by foreign host.
komkit@Linux:~$

จากวิธีการข้างต้น ผู้ดูแลระบบต้องทำการ telnet เข้าไปเองทุกๆ 1 ชั่วโมง เพื่อให้ได้ข้อมูลที่ต้องการ


วิธีการแบบอัตโนมัติ โดยใช้ expect บน Linux
แนวคิดคือ ทำเหมือนว่าเป็นคน Telnet เอง แต่ว่าจะให้ระบบ (bot) เป็นคนพิมพ์ให้
เริ่มด้วย ติดตั้ง expect

root@Linux:~# apt-get install expect

จากนั้นทำการสร้าง Script เพื่อให้ทำงานแทนเรา ดังตัวอย่าง

root@Linux:~# vi bot_telnet

#!/usr/bin/expect --
spawn telnet 192.168.0.1
expect "Username: "
send "komkit\r"
expect "pssword: "
send "TelnetPassword\r"
expect "Switch>"
send "show clock \r"
expect "Switch>"
send \r
expect "Switch>"
send "show mac-address-table interface fastEthernet 0/1 \r"
expect "Switch>"
send \r
expect "Switch>"
send exit\r
expect closed
exit 0

โดยค่าที่สนใจมีดังนี้

spawn คือ เริ่มทำงาน
expect คือ Prompt ที่สนใจ
send คือ เมื่อเจอ prompt ตาม expect ข้างต้นแล้ว ให้ส่ง Message หรือ Command เข้าไป (เหมือนว่าเรากำลังพิมพ์)
\r คือ กด Enter

ทำการ Save ไฟล์ แล้วทำการทดสอบดังนี้

root@Linux:~# expect bot_telnet
spawn telnet 192.168.0.1
Trying 192.168.0.1...
Connected to 192.168.0.1.
Escape character is '^]'.
User Access Verification

Username: komkit
Password:
Switch>show clock
12:23:09.058 GMT+7 Thu Dec 2 2010
Switch>
Switch>show mac-address-table interface fastEthernet 0/1
Mac Address Table
-------------------------------------------

Vlan Mac Address Type Ports
---- ----------- -------- -----
1 001a.a026.466b DYNAMIC Fa0/1
1 001a.a026.466d DYNAMIC Fa0/1
Total Mac Addresses for this criterion: 2
Switch>
Switch>exit
Connection closed by foreign host.
root@Linux:~#

จาก Output ดังกล่าวจะเห็นว่า เราได้ข้อมูลตามต้องการ ต่อไปก็นำ Script ดังกล่าวให้ทำงานโดย cron แล้วเก็บ Output ลงไฟล์ เช่น

1 * * * * root expect bot_telnet >> /home/komkit/Switch_MAC.log

เมื่อ Admin ต้องการดูข้อมูล ก็เข้าไปดู File ดังกล่าวได้ตลอดเวลา