Skip to content

Instantly share code, notes, and snippets.

@mbrownnycnyc
Last active September 26, 2022 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrownnycnyc/d202ca8041f87233442b35c41f1963fd to your computer and use it in GitHub Desktop.
Save mbrownnycnyc/d202ca8041f87233442b35c41f1963fd to your computer and use it in GitHub Desktop.
gitlab code searcher based on https://github.com/tuimm/gitlab-search, but added enum for all branches and regex string matches. need to add multithreading and dumping data to disk upon receipt.
import gitlab
import re
def search(gitlab_server, token, file_filter, regextofind, group=None, project_filter=None):
return_value = []
gl = gitlab.Gitlab(gitlab_server, private_token=token)
if (project_filter == '') and (group == ''):
projects = gl.projects.list(all=True)
else:
group_object = gl.groups.get(group)
group_projects = group_object.projects.list(search=project_filter)
projects = []
for group_project in group_projects:
projects.append(gl.projects.get(group_project.id))
for project in projects:
print("enuming project: ", project.name)
files = []
try:
files = project.repository_tree(recursive=True, all=True)
except Exception as e:
print(str(e), "Error getting tree in project:", project.name)
for file in files:
print("enuming file: ", file.get("name"))
matches=re.findall(file_filter, file['name'])
filename_matches = len(matches)>0
if filename_matches:
for branch in project.branches.list(get_all=True):
try:
file_content = project.files.raw(file_path=file['path'], ref=branch.name)
filecontentmatches=re.findall(regextofind, str(file_content))
filecontent_matches = len(filecontentmatches)>0
if filecontent_matches:
return_value.append({
"project": project.name,
"branch": branch.name,
"file": file['path']
})
except Exception as e:
#if a file doesn't exist in the targeted branch, then an error exists...
# print(str(e), "Error getting file contents for", file['path'], " in project:", project.name, " in branch: ", branch.name)
continue
return return_value
#testing with project specified, to check multiple files found, and check regex
search('https://gitlabinstance', 'access_token', '.*', '(?i)NaMe','infra','vpc')
#testing without project specified
search('https://gitlabinstance', 'access_token', '.*', '(?i)NaMe','infra','')
#searching for joinpath for CVE-2022-32190
thelist = search('gitlabinstance', 'access_token', '.*', '(?i)joinpath','','')
thejsonlist = json.dumps(thelist)
thejsonfile = open("thelist.json","w")
thejsonfile.write(thejsonlist)
thejsonfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment