2025年6月24日 星期二

Ubuntu 24.04 的 Apache2 設定 Cookies marked as HttpOnly


1.Apache2 啟用 mod_headers.so
$sudo a2enmod headers

2.編輯default-ssl.conf, 設定 HttpOnly;Secure;XSRF
$sudo nano /etc/apache2/sites-available/default-ssl.conf
加入
# HttpOnly;Secure;XSRF
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure;SameSite=strict


3.重啟 Apache2
$sudo service apache2 restart

4.如何檢測是否設定成功
假設該網站為https://www.example.com
$curl -I https://www.example.com

資料來源:

2025年6月19日 星期四

Ubuntu 24.04 的 Apache2 解決 TLS/SSL Weak Cipher Suites

1.apache2 啟用 SSL 模組
$sudo a2enmod ssl

2.使用nmap 檢查本機的漏洞
#掃RDP的3389
$nmap --script ssl-enum-ciphers -p 3389 127.0.0.1

#掃https的443
$nmap --script ssl-enum-ciphers -p 443 127.0.0.1

3.編輯 /etc/apache2/sites-available/default-ssl.conf
$sudo nano  /etc/apache2/sites-available/default-ssl.conf
將下面寫入

# 伺服器使用自己定義的 Cipher 排序(不要讓 client 決定)
SSLHonorCipherOrder on

# 停用 SSL 壓縮(防止 CRIME 攻擊)
SSLCompression off

# 安全 Cipher 套件組合
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305

SSLSessionTickets off

 SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1

 # Security headers
 ## X-Content-Type-Options
 Header set X-Content-Type-Options "nosniff"
    
 ## Content-Security-Policy
 Header set Content-Security-Policy "frame-ancestors 'self';"
    
 ## Strict Transport Security (HSTS)
 Header set Strict-Transport-Security "max-age=31536000; includeSubdomains; preload"


4.檢查設定語法是否正確
$sudo apache2ctl configtest

Ubuntu 24.04 的 Apache2 設定 HTTP Strict Transport Security(HSTS)與檢測方法

1.啟用 apache headers 模組 
$sudo a2enmod headers

2.重新啟動 apache 
$sudo service apache2 restart

3.編輯 /etc/apache2/conf-available/security.conf ,加上 header 設定
$sudo nano /etc/apache2/conf-available/security.conf

    Header always set Strict-Transport-Security "max-age=31536000;includeSubdomains; preload"
步驟3.的方式會將整個設定複雜化,可以直接將
Header always set Strict-Transport-Security "max-age=31536000;includeSubdomains; preload"
寫入到/etc/apache2/sites-available/default-ssl.conf 。這樣設定會比較單純。所以
3.1 編輯 /etc/apache2/sites-available/default-ssl.conf

Header always set Strict-Transport-Security "max-age=31536000;includeSubdomains; preload"


4.重新載入設定 
$sudo service apache2 reload

5.檢測https://www.example.com.tw指令:
$curl -s -D - https://www.example.com.tw -o /dev/null

資料來源:



Laravel 網站遇到Host header attack 解決方法及python檢測漏洞方法

1.設定.env的APP_ENV為production
APP_ENV=production

2.問Chat everywhere 的 prompt
host header attack apache 解決方案
host header attack apache 解決方案 https
host header attack apache 解決方案 https://www.example.com.tw
經過上述三段式的prompt,就可得到比較完整

步驟(1).修改/etc/apache2/sites-available/000-default.conf
確保 Apache 僅接受您擁有的域名的請求
<VirtualHost *:80>
    ServerName www.example.com.tw
    ServerAlias example.com.tw

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com\.tw$ [NC]
    RewriteRule ^ - [F]

<Directory /home/webadmin/html/example/public> Options -Indexes AllowOverride All Require all granted </Directory>

Redirect permanent / https://www.example.com/

</VirtualHost>

步驟(2).修改/etc/apache2/sites-available/default-ssl.conf
<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com

<Directory "/home/webadmin/html/example/public/"> Options -Indexes AllowOverride All Require all granted </Directory>

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ - [F]
    
    SSLEngine on
    SSLCertificateFile /path/to/certificate.crt
    SSLCertificateKeyFile /path/to/private.key
    SSLCertificateChainFile /path/to/chainfile.pem
</VirtualHost>

3.問Chat everywhere 的 prompt
給我一個python 檢測 https://www.example.com.tw 的host header attack 漏洞
安裝套件:
pip install requests

檢測程式:
import requests

# 目標 URL
url = "https://www.example.com.tw"

# 攻擊的 Host 標頭
malicious_host = "malicious.com"

# 自定義請求頭
headers = {
    "Host": malicious_host
}

try:
    # 發送請求
    response = requests.get(url, headers=headers)

    # 檢查響應
    if response.status_code == 200:
        print(f"可能存在 Host Header Attack 漏洞,響應內容:\n{response.text}")
    else:
        print(f"響應碼:{response.status_code},未檢測到漏洞。")
except requests.exceptions.RequestException as e:
    print(f"請求出錯:{e}")

測試結果:
資料來源:

2025年6月10日 星期二

Laravel 12 Migration 遷移-資料庫版本控制

Migration 是名詞,代表的是準備用來變更資料庫結構的檔案,但本身無法執行。 Migrate 是動詞,它是用來執行 Migration 的內容。

一、預設的Migration指令
指令:php artisan migrate

二、完成migrations的檔案位置:
database/migrations/
三、設定資料庫為mysql
1.修改.env設定

2.在命令提示字元,打上 
php artisan migrate

3.在網址打上:http://localhost:8080/phpmyadmin/index.php?route=/database/structure&db=students
就可以看到新增students資料庫,裡面資料表已建立完成。
四、如何建立自己的migration
1.指令:
php artisan make:migration StudentsTable

就會建立自己的migration,但是內部的程式碼只有兩個空函式,分別是up與down。

2.指令:
php artisan make:migration create_students_table

就會建立自己的migration,但是內部的程式碼只有兩個函式,分別是up與down,並建立基本的欄位。

3.在函式up裡面,出現兩個欄位 id() 與 timestamps() 
            $table->id();
            $table->timestamps();
其中 id() 是主鍵,timestamps()是時間戳記。
此時,下指令:php artisan migrate
就會得到三個欄位,分別是id、created_at、updated_at
如果想了解更多,請到https://laravel.com/docs/12.x/migrations#creating-columns。
接下來新增三個欄位
            $table->string('name');
            $table->string('email');
            $table->integer('age');
然後下指令:php artisan migrate
這時如果出現Nothing to migrate
則可以先用 
php artisan migrate:rollback
再用
php artisan migrate
就可以得到相對應的資料表欄位

五、若要在"使用中的資料表"中新增欄位,要怎麼做呢?
1.在命令提示列中,
php artisan make:migration addDateOfBirthToStudents --table=students

2.在新增的2025_06_27_070357_add_date_of_birth_to_students.php進行編輯

    $table->dateTime('date_of_birth')->nullable();
$table->enum('gender', ['f','m'])->default('m');
與
            $table->dropColumn('date_of_birth');
            $table->dropColumn('gender');
如下圖:

接下來使用
php artisan migrate


3.在命令提示列中,
php artisan make:migration addUserIdToStudents --table=students
此時會新增2025_06_29_050439_add_user_id_to_students.php
裡面會有兩個func,分別是up()與down()
up()要加入限制
 $table->foreignId('user_id')->constrained('users')->onDelete('cascade')->onUpdate('cascade');
down()要加入
$table->dropforeign(['user_id']);
$table->dropColumn(['user_id']);
六、取消變更的指令
1.取消變更的指令:
php artisan migrate:rollback

2.取消變更幾次的指令:
php artisan migrate:rollback --step=2


3.取消變更到初始位置的指令:
php artisan migrate:reset

接下來再來處理:
                            php artisan migrate

就會建立起所有的表格。

4.清除所有變更再建立起所有的表格的指令:
php artisan migrate:fresh

該指令的效果php artisan migrate:reset + php artisan migrate



資料來源:
1.Laravel 12 – Introduction to Migrations: Manage Your Database Efficiently


2025年6月6日 星期五

laravel 12 Controller 控制器

Controller 用來整理請求邏輯,將相關邏輯放在一個類別裡。 例如, UserController 處理所有與使用者有關的請求。 包括顯示 (showing)、創建 (creating)、更新 (updating)和刪除 (deleting)使用者等等。


一、Controller 控制器
1.指令建立控制器

php artisan make:controller StudentController 

 
2.在app/Http/Controllers/StudentController.php
新增一個函式
    public function index() {
        return "hello from controller.";
    }
可以看到

3.在routes/web.php
Route::get('students',[StudentController::class,'index']); 
可以看到

4.網址列打上http://127.0.0.1:8000/students
就可以看到
 

二、兩個同樣控制器的Route,該如何合併成一個Route
1.在app/Http/Controllers/StudentController.php
新增一個函式

    public function aboutus(){
        return 'Code with Mark.';
    }
2.在routes/web.php
新增一條規則
Route::get('aboutus', [StudentController::class, 'aboutus']);


3.網址列打上http://127.0.0.1:8000/aboutus
就可以看到

4.兩個同樣控制器的Route,合併成一個Route
在routes/web.php 合併成一個Route
Route::controller(StudentController::class)->group(function () {
    Route::get('student', 'index');
    Route::get('aboutus', 'aboutus');
});

5.網址列打上http://127.0.0.1:8000/aboutus、http://127.0.0.1:8000/student

三、傳送資料到控制器
1.在routes/web.php |
修改

    Route::get('aboutus/{id}/{name}','aboutus');
2.在app/Http/Controllers/StudentController.php
修改
    public function aboutus($id,$name){
        return 'ID No ' . $id .' Name ' . $name;
    }



3.網址列打上http://127.0.0.1:8000/aboutus/1/Mark



四、傳送資料到視圖View
1.在routes/web.php |
修改

Route::controller(StudentController::class)->group(function () {
    Route::get('student', 'index');
    Route::get('aboutus/{id}/{name}','aboutus');
});
2.在app/Http/Controllers/StudentController.php
修改
    public function aboutus($id,$name){
        return view('aboutus', compact('id', 'name'));
    }

3.在resources/aboutus.blade.php
修改
                <section>
                    <h2>About Us</h2>
                    <p>This is simple HTML and CSS template to start your project.</p>
                    <p>Name:{{$name}}</p>
                    <p>ID:{{$id}}</p>
                </section>

其中resources/layouts/app.blade.php

resources/aboutus.blade.php
4.網址列打上http://127.0.0.1:8000/aboutus/1/Mark

Ubuntu 26.04:Apache + Django + MySQL 正式部署

系列文章: 1.在 Ubuntu 26.04 建立 Django 開發環境 2.Ubuntu 26.04:Apache + Django + MySQL 正式部署 以 Apache 2.4、 mod_wsgi 、Python 虛擬環境及 MySQL 8.4 部署 D...