雙倍奇多
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # linux 的系統安全強化 ###### tags: `小專題` `unix` `大二` [Raspberry Pi 筆記(58):系統安全強化(上)用戶及連線篇](https://atceiling.blogspot.com/2020/02/raspberry-pi-58.html)) [Raspberry Pi 筆記(58):系統安全強化(中) 安裝防火牆及Fail2Ban](https://atceiling.blogspot.com/2020/02/raspberry-pi-58-fail2ban.html) [Raspberry Pi 筆記(58):系統安全強化(下) - 安裝防毒軟體Clamav](https://atceiling.blogspot.com/2020/02/raspberry-pi-58-fail2ban.html) ## Step 1 :要先更改密碼 ( 但我沒更改 ) ```bash= passwd 或是 sudo raspi-config ``` ![](https://i.imgur.com/6u2mx1j.png) ## Step 2 :更改用戶名 ```bash= sudo adduser tina ``` ![](https://i.imgur.com/9TJvkw8.png) 把必要的權限賦予給 tina 這位用戶、測試帳號是否可用,權限是否有問題: ```bash= sudo usermod -a -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,gpio,i2c,spi tina sudo su - tina ``` :::success 如果成功從 pi 轉換到 tina 這個用戶就成功 ::: ![](https://i.imgur.com/hkg3lZ0.png) 切換成功之後就可以把原本的使用者 pi 刪掉 (但我沒刪) ```bash= sudo pkill -u pi sudo deluser pi # 只刪掉pi帳號 sudo deluser -remove-home pi # 同時把pi & /home/pi的資料夾刪掉 ``` ## Step 3 :sudo 時需要密碼 ```bash= sudo nano /etc/sudoers.d/010_pi-nopasswd ``` ![](https://i.imgur.com/WyxmBOx.png) ## Step 4 :更新最新的安全修正程式 ```bash= sudo apt update sudo apt full-upgrade ``` ## Step 5 :改善SSH安全性 更改配置來允許或拒絕特定用戶使用 SSH連線 ```bash= sudo nano /etc/ssh/sshd_config AllowUsers tina max #在設定檔案的最後面,加上允許登錄的用戶名稱 DenyUsers rex kent #也可以使用 DenyUsers 來阻止某些用戶登錄 sudo systemctl restart ssh #要重新開機才可以生效 - 我不會所以手動關機 ``` ## Step 6 :檢查已經開啟的服務埠 ```bash= netstat -tulnp ``` ![](https://i.imgur.com/RohQsvU.png) ## Step 7 :安裝 ufw 防火牆、啟動 ```bash= sudo apt-get install ufw sudo ufw enable ``` ![](https://i.imgur.com/qRw3v17.png) 關閉防火牆: ```bash= sudo ufw disable ``` ![](https://i.imgur.com/xx9ABzA.png) 查看防火牆狀態 ( 有啟用 - active , 沒有啟用 - inactive ) : ```bash= sudo ufw status ``` ![](https://i.imgur.com/R3tJhGn.png) ## Step 8 :對服務或連接埠設規則 ```bash= Step 8 :對服務或連接埠設規則 ``` ![](https://i.imgur.com/IZI6fMo.png) ufw 刪除,在原本的規則前面加上 delete : ```bash= sudo ufw delete allow http ``` ![](https://i.imgur.com/3SekC5t.png) 如果要進一步指定 tcp 連線的話: ```bash= sudo ufw allow http/tcp ``` ![](https://i.imgur.com/nyBP4Uk.png) 其他: ```bash= sudo ufw deny out ssh # 限制無法從樹莓派 ssh 到其他主機 Rule added Rule added (v6) sudo ufw status # 看一下執行的結果 Status: active To Action From -- ------ ---- 22/tcp DENY OUT Anywhere 22/tcp (v6) DENY OUT Anywhere (v6) sudo ufw delete deny out ssh # 要刪除上述規則 Rule deleted Rule deleted (v6) ``` ## Step 9 :對IP設定規則 ```bash= sudo ufw deny from 192.168.1.10 # 拒絕 192.168.1.10 這個 IP 連線本機所有 Port sudo ufw deny from 192.168.1.10 port 22 # 拒絕對這個來源IP使用 ssh 連線 sudo ufw allow from 192.168.0.0/24 # 允許192.168.0 這個 C Class網段的設備連線這部主機 sudo ufw deny from any to 192.168.1.10 port 22 # 禁止所有連入的 ssh 連線 ``` ## Step 10 :利用編號刪除規則 ```bash= sudo ufw status numbered # 利用參數將 ufw 的規則用編號列出 ``` ![](https://i.imgur.com/5LFbowi.png) ```bash= sudo ufw delete 2 # 如要刪除該編號規則 ``` ![](https://i.imgur.com/c40d4pR.png) ## Step 11 :安裝圖形介面防火牆 gufw ```bash= sudo apt-get install gufw sudo gufw ``` ![](https://i.imgur.com/jtFqIor.png) ```bash= sudo ufw logging on sudo ufw logging off ``` ![](https://i.imgur.com/OwzPVvS.png) 調整日誌紀錄等級:ufw 日誌等級有 low、medium、high、full 四種,預設為 low。low資訊較少,full資訊最多,也站較多硬碟空間,要改成medium ```bash= sudo ufw logging medium ``` ## Step 12 :安裝fail2ban ```bash= sudo apt-get install fail2ban # 先將jail.conf檔案複製成 jail.local,再開啟編輯器修改 sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sudo nano /etc/fail2ban/jail.local ``` 在檔案最後面加上以下幾行: ```bash= enabled  = true #啟動fail2ban maxretry = 3 # 允許發生入侵或登入行為時,可以嘗試失敗的次數 bantime = 86400 # 封鎖IP的秒數,設定成 -1時會永久封鎖 findtime = 7200 #搜尋指定日誌的範圍秒數 destemail = root@localhost #要收到通知的郵件收件人 ``` 啟用、停用 fail2ban : ```bash= sudo service fail2ban start #啟用 sudo service fail2ban stop #停用 ``` ## Step 13 :安裝ClamAv防毒軟體 ```bash= sudo apt-get install clamav clamav-daemon sudo nano /etc/clamav/clamd.conf #如果要用 Daemon的模式進行,需要編輯設定檔 clamd.conf ,可依照個人使用需要修改此設定檔的內容 sudo service clamav-daemon start # 在Daemon模式下,如要啟動 ClamAv sudo service clamav-daemon stop # 停止 ClamAv 服務 ``` 要再執行一次才可以正常啟動服務: ```bash= systemctl daemon-reload ``` 查看服務是否正常: ```bash= sudo systemctl status clamav-daemon ``` ![](https://i.imgur.com/YjppTsj.png) 檢查 clamscan 版本: ```bash= clamscan -V ``` ![](https://i.imgur.com/lAZG6vM.png) ## Step 14 :更新病毒碼 ```bash= sudo freshclam ``` ![](https://i.imgur.com/9dFQHCd.png) 上圖,發生了一點問題,因為日誌檔被其他程序鎖定: 所以要先把clamav-freshclam的服務停掉,等執行完病毒碼更新後,再重新啟動服務 ```bash= # 停用服務 sudo systemctl stop clamav-freshclam.service 或 sudo /etc/init.d/clamav-freshclam stop # 啟用服務 sudo systemctl start clamav-freshclam.service 或 sudo /etc/init.d/clamav-freshclam start ``` :::danger 輸入指令的流程應該為:先停用clamav-freshclam的服務,更新病毒碼,再重新啟用服務 ::: ![](https://i.imgur.com/Dq0MNwO.png) ## Step 15 :圖形介面ClamAv 除了命令列及 Daemon 模式外,還有一種圖形介面模式,稱做 ClamTk,ClamTk 是 ClamAV 的圖形界面,ClamTk 本身是不包含防毒軟體的,ClamTk 就是利用 ClamAV 的病毒掃瞄引擎及病毒碼來提供病毒掃瞄的功能。 如要安裝 ClamTk 時,可執行以下指令,安裝前如果還沒有安裝 ClamAv ,執行安裝 ClamTk 時,會一併將 ClamAv 安裝 ```bash= sudo apt-get install clamtk clamtk ``` ![](https://i.imgur.com/9gILAVj.png) ## Step 16 :檔案掃描 要掃描 /home/USER 目錄內的檔案,並將受感染的檔案放到 /home/USER/VIRUS: -r 為遞迴掃描,包含底下所有的子目錄 :::danger 注意注意,要先建一個叫做virus的資料夾 ::: ```bash= sudo clamscan -r --move=/home/pi/virus /home/pi ``` ![](https://i.imgur.com/yqfLXRY.png) **這樣就完成ㄌ掃描檔案的部分** 因為忘記建一個virus的資料夾所以沒有把資料存進去,乾 ![](https://i.imgur.com/Em39AA9.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully