顯示具有 apache 標籤的文章。 顯示所有文章
顯示具有 apache 標籤的文章。 顯示所有文章

2025/06/26

本次的ZAP掃出以下的中風險







去年已經出現 本來想說去年應該已經解決了
但今年還是看到 可能是解決不完整

先在 apache 的 CSP 設定內加入以下二行

frame-ancestors 'self';
form-action 'self';

再來處理 style-src
先把 'unsafe-inline' 拿掉
接下來處理的方式有二種

如果在html檔裡直接定義 css 

要使用 hash 或 nonce
使用 nonce 是比較方便的做法

首先在 apache config 裡

style-src 這個定義加上 'nonce-12345678'

12345678 建議置換為亂碼

之後在 css 定義裡加上 nonce 定義
nonce的字串必須跟apahce config裡的相同

<style nonce="123456789" type="text/css">
</style>

修改 html內文 把原本直接定義的style改成先定義css再引用 

例如原本是

<a href="123456.exe" style="color:red;">123456</a>

要改成

<a href="123456.exe" class="red-text">123456</a><br>

同時在上方的css定義加入 red-text

<style nonce="sjdfhs6849" type="text/css">

.red-text {
  color: red;
  font-size: 21px;
}

</style>


另外不使用 hash 跟 nonce 的方法是 把css以獨立檔案定義後再引用

建立 style.css

檔案內容如下

.red-text {
  color: red;
  font-size: 21px;
}

接下來在 html 的head中引用

<head>
<link rel="stylesheet" href="style.css">
</head>

html內文的修正方法如上

2024/05/22

這几天用ZAP在弱掃

當網站很大時 因為會產生很大量的cache

所以要確保執行弱掃那台几器的HD空間要夠

不然有可能還沒做完就爆了

另外有很多網站都出現以下的報告


 


查了一下 可以在httpd.conf 設定

新增以下內容


<IfModule mod_headers.c>

Header set X-Frame-Options "SAMEORIGIN"

Header set Content-Security-Policy "default-src 'self'"

</IfModule>


設完要重啟apache

另外 如果有參考到外面的任何元件

版面是會亂掉的

這個要特別注意


設完後再掃一次出現另一個報告




如果要再處理

Header set 就要再改成如下的設定


Header set Content-Security-Policy "frame-ancestors 'self'; form-action 'self'; base-uri 'self'; img-src 'self'; default-src 'self'; script-src 'self'; style-src 'self';"


當然影響範圍就會更大


https://lawrencechuang760223.medium.com/%E5%A6%82%E4%BD%95%E5%9C%A8apache-server%E4%B8%8B%E6%96%B0%E5%A2%9Ex-frame-options%E8%88%87content-security-policy%E8%87%B3response-header%E4%B8%AD-fix-web-application-d68209ff385c


https://medium.com/@andy001018/owasp-zap-%E5%BC%B1%E6%8E%83%E8%99%95%E7%90%86%E7%B4%80%E9%8C%84-2fb0f4888908

2021/08/28

最近要求要把所有的網站改成 https

以 centos 7 apache 為例

首先取得憑証

因為單位有買整個domain的

所以直接拿來用


把憑証放到以下目錄

/etc/pki/tls/certs


安裝 mod_ssl

$ yum -y install mod_ssl


修改 ssl.conf

$ vi /etc/httpd/conf.d/ssl.conf

修改以下二行

SSLCertificateFile /etc/pki/tls/certs/server.cer 

SSLCertificateKeyFile /etc/pki/tls/certs/server.key


重新啟動 Apache

$ systemctl restart httpd.service


若使用nginx

修改 /etc/nginx/nginx.conf 加入以下藍色設定


server {

  listen 80 default_server;

  listen [::]:80 default_server;


  # 加入 SSL 設定

  listen 443 ssl default_server;

  listen [::]:443 ssl default_server;


  # 憑證與金鑰的路徑

  ssl_certificate /etc/pki/tls/certs/server.cer;

  ssl_certificate_key /etc/pki/tls/certs/server.key;


  # ...

}


systemctl restart nginx


