|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | +#include<string.h> |
| 4 | + |
| 5 | +// Add a global variable to skip the first line of shell output in windows. |
| 6 | +int first_skip = 1; |
| 7 | + |
| 8 | +// Function to format shell output. |
| 9 | +char* formatLog(char *buffer) |
| 10 | +{ |
| 11 | + // Check which OS the script is currently running on. |
| 12 | + #ifdef __linux__ |
| 13 | + |
| 14 | + // Tokenize the script using `:` as a delimiter. |
| 15 | + char *token_1 = strtok(buffer, ":"); |
| 16 | + char *token_2 = strtok(NULL, ":"); |
| 17 | + |
| 18 | + // Extract the password tokenizing through `=`. |
| 19 | + char *ssid_part = strrchr(token_1, '/'); |
| 20 | + char *psk = strrchr(token_2, '='); |
| 21 | + |
| 22 | + // Extract the ssid. |
| 23 | + char *ssid = strtok(ssid_part+1, "."); |
| 24 | + |
| 25 | + // Some formatting to make it more readable. |
| 26 | + psk += 1; |
| 27 | + strcat(ssid, ":"); |
| 28 | + strcat(ssid, psk); |
| 29 | + |
| 30 | + return ssid; |
| 31 | + |
| 32 | + #elif _WIN32 |
| 33 | + |
| 34 | + #include<ctype.h> |
| 35 | + |
| 36 | + // Skip the first line of shell output. |
| 37 | + if(first_skip == 1) |
| 38 | + { first_skip = 0; |
| 39 | + return ""; |
| 40 | + } |
| 41 | + |
| 42 | + // Extract ssid by tokenizing through `:`. |
| 43 | + char *ssid = strtok(buffer, ":"); |
| 44 | + ssid = strtok(NULL, ": "); |
| 45 | + |
| 46 | + // Trim trailing whitespaces. |
| 47 | + char *end; |
| 48 | + end = ssid + strlen(ssid) - 1; |
| 49 | + while(end > ssid && isspace((unsigned char)*end)) end--; |
| 50 | + end[1] = '\0'; |
| 51 | + |
| 52 | + // Create a file pointer to access a different shell. |
| 53 | + FILE *shell_ptr; |
| 54 | + |
| 55 | + // Format the shell command to extract the password. |
| 56 | + char psk_command[1024]; |
| 57 | + sprintf(psk_command, "netsh wlan show profile %s key=clear | findstr \"Key Content\"", ssid); |
| 58 | + shell_ptr = popen(psk_command, "r"); |
| 59 | + |
| 60 | + // Create a buffer to read the data. |
| 61 | + char psk_temp[1024]; |
| 62 | + fgets(psk_temp, sizeof(psk_temp), shell_ptr); |
| 63 | + |
| 64 | + // Extract the password. |
| 65 | + char *psk = strtok(psk_temp, ":"); |
| 66 | + psk = strtok(NULL, ": "); |
| 67 | + |
| 68 | + // Ignore passwords which are null. |
| 69 | + if((psk != NULL) && (psk[0] == '\0') && (strcmp(psk, "1") != 0)) |
| 70 | + { |
| 71 | + end = psk + strlen(psk) - 1; |
| 72 | + while(end > psk && isspace((unsigned char)*end)) end--; |
| 73 | + end[1] = '\0'; |
| 74 | + } |
| 75 | + |
| 76 | + // Set open networks as [Open Network] |
| 77 | + if(strlen(psk) == 2) |
| 78 | + psk = "[Open Network]"; |
| 79 | + |
| 80 | + // Close unused shell pointer. |
| 81 | + pclose(shell_ptr); |
| 82 | + |
| 83 | + // Format output for better readability. |
| 84 | + strcat(ssid, ":"); |
| 85 | + strcat(ssid, psk); |
| 86 | + return ssid; |
| 87 | + |
| 88 | + #endif |
| 89 | +} |
| 90 | + |
| 91 | +int main() |
| 92 | +{ |
| 93 | + /* |
| 94 | + Create file pointers for: |
| 95 | + - Reading information from shell. |
| 96 | + - Writing to a Text File. |
| 97 | +
|
| 98 | + Also create a buffer to read the shell output, line by line. |
| 99 | + */ |
| 100 | + FILE *shell_op, *fptr; |
| 101 | + char buffer[3096]; |
| 102 | + |
| 103 | + // Check which OS the program is currently being run on. |
| 104 | + #ifdef _WIN32 |
| 105 | + |
| 106 | + char *command = "netsh wlan show profile | findstr \":\""; |
| 107 | + |
| 108 | + #elif __linux__ |
| 109 | + |
| 110 | + char *command = "sudo grep -r '^psk=' /etc/NetworkManager/system-connections/"; |
| 111 | + |
| 112 | + #endif |
| 113 | + |
| 114 | + // Open a text file for writing. |
| 115 | + fptr = fopen("wifi_password.txt", "w"); |
| 116 | + |
| 117 | + // Check if file was created or not. |
| 118 | + if(fptr == NULL) |
| 119 | + { |
| 120 | + puts("Error creating file..."); |
| 121 | + exit(1); |
| 122 | + } |
| 123 | + |
| 124 | + // Spawn a shell process to communitcate with OS. |
| 125 | + shell_op = popen(command, "r"); |
| 126 | + if(shell_op == NULL) |
| 127 | + { |
| 128 | + puts("Failed to communicate with shell."); |
| 129 | + exit(1); |
| 130 | + } |
| 131 | + |
| 132 | + // Read shell output line by line. |
| 133 | + while (fgets(buffer, sizeof(buffer), shell_op) != NULL) |
| 134 | + { |
| 135 | + // Format shell output for better readability. |
| 136 | + char *formattedLog = formatLog(buffer); |
| 137 | + |
| 138 | + // Write the formated information to the file. |
| 139 | + fprintf(fptr, "%s", formattedLog); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + // Close all file pointers and free up memory space. |
| 144 | + pclose(shell_op); |
| 145 | + fclose(fptr); |
| 146 | + |
| 147 | + // Time Complexity = O(n); n = No. of saved wifi networks. |
| 148 | + // Space Complexity = O(1); |
| 149 | + |
| 150 | + return 0; |
| 151 | +} |
0 commit comments