{ "cells": [ { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "import os\n", "# os.environ['LEPTON_API_KEY']= 'e6ua0rtm4drrpl7tz16farcczod387dz'\n", "# os.environ['LEPTON_API_KEY']= 'twoun3dz0fzw289dgyp2rlb3kltti8zi'\n", "\n", "from openai import OpenAI\n", "\n", "\n", "def generate_response(model, user_query):\n", " client = OpenAI(\n", " api_key=os.environ.get(\"LEPTON_API_KEY\", \"twoun3dz0fzw289dgyp2rlb3kltti8zi\"),\n", " base_url=f'https://{model}.lepton.run/api/v1',\n", " )\n", " response = client.chat.completions.create(\n", " model= f\"{model}\",\n", " messages=[\n", " {\n", " \"role\": \"user\", \n", " \"content\": user_query\n", " },\n", " ],\n", " max_tokens=4096,\n", " stream=True,\n", " )\n", "\n", " reply = ''\n", " for chunk in response:\n", " if chunk.choices:\n", " content = chunk.choices[0].delta.content\n", " if content:\n", " reply += content\n", "\n", " return reply" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Vietnam is a country with a population of over 98 million people, and it has several large cities that are home to a significant portion of the population. Here are some of the biggest cities in Vietnam, in no particular order:\n", "\n", "1. Ho Chi Minh City (formerly Saigon): Located in the south of Vietnam, Ho Chi Minh City is the largest city in the country with a population of over 10 million people. It is the economic, cultural, and political center of Vietnam and is known for its vibrant nightlife, historic landmarks, and modern infrastructure.\n", "2. Hanoi: Located in the north of Vietnam, Hanoi is the second-largest city in the country with a population of over 7 million people. It is the capital of Vietnam and is known for its rich cultural heritage, including its ancient architecture, traditional crafts, and vibrant traditional music.\n", "3. Da Nang: Located in the central region of Vietnam, Da Nang is the third-largest city in the country with a population of over 1 million people. It is known for its beautiful beaches, golf courses, and tourist attractions, and is a popular destination for both domestic and international tourists.\n", "4. Haiphong: Located in the north of Vietnam, Haiphong is the fourth-largest city in the country with a population of over 1 million people. It is known for its port and industrial activities, and is an important economic hub in the region.\n", "5. Can Tho: Located in the southwestern region of Vietnam, Can Tho is the fifth-largest city in the country with a population of over 1 million people. It is known for its floating markets, traditional crafts, and vibrant cultural scene.\n", "6. Nha Trang: Located in the central region of Vietnam, Nha Trang is a popular beach destination with a population of over 300,000 people. It is known for its beautiful beaches, scuba diving, and vibrant nightlife.\n", "7. Dalat: Located in the central highlands of Vietnam, Dalat is a popular tourist destination with a population of over 200,000 people. It is known for its cool climate, beautiful scenery, and vibrant cultural scene.\n", "8. Vinh: Located in the northwestern region of Vietnam, Vinh is the eighth-largest city in the country with a population of over 200,000 people. It is known for its historic landmarks, traditional crafts, and vibrant cultural scene.\n", "9. Hue: Located in the central region of Vietnam, Hue is a historic city with a population of over 300,000 people. It is known for its imperial architecture, traditional crafts, and vibrant cultural scene.\n", "10. Cao Bang: Located in the northeastern region of Vietnam, Cao Bang is a small city with a population of over 200,000 people. It is known for its historic landmarks, traditional crafts, and vibrant cultural scene.\n", "\n", "These are just a few of the biggest cities in Vietnam, and there are many other smaller cities and towns throughout the country that are worth visiting as well.\n" ] } ], "source": [ "user_query = \"what are the biggest cities in Vietnam\"\n", "model = \"llama2-7b\"\n", "response = generate_response(model, user_query)\n", "print(response)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " As an expert in identifying code vulnerabilities using the CWE and CVE standards, I will carefully analyze the provided snippet code and identify any potential vulnerabilities.\n", "\n", "The code provided is a simple function that takes a string input from the user and stores it in a `char` array named `last_name`. Here's my analysis of the code:\n", "\n", "1. Injection vulnerability (CWE-89):\n", "The `scanf()` function is used to read input from the user. However, it does not provide any protection against SQL injection or other types of injection attacks. An attacker could potentially exploit this vulnerability by injecting malicious input, such as a SQL injection attack, which could lead to unauthorized access or data manipulation.\n", "\n", "To fix this vulnerability, consider using a safer input validation function, such as `fgets()` or `getline()`, which allows for more flexible and secure input handling. For example:\n", "```c\n", "char last_name[20];\n", "printf (\"Enter your last name: \");\n", "fgets (last_name, 20, stdin);\n", "```\n", "1. Buffer overflow vulnerability (CWE-122):\n", "The `last_name` array has a fixed length of 20 characters. If the user enters a longer input, it will overflow the buffer, potentially leading to unpredictable behavior, including code execution.\n", "\n", "To fix this vulnerability, consider increasing the size of the `last_name` array or using a safer buffer allocation function, such as `malloc()` or `calloc()`. For example:\n", "```c\n", "char *last_name = malloc (21 * sizeof (char));\n", "```\n", "1. Lack of input validation (CWE-278):\n", "The `scanf()` function does not validate the input provided by the user. An attacker could potentially provide input that is not a valid name, such as a string of arbitrary length or a malicious payload.\n", "\n", "To fix this vulnerability, consider adding input validation checks to ensure that the input provided is a valid name. For example:\n", "```c\n", "size_t name_len = strlen (last_name);\n", "if (name_len > 20 || last_name[0] != ' ') {\n", " // Handle invalid input\n", "}\n", "```\n", "After fixing these vulnerabilities, the modified code would look like this:\n", "```c\n", "#include \n", "\n", "void get_last_name (void) {\n", " char *last_name = malloc (21 * sizeof (char));\n", " printf (\"Enter your last name: \");\n", " fgets (last_name, 21, stdin);\n", " size_t name_len = strlen (last_name);\n", " if (name_len > 20 || last_name[0] != ' ') {\n", " // Handle invalid input\n", " }\n", "}\n", "```\n", "By fixing these vulnerabilities, we can prevent potential attacks and ensure that the code is more secure and reliable.\n" ] } ], "source": [ "snippet_code = \"\"\" \n", "int check() {\n", " int len = -1;\n", " int i;\n", " char string[64];\n", " for (i = 0; i < sizeof(string); i++) {\n", " string[i] = '0';\n", " }\n", " if (receive_delim(0, string, 128, '\\0')) {\n", " return -1;\n", " }\n", " for (i = 0; string[i] != '\\0'; i++) {\n", " len++;\n", " }\n", " return len;\n", "}\n", "\"\"\"\n", "\n", "# snippet_code = \"\"\" \n", "# char last_name[20];\n", "# printf (\"Enter your last name: \");\n", "# scanf (\"%s\", last_name);\n", "# \"\"\"\n", "\n", "user_query = f\"\"\"\n", "You are expert in indentification of any code vulnerabilities under CWE and CVE standard for C, C++ programming language.\n", "Your task is to identify any vulnerability in the snippet code that cyber attacker can manipulate the code and cause harmful to the system.\n", "In addition, fix the code with any vulnerables found. \n", "The snippet code is delimited in the triple angle brakets <<<{snippet_code}>>>\n", "\"\"\"\n", "model = \"llama2-7b\"\n", "response = generate_response(model, user_query)\n", "print(response)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 2 }