陳又瑀
    • 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
      • Invitee
    • Publish Note

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

      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.
      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
    • Engagement control
    • 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 Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control 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
Invitee
Publish Note

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

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.
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
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
###### tags: `1111` `lsa` `ncnu` # Week 07 (2022/10/27) [TOC] - Book mode: https://hackmd.io/@ncnu-opensource/book --- ## ssh tunnel > [SSH Tunnel](https://johnliu55.tw/ssh-tunnel.h> [Local Tunnel](https://johnliu55.tw/ssh-tunnel.html) - 基於 SSH protocol 延伸之應 - 類似 VPN :::info ### VPN 將專用網路延伸到公共網路上,使使用者能夠在共享或公共網路上透過專用的tunnel傳送和接收資料,就像他們的電腦直接連接到專用網路上一樣,VPN的好處包括增加專用網路的功能、安全性和管理。 #### VPN 與 SSH Tunnel 的差異 - 還要去安裝 VPN 的伺服器 - VPN 是直接登陸那邊的區網,加入遠端區網,成為區網一份子,得到一個區網內部的 IP - 區網裡的電腦會看到你的虛擬 IP - 如果是 ssh tunnel 那些 IP 就不存在,所有人只會看到你那台跳板電腦的 IP ::: ![](https://i.imgur.com/ok3QP4f.png) - ssh Tunnel範例 - ssh client 和 ssh server 建立連線,架立起 tunnel - tunnel 啟用的情況下,application 對 ssh server 開啟的 port 連線 - ssh client 藉由加密通道將 application 傳送至 server - ssh server 再連線到真正的 application server (通常兩這會在同一台機器) - 在上述情況下,就可以在不更動 application server 下讓 application 跟 server 加密通訊 - port forwarding的角色 - Client - 任何你可以敲 ssh 指令來啟動 port forwarding 的機器 - SSH Server - 可以被Client用ssh連進去的機器 - Target Server - 某一台你想建立連線的機器,通常是為了對外開放 - 最終要到的地方 > 注意: > client 與 SSH server 本身都可以是 target server,不是真的一定要第三台機器 :::info ### nginx - install `sudo apt install nginx` - start `service nginx start` - look up `sudo netstat -ntupl` - change port `sudo vim /etc/nginx/sites-enabled/default` ::: ### Local Tunnel(Local Forwarding) - 將你的電腦連到外面的 server 上,我要去某個網站 - Local Tunnel 就是將一個 ssh server 接收到的 port 轉送到遠端 server - 使用情境 1. 在建立 tunnel 或是檔案傳輸路徑經過跳板 2. 在外網與網的服務串聯 3. 連接一個遠端的雲端資料庫 - 指令參數 - `ssh -L <local IP>:<local port>:<target IP>:<target port> <target server user name>@<target IP>` - 情境一 連到位在放火牆後的開發伺服器上的服務 - 只適用於你在這台開發伺服器上有帳號 - ![](https://i.imgur.com/yEjHaVb.png) - ![](https://i.imgur.com/uVRcQk2.png) 依靠 ssh sever 幫你轉送資料到 8080 port :::info `ps -aux` 列出執行中的proccess - `-A`:所有的process均顯示出來 - `-a`:不予terminal有關process - `-u`:有效使用者(effective user)相關的process - `-x`:通常與a這個參數一起使用,可列出較完整資訊 - `USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND` - `SER`:process擁有者 - `PID`:pid - `%CPU`:占用的CPU使用率 - `%MEM`:占用的記憶體使用率 - `VSZ`:占用的虛擬記憶體大小 - `RSS`:占用的記憶體大小 ::: - 情境二 透過防火牆後的機器,連到防火牆後的特定服務 防火牆後有任何一台你可以 SSH 的機器,接著修改一下指令裡的 host 設定,你就可以利用這台機器進行資料轉送 - ![](https://i.imgur.com/H0J4qqZ.png) localhost一樣有一台你的機器,但是你沒有存放服務機器的權限,無法直接SSH服務 - ![](https://i.imgur.com/NS3Ovos.png) 藉由你的機器當跳板存取服務 ### Remote Tunnel(Remote Forwarding)(reverse Tunnel) - 和 local Tunnel 相反,把本地機器上的服務 port 導入到 server 讓它連線,在Server 端(80 port) 想讀取Client 端 (8080 port) 的本機服務 - 從 service 開到 client,要把 service 提供給別人時,提供網站給別人 - `ssh -R <server IP>:<server port>:localhost:<localhost port> jump@192.168.0.105 -Nf` - 注意需要更改 `sshd_config` 內的 `GatewayPort` > `sshd_config`: SSH 的 server 設定檔 - 情境一:透過對外機器,讓其他人能夠連到你的電腦上的服務 ![](https://i.imgur.com/0VtJVoB.png) - 情境二:透過對外機器,從外面連回內部網路上的服務 ![](https://i.imgur.com/vTudoVa.png) `ssh -R 9090:<internel server ip>:8080 <user name>@<ip>` ### Dynamic forward > remote + local > 可以設定自動 proxy 到你要的 port 去 :::info 只要有一台位於內網且具有外部 IP 的機器,你就可以利用這個方法建立一個 HTTP 代理伺服器,讓你能夠從外面連回內網裡的所有 HTTP(S) 服務 ::: ![](https://i.imgur.com/AwaA591.png) - 中間開一個 proxy,請他轉送,就不用開兩條 tunnel - 可偽裝連線來源 ip (**你的操作都會經過被你連的那台主機在通往其他地方**) > 資訊進來時會送到自己在本機開的 port > 資料要出去的時先連線到你借用的主機(跳板 IP),再出去,所以別人追蹤回來不會看到你的 IP,只會看到你借用的主機的 IP - 我連上去,之後所有連線需求都由 proxy 轉送 - `curl <網址>`: 直接幫你連上去指定網址的指令 - `socksify curl <網址>`: 利用指定的 IP 幫你連上去指定網址的指令 - 別人查看你的 IP 的時候就會是你指定 IP ### port attacking - 掃描一台電腦所有的 port ,看看那些 port 有開啟,而每個開啟的 port 也會對應一個使用這個 port 的使用者,通常一些常見的應用程式其開啟的 port 都是固定的,攻擊者就可以從中挑選一些安全性較低,容易入侵得程式進行攻擊,透過這樣的方式入侵到系統中 - 解決方式: firewall, tcp wrapper, port knocking(現在很少用) :::info **port knocking** - Port Knocking 指的是 Client 必須用特殊的順序來對 SSH Server 上的某些 port 發出連線請求後,SSH server 才會開放 client 連線的技巧(比如依序對 port 1000、2000、3000 發出請求,才會對你開放port 22) - >這樣的好處是平時 Port 22 就會是關閉的狀態,讓攻擊者以為 SSH 沒有開放,減少被攻擊的機會。 - 避免人去暴力破解 ![](https://i.imgur.com/fCD1DgI.png) - 優點 - 有效防止 port attacking - 容易移植到其他應用程式 - 缺點: - 伺服器必須告知Client暗號是什麼 - 暗號系統若故障影響重大,可能會有很多個port都不能使用 > [使用 Port Knocking 技術增加伺服器與網路的安全性](http://blogger.gtwang.org/2013/10/port-knocking.html) ::: ### 補充 - `traceroute <網址>`: 告訴你在連上這個網站的過程會經過甚麼地方 - 實作前要去設定檔打開 - gSTM: 圖形化介面的 SSH Tunnel Manager > [SSH Tunnel with gSTM in ubuntu](https://evilc0de.blogspot.com/2009/12/ssh-tunnel-with-gstm-in-ubuntu.html) - `curl + 網址`: 文字介面中一個類似小瀏的小工具 - `socksify + curl + 網址`: 參考系統上的某個設定檔 - `host + 網址`: 查看網站的ip位置 - PortSentry: 幫你隨機掃描 port(port scan) > 阻絕暴力破解法 :muscle: - 同時聽很多個 port,如果指定的幾個 port 被偵測到使用,視為攻擊 - [fail2ban](https://officeguide.cc/linux-fail2ban-protect-ssh-server-tutorial-examples/) : 偵測一個IP嘗試登入多次,發現此IP正在暴力破解,來阻擋此IP - [nmap](https://nmap.org/) : 幫你做 port scan # Web Server ## Introduce :::info What is server? - 提出要求的時候,以任何形式提供回應的,都可以稱為**service**。 - 用來提供任何service的軟體,就可以稱為**server**。 - 種類有很多,database server、file server、**Web server** - Server 的功能很多,有些我們每天都會使用 - 遊戲server、Web Server ::: ### Web Server - First released in 1991(CERN httpd,由 CERN 跟 World Wide Web Consortium(W3C)共同開發) - 透過 http 及其他協定回應 client 端要求的 data - ![](https://i.imgur.com/TsoJzq7.png) - 1970就有網路了,一開始的網路是純文字的介面,用於軍事資料傳輸。。。 - 原先的檔案是一個一個分開的檔案,html 可以將分散的檔案合再一起 - HTTP server: 支援這些服務的 server - client如何看到他們提供的東西? --> 透過瀏覽器 - `telnet lsa.kija.io 80`: 透過80 port去找 ### HTTP ![](https://i.imgur.com/nF5WEBL.png) - **H**yper**t**ext **T**ransfer **P**rotocol - 應用層的協定 > 協定:界定溝通的方式 - 在 client 端跟 server 端 respond & request 的標準 - 預設 port: `80 port` ### port - 埠(ㄅㄨˋ)號 - 用來提供給不同的服務使用,每個 port 提供特定的服務 - 為什麼要分 port? (like郵局窗口) - 假設所有data 傳輸到電腦上,電腦要分辨不同的data需要給不同的服務處理 > 全都塞在一起會爆掉 - port number - 以 16 bits 表示,共有 $2^{16}$ 個可以使用 - 0~1023 需要超級使用者權限才能取得、使用,用於常見的通訊服務 - 0:保留埠 - 常用的 port 及協定會記錄在 `/etc/services` 裡面 - demo: `less /etc/services` - 1024以上不需要超級使用者即可使用 - ![](https://i.imgur.com/rwDRprr.png) :::info **IANA** (Internet Assignment Numbers Authority) - 提供穩定的網際網路和環境 - 隸屬於 ICANN (Internet Corporation for Assigned Names and Numbers) - **定義 port number** ::: ### DNS - Domain Name System - 將網域名稱轉為 ip 位置 or 將 ip 位置轉會成網域名稱 - IP位置太難記,因此用domain name這種比較好記的方式,再由DNS轉為電腦讀得懂的IP位置。 - ![](https://i.imgur.com/SM0wIeK.png) - 目前共有 13 台 root DNS server - 網址從後面(Root,已被省略)開始讀 - root:只需要記國家 - `https://www.im.ncnu.edu.tw/` - tw 國家代號 - edu 接教育類(edu, nctu...) > ![](https://i.imgur.com/obWBG2I.png =200x) ![](https://i.imgur.com/pkjo1QB.png) > [DNS各種角色](https://ithelp.ithome.com.tw/m/articles/10194048) ### 如何查找 DNS - FQDN(Fully Qualified Domain Main) = Host NAME + Domain Name - `/etc/hostname` - 解析順序 1. `/etc/nsswitch.conf`:Which - `files`:local files, such as `/etc/passwd` or `/etc/hosts` 2. `/etc/hosts` - 加上 `1.2.3.4 google.com`,`google.com`會連到IP位置為1.2.3.4的電腦上 > 編輯完 `/etc/hosts` 之後,輸入 `host google.com` 就會連上 1.2.3.4 3. `/etc/resolv.conf` > 此檔案可用來設定 DNS 用戶端要求名稱解析時,所定義的各項內容 - `nslookup lsa.kija.io`: 詢問 lsa.kija.io 的ip位置 - 可以修改 `nameserver`後面的ip,這樣它就會去訪問你指定的ip名稱 - `search`: 如果設定為`ncnu.edu.tw`,則輸入`host www`會補全變成`www.ncnu.edu.tw` - DNS 紀錄類型 - ![](https://i.imgur.com/QZh1oqe.png) >[圖片來源](https://awsl.blog/2021/DNS-Record-Types) - `A`: IPv4 address record - `AAA` : IPv6 address record - `dig lsa.kija.io AAA` - `NS`: 的記錄檔是放在哪台伺服器上的 - `CName`:將別名映射到正確域名 - `MX`:指向 mail server,// 可使用自己的網域名稱作為電子郵件地址 - `dig bluet.org MX` - `PTR`: IP 位置轉為 domain name // 如何使用dig做查詢 - `dig -x <ip位置>` ### 相關指令 - `host` + domain name - 將domain name轉為ip位置 - `host lsa.kija.io` - `dig` + domain name - `dig +trace <domain name>` - 列出追蹤路徑 - 從root DNS sever(13台)開始找,一個一個問,直到問到domain name - `dig <@DNS server>`:: 指定透過哪台DNS server::::::: - `dig domain name <DNS類型>`: 查詢相對應的類型 - `nslookup` - 將 domain name 的 ip 位址 ### 常見的 web server 種類繁多,各有各自適合的使用類型 - Nginx - 佔有記憶體少、穩定性高,可以同時承受很大的流量 - 採模組化設計,有許多模組和第三方模庫 - 設計的目標是為了比 Apache 還要好 - Lighttpd - 適合做檔案傳輸 - 低記憶體占用,低 CPU 負載,速度最佳化 - 支援`cgi` & `fastcgi` - Apache2 - 歷史最悠久 - 以前市占率最高 - 2019 後被 Ngnix 超越 - 模組功能強大 - 佔用資源大 :::success **web server 相關指令** - 安裝: `sudo apt install <web server>` - 查看狀態: `sudo service <web server> status` - 開啟此 server 的運作: `sudo service <web server> start` - 停止此 server 的運作: `sudo service <web server> stop` ::: ## Nginx - 打開瀏覽器打`127.0.0.1` or `localhost` - ![](https://i.imgur.com/EzkUKvt.png) - 設定檔: - `/etc/nginx/sites-available/` 裡的 `default` - sudo vim defualt![](https://i.imgur.com/jwfkvww.png) - ![](https://i.imgur.com/qlJVbt5.png) - 預設 port 80 > `/etc/nginx/sites-enabled/` 裡面也有 `default` 檔 >> 這裡面的 `default` 檔是 **softlink** 到 `/sites-available` 裡面的 `default` 的 >> ![](https://i.imgur.com/glbyNqy.png) ## Lighttpd - 設定檔: `/etc/lighttpd/` 裡面的 `lighttpd.conf` - ![](https://i.imgur.com/Vxs6Pp8.png) - `server.modules`: 預設會開啟的模組 - `server.document-root`: 網頁根目錄 - `server.username`: 用來執行 lighttpd 的身分。如果 lighttpd 以 root 身份啟動,則必須同時指定 `server.username` 和 `server.groupname` 才能讓 lighttpd 放棄權限。 - `server.port`: 開啟的預設 port/ - `index.file.name`: 根目錄裡面有甚麼檔案 ## Apache2 - 設定檔: `/etc/apache2/` 裡面的 `apache2.conf` - ![](https://i.imgur.com/FdBA7sO.png =250x) - ![](https://i.imgur.com/Kdgxjxo.png =250x) - ![](https://i.imgur.com/XBR7XJr.png =250x) - port 的設定檔 - `/etc/apache2/` 裡面的 `ports.conf` ![](https://i.imgur.com/a0dU9mb.png) :::success > 更改設定檔的步驟都是: > 停止服務 $\to$ **備份(怕用壞原本的檔案,檔名後面加 `.backup`)** $\to$ 更改設定檔 $\to$ 重新打開服務[color=#c1db59] ::: - 只要有把 port 號分開,就可以同時開啟三種 web server - ![](https://i.imgur.com/fbT5d8G.jpg) - 複製檔案: 像是特殊權限檔案 `(/etc/shadow)`,密碼檔、設定檔、…等,需要加上 `-a` 或 `-p` 參數,才能完整複製檔案。如果複製過程希望有進度條功能,建議改用 `scp` 或 `rsync` 指令 - `cp -a`: 將檔案的權限也一併複製 - `.backup`: 複製的 conf 檔 - 多加一層副檔名比直接加好 > [Linux常用指令-cp-複製檔案與目錄](http://it.givemepower.net/linux-command-cp-copy-files-and-directories/) ::: danger ## 回家作業 - 三種 web server 都下載,把他們 port 號分開,並且同時運行 :::

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

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

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