近期收到了电子工业出版社赠送的一本网络安全书籍《python黑帽子》,书中一共24个实验,今天复现第11个实验(人脸识别),我的测试环境是mbp电脑+conda开发环境。这个实验是紧接着上一个流量图片还原的,而流量图片还原可以接在中间人攻击上,这样就可以嗅探到目标正在浏览什么内容,比如说色情网站,从而为社会工程学欺诈勒索创造突破口~
ailx10
网络安全优秀回答者
网络安全硕士
去咨询
1、先去网上随便搜索若干张人脸图片,本来打算全部放美女图,这里致敬一下行业大佬
@腹黑
、
@kn1f3
2、下载必备的xml配置文件
wget https://eclecti.cc/files/2008/03/haarcascade_frontalface_alt.xml
3、在mbp上运行脚本
4、在faces文件夹中查看人脸结果
参考代码:
# -*- coding: utf-8 -*-
# @Time : 2022/6/13 7:36 PM
# @Author : ailx10
# @File : detector.py
import cv2
import os
ROOT = "/Users/ailx10/py3hack/chapter4/picture"
FACES = "/Users/ailx10/py3hack/chapter4/faces"
TRAIN = "/Users/ailx10/py3hack/chapter4/training"
def detect(srcdir=ROOT,tgtdir=FACES,train_dir=TRAIN):
for fname in os.listdir(srcdir):
if not fname.upper().endswith(".JPG"):
continue
fullname = os.path.join(srcdir,fname)
newname = os.path.join(tgtdir,fname)
img = cv2.imread(fullname)
if img is None:
continue
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
training = os.path.join(train_dir,"haarcascade_frontalface_alt.xml")
cascade = cv2.CascadeClassifier(training)
rects = cascade.detectMultiScale(gray,1.3,5)
try:
if rects.any():
print("Got a face")
rects[:,2:] += rects[:,:2]
except AttributeError:
print(f"No face found in {fname}")
continue
for x1,y1,x2,y2 in rects:
cv2.rectangle(img,(x1,y1),(x2,y2),(127,255,0),2)
cv2.imwrite(newname,img)
if __name__ == "__main__":
detect()
发布于 2022-06-13 19:52