https://www.codepulse.com.tw/zh-tw/ssl%E6%86%91%E8%AD%89%E5%AE%89%E8%A3%9D%E6%95%99%E5%AD%B8%EF%BC%8C%E4%BB%A5centos%E7%82%BA%E4%BE%8B


https://blog.gtwang.org/linux/nginx-create-and-install-ssl-certificate-on-ubuntu-linux/

2018/08/17

今天升級ocs 2.5 版的
升級的時候沒有發生什麼問題
但升完後
只要點search 畫面就是一片空白
查一下log 發現

Can't use method return value in write context in /usr/share/ocsinventory-reports/ocsreports/require/search/TranslationSearch.php on line 224, referer: http://1.2.3.4/ocsreports/index.php?first

爬了一下
果然有人問到


有人回答了

The multi-criteria search uses tables that need the minimum php 5.5 version. I advise you to update your php version to 5.5 or higher.

GG

centos 7 官方的php只出到5.4 而且也不想去升
而且有人升完後還要再安裝php-xml 才能用

把昨天的備分倒回去
等官方升到 php 5.5 再說吧

2018/05/09

今天有個新需求
要把apache 的log丟到graylog
找到很多文件
以下的方式算是最簡單的做法

apache跑在centos 上

把以下的內容加到 /etc/rsyslog.conf 或在/etc/rsyslog.d/裡加上一個新的檔案 如 apachelog.conf

$ModLoad imfile

# apache error.log
$InputFileName /var/log/httpd/error_log
$InputFileTag apache-errors:
$InputFileStateFile state_file_error_apache
$InputFileFacility local6
$InputFileSeverity info
$InputRunFileMonitor
$InputFilePollInterval 10

# apache access.log
$InputFileName /var/log/httpd/access_log
$InputFileTag apache-access:
$InputFileStateFile state_file_access_apache
$InputFileFacility local6
$InputFileSeverity info
$InputRunFileMonitor
$InputFilePollInterval 10

if $programname == 'apache-access' then @1.2.3.4:514
& stop
if $programname == 'apache-errors' then @1.2.3.4:514
& stop

依需求修改 log檔的路徑
外部 log server 的ip及port

改完後rsyslogd要重啟



2016/10/08

