2024-08-26 11:16:40 -05:00
import os
2024-08-26 13:27:14 -05:00
import argparse
2024-08-26 15:52:50 -05:00
import logging
2024-08-27 13:56:30 -05:00
import json
2024-08-26 15:52:50 -05:00
from github import Github , Auth , GithubException
# Configure logging
logging . basicConfig ( level = logging . INFO , format = ' %(asctime)s - %(levelname)s - %(message)s ' )
2024-08-26 11:16:40 -05:00
def auth ( ) :
access_token = os . getenv ( " GITHUB_ACCESS_TOKEN " )
2024-08-26 15:46:39 -05:00
if not access_token :
raise ValueError ( " GITHUB_ACCESS_TOKEN environment variable not set " )
2024-08-26 11:16:40 -05:00
auth = Auth . Token ( access_token )
g = Github ( auth = auth )
return g
class GithubSearcher ( ) :
def __init__ ( self , query ) :
self . g = auth ( )
self . query = query
self . result = None
def search_repo ( self ) :
2024-08-26 15:52:50 -05:00
try :
self . result = self . g . search_repositories ( self . query )
except GithubException as e :
logging . error ( f " Error searching repositories: { e } " )
self . result = None
2024-08-26 13:27:14 -05:00
2024-08-26 11:16:40 -05:00
def search_users ( self ) :
2024-08-26 15:52:50 -05:00
try :
self . result = self . g . search_users ( self . query )
except GithubException as e :
logging . error ( f " Error searching users: { e } " )
self . result = None
2024-08-26 11:16:40 -05:00
2024-08-27 13:56:30 -05:00
def search_in_repo_name ( self ) :
2024-08-26 15:52:50 -05:00
try :
self . result = self . g . search_repositories ( ' in:name ' + self . query )
except GithubException as e :
logging . error ( f " Error searching in name: { e } " )
self . result = None
2024-08-26 13:27:14 -05:00
2024-08-26 15:46:39 -05:00
def get_repo_open_issues ( self , repo ) :
2024-08-26 15:52:50 -05:00
try :
2024-08-27 10:35:22 -05:00
return repo . get_issues ( state = ' open ' )
2024-08-26 15:52:50 -05:00
except GithubException as e :
logging . error ( f " Error getting open issues: { e } " )
return None
2024-08-26 15:46:39 -05:00
2024-08-27 10:35:22 -05:00
def get_repo_open_pull_requests ( self , repo ) :
try :
return repo . get_pulls ( state = ' open ' )
except GithubException as e :
logging . error ( f " Error getting open pull requests: { e } " )
return None
2024-08-26 15:46:39 -05:00
def get_repo_contents ( self , repo ) :
2024-08-26 15:52:50 -05:00
try :
2024-08-27 13:56:30 -05:00
return repo . get_contents ( " " )
2024-08-26 15:52:50 -05:00
except GithubException as e :
logging . error ( f " Error getting repository contents: { e } " )
return None
2024-08-26 15:46:39 -05:00
2024-08-26 11:16:40 -05:00
def get_result ( self ) :
return self . result
def main ( ) :
2024-08-26 15:52:50 -05:00
parser = argparse . ArgumentParser ( description = " Search GitHub repositories and users for PoC exploits and CVEs. " )
parser . add_argument ( " --query " , type = str , required = True , help = " The search query. " )
2024-08-27 13:56:30 -05:00
parser . add_argument ( " --search_type " , type = str , required = True , choices = [ " repo " , " users " , " in-repo-name " ] , help = " The type of search to perform: ' repo ' , ' users ' , or ' in-repo-name ' . " )
2024-08-27 10:35:22 -05:00
parser . add_argument ( " --get_file_contents " , action = " store_true " , help = " Get the contents of repo results. " )
parser . add_argument ( " --get_open_issues " , action = " store_true " , help = " Get the open issues of repo results. " )
parser . add_argument ( " --get_open_pull_requests " , action = " store_true " , help = " Get the open pull requests of repo results. " )
2024-08-27 13:56:30 -05:00
parser . add_argument ( " --json " , action = " store_true " , help = " Output results in JSON format. " )
2024-08-27 10:35:22 -05:00
2024-08-26 13:27:14 -05:00
args = parser . parse_args ( )
searcher = GithubSearcher ( args . query )
if args . search_type == " repo " :
searcher . search_repo ( )
elif args . search_type == " users " :
searcher . search_users ( )
2024-08-27 13:56:30 -05:00
elif args . search_type == " in-repo-name " :
searcher . search_in_repo_name ( )
2024-08-26 13:27:14 -05:00
2024-08-26 11:16:40 -05:00
result = searcher . get_result ( )
2024-08-26 15:46:39 -05:00
if result is None :
print ( " No results found. " )
return
2024-08-27 13:56:30 -05:00
output = [ ]
2024-08-26 13:27:14 -05:00
for item in result :
if args . search_type == " users " :
2024-08-26 13:56:26 -05:00
user_repos = item . get_repos ( )
2024-08-27 13:56:30 -05:00
2024-08-26 13:56:26 -05:00
for repo in user_repos :
2024-08-27 13:56:30 -05:00
repo_info = { " repo_url " : repo . html_url , " repo_name " : repo . name , " repo_description " : repo . description }
output . append ( repo_info )
2024-08-26 13:27:14 -05:00
else :
2024-08-27 13:56:30 -05:00
repo_info = { " repo_url " : item . html_url , " repo_name " : item . name , " repo_description " : item . description }
2024-08-26 11:16:40 -05:00
2024-08-26 15:46:39 -05:00
if args . get_open_issues :
2024-08-27 13:56:30 -05:00
open_issues = searcher . get_repo_open_issues ( item )
if open_issues :
repo_info [ " open_issues " ] = [ { " title " : issue . title , " url " : issue . html_url } for issue in open_issues ]
2024-08-27 10:35:22 -05:00
if args . get_open_pull_requests :
2024-08-27 13:56:30 -05:00
open_prs = searcher . get_repo_open_pull_requests ( item )
if open_prs :
repo_info [ " open_pull_requests " ] = [ { " title " : pr . title , " url " : pr . html_url } for pr in open_prs ]
2024-08-26 15:46:39 -05:00
if args . get_file_contents :
2024-08-27 13:56:30 -05:00
contents = searcher . get_repo_contents ( item )
if contents :
repo_info [ " contents " ] = [ content_file . name for content_file in contents ]
output . append ( repo_info )
if args . json :
print ( json . dumps ( output , indent = 4 ) )
else :
for item in output :
print ( item )
2024-08-26 15:46:39 -05:00
2024-08-26 11:16:40 -05:00
if __name__ == " __main__ " :
main ( )