0×01 撸起袖子开干
人生苦短,我用Python!
Python强大的库、简洁语言以及开发迅速等特点,深受广大程序开发者喜爱。那么我们就用Python来开发吧!
0×02 步骤解析
1.登陆Github
登陆这里设置了一个坑,登陆 https://github.com/login 会跳转到https://github.com/session ,然后提交请求主体。而主体包含了如下参数:
“commit=Sign+in&utf8=%E2%9C%93&authenticity_token=sClUkea9k0GJ%2BTVRKRYsvLKPGPfLDknMWVSd%2FyWvyGAR9Zz09bipesvXUo8ND2870Q2FEVsQWFKScyqtV0w1PA%3D%3D&login=YourUsername&password=YourPassword”
commit、uft8、login和password值相对来说是固定的,我们要做到工具登陆,那么需要获取到authenticity_token这个值,然后一起通过POST方法提交。那应该如何获取该值呢?
我们打开浏览器尝试手动正常登陆,同时按F12打开“开发者工具”,输入用户名和密码可以看到跳转到 https://github.com/session ,而authenticity_token的值就在如下图位置:
虽然是隐藏的,但是我们可以通过Xpath来获取它,然后跟其他参数一起提交登陆Github。看代码:
2.查询关键词及结果呈现
登陆后请求查询的URL,然后获取响应的页面,使用xpath解析节点获取想要的信息。关于xpath的语法请看这里
http://www.runoob.com/xpath/xpath-tutorial.html
我们还要将获取的信息写入表格里面,便于以后查看。详情如下:defhunter(gUser,gPass,keyword,payloads):globalsensitive_listglobaltUrls sensitive_list = [] tUrls = []try:#创建表格csv_file = open('leak.csv','w',encoding='utf-8',newline='') writer = csv.writer(csv_file)#写入表头writer = writerow(['URL','Username','Upload Time','Filename'])#搜索信息s = login_github(gUser,gPass) print('登陆成功,正在检索泄露信息......') sleep(1)forpageintqdm(range(1,6)):#检索1到6页匹配关键词keyword的结果search_code ='https://github.com/search?p='+ str(page) +'&q='+ keyword +'&type=Code'resp = s.get(search_code) results_code = resp.text dom_tree_code = etree.HTML(results_code)#采用lxml提供的etree来解析结果Urls = dom_tree_code.xpath('//div[@class="d-inline-block col-10"]/a[2]/@href')#获取仓库地址users = dom_tree_code.xpath('//a[@class="text-blod"]/text()')#获取用户名datetime = dom_tree_code.xpath('//relative-time/text()')#获取上传时间filename = dom_tree_code.xpath('//div[@class="d-inline-block col-10"]/a[2]/text()')#获取上传的文件名称foriinrange(len(Urls)):forUrlinUrls: Url ='https://github.com'+ Url#获取的URl被截断,所以需要加入前缀便于访问tUrls.append(Url) writer.writerow([tUrls[i],users[i],datetime[i],filename[i]])#写入表格文件'''
以下部分主要是获取泄露的raw代码,然后在代码中搜索用户自定义的payload,例如 password,username,IP等等,然后把存在敏感关键词的URL存放在sensitvie_list列表中,用于后续的邮件发送预警。
'''forraw_urlinUrls: url ='https://raw.githubusercontent.com'+ raw_url.replace('/blob','') code = requests.get(url).textforpayloadinpayloads:ifpayloadincode: leak_url ='命中的Payload为:'+ payload +'\r\n'+'https://github.com'+ raw_url +'\r\n\r\n\r\n'+'代码如下: \r\n'+ code +'\r\n\r\n'sensitive_list.append(leak_url) csv_file.close()returnsensitive_listexceptExceptionase: print(e)
以上代码的核心主要是采用xpath解析DOM树,然后根据需要的数据逐一获取然后写入表格中。最后请求raw.githubusercontent.com来获取源代码,根据用户提供的payload进行逐一匹配,如果匹配则记录payload、URL以及代码,然后发送邮件预警。
3.邮件预警
其实邮件发送部分不是工具的重点,但是还是有必要贴上代码部分。请看:
def send_warning(host,username,password,sender,receivers,content) def _format_addr(s): name,addr = parseaddr(s)returnformataddr((Header(name,'utf-8').encode(),addr) msg = MIMEMultipart() msg['From'] = _format_addr('Github安全监控<%s>'% sender) msg['To'] =''.join(receivers) Subject ='Github敏感信息泄露通知'msg['Subject'] = Header(Subject,'utf-8').encode() msg.attach(MIMEText('Dear all \r\n\r\n请注意,怀疑Github上已经上传敏感信息!以下是可能存在敏感信息的仓库!\r\n\r\n'+content+'\r\n\r\n')) withopen('leak.csv','rb')asf:m= MIMEBase('excel','csv',filename='leak.csv')m.add_header('Content-Disposition','attachment',filename ='leak.csv'm.add_header('Content-ID','<0>')m.add_header('X-Attachment-ID','0')m.set_payload(f.read()) encoders.encode_base64(m) msg.attach(m)try: server = smtplib.SMTP(host,25) server.login(username,password) server.sendmail(sender,receivers,msg.as_string())print('邮件发送成功!') except Exceptionaserr:print(err) server.quit()
4.配置文件读取
我们将创建一个.ini的文件,便于工具读取我们想要传入工具的关键词、用户名、密码以及payload等等。ini配置文件定义如下:
[KEYWORD]keyword= your main keyword here[EMAIL]host= Email serveruser= Email Userpassword= Email password[SENDER]sender= The email sender[RECEIVER]receiver1= Email receiverNo.1receiver2= Email receiverNo.2[Github]user= Github Usernamepassword= Github Password[PAYLOADS]p1= Payload1p2= Payload2p3= Payload3p4= Payload4p5= Payload5p6= Payload6
然后我们在main函数中读取它们,然后传入工具中。
if__name__== '__main__': config = configparser.ConfigParser() config.read('info.ini') g_User = config['Github']['user'] g_Pass = config['Github']['password'] host = config['EMAIL']['host'] m_User = config['EMAIL']['user'] m_Pass = config['EMAIL']['password'] m_sender = config['SENDER']['sender'] receivers = [] for k in config['RECEIVER']: receivers.append(config['RECEIVER'][k]) keyword = config['KEYWORD']['keyword'] payloads = [] for key in config['PAYLOADS']: payloads.append(config['PAYLOADS'][key]) sensitive_list = hunter(g_User, g_Pass, keyword, payloads) if sensitive_list: print('\033[1;31;0m警告:找到敏感信息!\r\n\033[0m') print('开始发送告警邮件......') content = ''.join(sensitive_list) send_warning(host, m_User, m_Pass, m_sender, receivers, content) else: print('恭喜:未找到敏感信息!\r\n') print('所有检查已完成,已生成报表!\r\n') print('开始发送报表......\r\n') send_mail(host, m_User, m_Pass, m_sender, receivers)
以上代码中存在另外一个send_mail函数,同样是发送邮件的功能跟send_warning功能一样,只是发送的内容不一样。这里不再赘述。这样我们就完成了整个工具的核心部分。怎么样?对于老司机来说很简单吧!
0×03 监控效果
1.运行效果
2.邮件预警
欢迎大家加入我的千人交流学习答疑群:125240963
网友评论