|
| 1 | +from dotenv import load_dotenv |
| 2 | +from langchain_groq import ChatGroq |
| 3 | +from langchain_core.prompts import ChatPromptTemplate |
| 4 | +import os |
| 5 | + |
| 6 | +class DorkGenerator: |
| 7 | + """ |
| 8 | + Class representing an AI agent capable of generating Google Dorks based on user-provided descriptions. |
| 9 | + |
| 10 | + Attributes: |
| 11 | + model (ChatGroq): The Groq model used for generating Google Dorks. |
| 12 | + prompt_template (ChatPromptTemplate): The template for the prompt to generate Google Dorks. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, model_id="llama3-70b-8192", groq_api_key=None): |
| 16 | + """ |
| 17 | + Initializes a new DorkGenerator with the specified Groq model and API key. |
| 18 | + |
| 19 | + Args: |
| 20 | + model_id (str): The ID of the Groq model to use. Default is 'llama3-70b-8192'. |
| 21 | + groq_api_key (str): The API key for accessing Groq services. |
| 22 | + """ |
| 23 | + if groq_api_key is None: |
| 24 | + groq_api_key = os.getenv("GROQ_API_KEY") |
| 25 | + |
| 26 | + if not groq_api_key: |
| 27 | + raise ValueError("Groq API key must be provided either as an argument or an environment variable.") |
| 28 | + |
| 29 | + self.model = ChatGroq(model=model_id, temperature=0, api_key=groq_api_key) |
| 30 | + self.prompt_template = ChatPromptTemplate.from_messages( |
| 31 | + [ |
| 32 | + ( |
| 33 | + "system", |
| 34 | + """ |
| 35 | + Generate a specific Google Dork based on the user's description. A Google Dork uses advanced search operators to find specific information that is hard to locate through a normal search. |
| 36 | + Your task is to convert the user's description into an accurate Google Dork. Provide only the Google Dork in your response, without any additional text or prefixes. |
| 37 | +
|
| 38 | + Here are some examples of how you should formulate the Google Dorks based on different descriptions: |
| 39 | +
|
| 40 | + Description: PDF documents related to cybersecurity published in the last year. |
| 41 | + Google Dork: filetype:pdf "cybersecurity" after:2023-01-01 |
| 42 | +
|
| 43 | + Description: PowerPoint presentations on climate change available on .edu sites. |
| 44 | + Google Dork: site:.edu filetype:ppt "climate change" |
| 45 | +
|
| 46 | + Description: Lists of email addresses in text files within government domains. |
| 47 | + Google Dork: site:.gov filetype:txt "email" | "correo electrónico" |
| 48 | +
|
| 49 | + Now, based on the following user-provided description, generate the corresponding Google Dork: |
| 50 | + """ |
| 51 | + ), |
| 52 | + ("human", "{description}"), |
| 53 | + ] |
| 54 | + ) |
| 55 | + |
| 56 | + def generate_dork(self, description): |
| 57 | + """ |
| 58 | + Generates a Google Dork based on the provided description. |
| 59 | + |
| 60 | + Args: |
| 61 | + description (str): Description provided by the user to generate the Google Dork. |
| 62 | + |
| 63 | + Returns: |
| 64 | + str: Generated Google Dork or None if an error occurs. |
| 65 | + """ |
| 66 | + try: |
| 67 | + # Create a chain using the prompt template and the model |
| 68 | + chain = self.prompt_template | self.model |
| 69 | + # Invoke the chain with the provided description |
| 70 | + response = chain.invoke({"description": description}) |
| 71 | + # Extract content from the response |
| 72 | + # Check the type of response and use appropriate method to access the content |
| 73 | + if hasattr(response, 'text'): |
| 74 | + content = response.text.strip() |
| 75 | + elif hasattr(response, 'content'): |
| 76 | + content = response.content.strip() |
| 77 | + else: |
| 78 | + raise TypeError("Unexpected response type: Unable to extract content.") |
| 79 | + return content |
| 80 | + except Exception as e: |
| 81 | + print(f"Error generating Google Dork: {e}") |
| 82 | + return None |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + load_dotenv() |
| 86 | + # Ensure the Groq API key is set in your environment or replace with your actual key |
| 87 | + api_key = os.getenv("GROQ_API_KEY") |
| 88 | + dork_generator = DorkGenerator(groq_api_key=api_key) |
| 89 | + print(dork_generator.generate_dork("List of users and passwords in text file contents.")) |
0 commit comments