為了安全
愈來愈多的網站都改用https的傳輸方式了
Let`s Encrypt提供了一個方便 自動而且免費的方式來達成這個需求
certbot幫助管理者方便的佈署
只要選擇使用的web server及os
就有完整的安裝方式

https://certbot.eff.org/
https://letsencrypt.org/

2016/03/22

為了預先解決centos 5到明天支援到期的問題
今天來把原先提供下載服務的机器升成centos 7
比較難處理的是原本提供kms認証的部分
几個跟原centos 5不同的地方記錄一下

原本apache用來ldap認証的模組換成 mod_ldap

寫法也有一些改變 範例如下 (novell 適用)

<Directory /var/www/html/test>
    AuthName ldap
    AuthType Basic
    AuthBasicProvider ldap
    AuthLDAPURL ldap://test.com.tw:389/o=users?cn?sub?(objectClass=*)
    AuthLDAPBindDN "cn=user01, ou=user, ou=people, o=users"
    AuthLDAPBindPassword password
    Require valid-user
</Directory>

ip forword的寫法也改了

在/etc/sysctl.conf 加上
net.ipv4.ip_forward = 1

重新載入
sysctl -p /etc/sysctl.conf


因為需要使用apche這個身分下iptables的指令
所以要編輯 /etc/sudoers
Defaults    requiretty 改為 Defaults:apache !requiretty apache不需要tty

再加上
User_Alias      APACHE = apache
Cmnd_Alias      FIREWALL = /sbin/iptables

APACHE  ALL = (ALL) NOPASSWD: FIREWALL

原來的firewalld要停掉 改用iptables

systemctl disable firewalld
yum install -y iptables-services
systemctl enable iptables

http://my.oschina.net/fsxchen/blog/134601
http://superuser.com/questions/803741/how-to-enable-ip-masquerading-forwarding-on-centos-7

2016/03/05

最近把從mysql撈資料的程式用python改寫 之前是用php

改完後發生的第一個問題就是中文全變成了???

192.168.105.61 || 2016-03-04 00:05:05 || 2016-03-07 00:05:05 || Flow Checking ????????????(1368>????:700) to Deny this IP cannot access network

原因是從mysql撈出來時中文就亂了 所以不管之後怎麼轉碼 都沒有用了
所以必須在撈時就要指定編碼 加上下方紅色字部分

db = MySQLdb.connect(host="localhost", user="abc", passwd="pwd", db="test", charset='utf8')

但加完後網頁反而出不來了
再查了一下資料 說是要再指定sys的編碼 於是程式內必需再加入以下三行

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

加完後之後的結果變成

192.168.105.61 || 2016-03-04 00:05:05 || 2016-03-07 00:05:05 || Flow Checking �訫�銝餅�����賊�蝬脰楝�輻�(1368>�𣂼��賊�:700) to Deny this IP cannot access network

看來中文有出來了 接下來是顯示的問題了 因為這個os比較久了 所以apache預設的編碼是設為big5 如果要去改會影響到其他東東 所以只能在程式加上指定編碼來處理了 比較新版本os的apache應該都預設為utf8了 應該不會碰到這個問題

print '<html>'
print '<head>'
print '<meta charset="UTF-8">'
print '</head>'
print '<body>'

print '</body>'
print '</html>'

網頁正常 搞定

192.168.105.61 || 2016-03-04 00:05:05 || 2016-03-07 00:05:05 || Flow Checking 違反主機連線數量網路政策(1368>限制數量:700) to Deny this IP cannot access network

完整程式碼如下

#!/usr/bin/python
# -*- coding: utf-8 -*-
print "Content-type: text/html"
print
# 引入 MySQL 模組
import MySQLdb
#引入 sys 並指定sys為utf-8編碼
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# 連接 MySQL
db = MySQLdb.connect(host="localhost", user="abc", passwd="pwd", db="test", charset='utf8')
cursor = db.cursor()
# 執行 SQL
cursor.execute("select ip from table;")
result = cursor.fetchall()
# 輸出結果
a=0
for record in result:
        if (a%2)==1:
                print "<FONT  COLOR=FF0000>"
        else:
                print "<FONT  COLOR=000000>"

        print record[0]
        print "<br>"
        print "<br>"
        a=a+1
db.close()

2016/02/29

關於如何在網頁上執行python目前找到二個方法
一個是在apache中載入module
有二個module可以用

http://modpython.org/
https://code.google.com/archive/p/modwsgi/

根據一般的說法是wsgi的效能會比modpython好二倍以上
使用方式是在apache啟動時載入模組

另一個方式是採用cgi
直接把寫好的python放在 /var/www/cgi-bin/下

在程式碼的最前面加上
print "Content-type: text/html"
print

這樣就能直接用了

bash perl 也是相同的方法

目前先採用cgi的方式

http://blog.xuite.net/autosun/study/42871538-%5BUbuntu%5D+%E5%AE%89%E8%A3%9D+Apache2+%2B+Python+%2B+MySQL

2014/04/18

今天是ubuntu 14.04 release的日子
一開始upgrade時總是有三個檔案有問題
後來把source改到米國就ok了
但升完smokeping卻發生圖出不來的問題


查了一下rrd都有正常update
原來是apache2的升級把smokeping的site砍了
補回去
ln -s /etc/apache2/sites-enabled/smokeping.conf /etc/smokeping/apache2.conf
service apache2 restart
恢復正常

http://askubuntu.com/questions/365088/smokeping-web-front-end-on-ubuntu-13-10

2013/03/14

webalizer

用來分析apache ftp squid的log
centos及ubuntu皆有收進套件
執行前要
LANG=c
否則產生的網頁會有亂碼
語法如下

webalizer -v -F squid -o /tmp/y/ access.log.1

http://www.webalizer.org/

2012/03/03


今天在找要如何用python登入apache的認証網頁

找到httplib2

http://code.google.com/p/httplib2/

範例如下

import base64
import httplib2
h = httplib2.Http()
auth = base64.encodestring( 'id' + ':' + 'password' )
resp, content = h.request(
        'http://10.0.0.56/all/',
        'GET',
        headers = { 'Authorization' : 'Basic ' + auth }
    )

print resp        #傳回html的標頭

print content   #傳回html的內容