1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | /* * * __author__: Suraj Singh Bisht * surajsinghbisht054@gmail.com * www.bitforestinfo.com * * ============================================================ * Created During C socket practise * ============================================================ * * Tested On : * Linux parrot 4.14.0-parrot13-amd64 #1 SMP Parrot 4.14.13-1parrot13 (2018-01-21) x86_64 GNU/Linux * gcc (Debian 7.3.0-5) 7.3.0 * * */
// include header #include <stdlib.h> // standard library #include <stdio.h> // Standard Input Output Library #include <sys/socket.h> // socket header #include <sys/types.h> // Data types #include <string.h> // String #include <netinet/in.h> #include <arpa/inet.h> // #include <unistd.h>
#define PORT_EXPRESSION_ITER 11
// Error Report On Screen void error(char *msg) { printf("[Error] %s\n", msg); printf("[X] Terminating Program...\n"); exit(0); }
// Data Type typedef struct list{ int data; int status; struct list * next; } plist;
// append nodes void listappend(plist *head, int val){ /* * * To Append New Node Into Linked List Structure. */
plist *tmp; // starting point if(head->next == NULL){ tmp = malloc(sizeof(plist)); if (tmp==NULL){ error("Error During Allocating Node For Port Storing List"); } tmp->data = val; tmp->next = NULL; tmp->status = 0; head->next = tmp; } else{ while(head->next!=NULL){ //printf("Checking Node %i\n", head->data); tmp = head->next; head = tmp;
//exit(0); } tmp = malloc(sizeof(plist)); tmp->data = val; tmp->next = NULL; tmp->status = 0; head->next = tmp; } }
void expandlist(plist *head){ /* * * This Function is dedicated To explore all node data value Into Terminal. * */ plist *tmp; while(head->next!=NULL){ printf("Node->Data %i\n", head->data); tmp = head->next; head = tmp;
//exit(0); } printf("Node->Data %i\n", head->data);
} void checkport(int sockd, struct sockaddr_in client, int port, char *ip, plist *node){ client.sin_port = htons(port); client.sin_addr.s_addr = inet_addr(ip); if(connect(sockd, (struct sockaddr*)&client, sizeof (client))<0){ node->status=0; printf("Close\n"); }else{ node->status=2; printf("Open\n"); }
}
void portscanner(plist *p, char *ip){ /* * This Function is Completely Dedicated To Perform Port scanning * process and save result into linked list status block * */ int sockd; plist *tmp; tmp = p->next; p = tmp; /* struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; */ struct sockaddr_in victim;
sockd = socket(AF_INET, SOCK_STREAM, 0); struct timeval timeout; timeout.tv_sec = 1; // after 1 seconds connect() will timeout timeout.tv_usec = 0;
setsockopt(sockd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
if (sockd<0){ error("[Error] During Creation Of Socket Handler"); } victim.sin_family = AF_INET; while (p->next!=NULL){ printf("Scanning IP :%s | Port : %i ---> ", ip, p->data); checkport(sockd, victim, p->data, ip, p); tmp = p->next; p = tmp;
} printf("Scanning IP :%s | Port : %i ---> ", ip, p->data); checkport(sockd, victim, p->data, ip, p); tmp = p->next; p = tmp;
close(sockd);
}
void ranger(char *exp, plist *p){ /* * * This Function is completely Dedicated To Generate Port Integers From Provided * Port Expression. * */
char s[6], e[6], *n; int sn, en, i;
// Get Tail n = strpbrk(exp, "-");
// Store it In another variable // surpass '-' char n++;
// copy value in ending variable strcpy(e, n); // convert string into integers en = atoi(e);
// get head n = strtok(exp, "-");
// store in another variable strcpy(s, n); // convert string into integers sn = atoi(s); // check range if (sn >= en) { //printf("Starting From : %s, End at : %s\n",s,e); //printf("Starting From : %i, End at : %i\n",sn,en); ; error("Incorrect Range Found"); } for (i = sn; i < en; i++) { listappend(p, i); } }
// Argument Parser And Processing Function void argument_parse(char *ip, char *port){ /* * * This Function Is Completely Dedicated To Extract Port Integers From Port Expressions * And Also Generate Port Integers From Provided Port Range Expression. * */
plist *p; // Port List Object Define
p = malloc(sizeof(plist)); // Allocate Space For List Object p->next = NULL; p->data = 0; p->status = -1;
int l = strlen(port); // length of characters in port expression char c; // to handle every single character of port expression char pnum[PORT_EXPRESSION_ITER]; int pn = 0, iport;
bzero(pnum, sizeof(pnum)); // make sure, space is clear
// iter all characters of port expresion for (int i = 0; i < l; i++) { c = port[i];
// check if character is a valid expression if ((c == ' ') | (c == '1') | (c == '2') | (c == '3') | (c == '4') | (c == '5') | (c == '6') | (c == '7') | (c == '8') | (c == '9') | (c == '0') | (c == '-') | (c == ',')) { // Start Storing Characters if (pn == 0) { bzero(pnum, sizeof(pnum)); pnum[pn] = c; // string starting pnum[PORT_EXPRESSION_ITER] = '\0'; // string end pn++; }
// Split Expression And Save Into List else if ((c == ',') & (pn != 0)) { // check if range expression if (strpbrk(pnum, "-")) { ranger(pnum, p); } else { iport = atoi(pnum); listappend(p ,iport); }
// Reset Input character collector pn = 0; bzero(pnum, sizeof(pnum)); }
else if ((pn == 5)&(!strpbrk(pnum, "-"))) { // check if range expression if (strpbrk(pnum, "-")) { ranger(pnum, p); } else { iport = atoi(pnum); listappend(p ,iport); }
// Reset Input character collector pn = 0; bzero(pnum, sizeof(pnum)); pnum[pn] = c; // string starting pnum[PORT_EXPRESSION_ITER] = '\0'; // string end pn++; } else if (pn < 5) { pnum[pn] = c; pn++; } else if (strpbrk(pnum, "-")) { pnum[pn] = c; pn++; } else {
printf("[-] Port Expression Droped Characters : %i\n", pn); } } else { //printf("%s", c); error("Please Provide Valid Port Range Expressions"); } }
iport = atoi(pnum); listappend(p ,iport); //expandlist(p);
// Reset Input character collector pn = 0; bzero(pnum, sizeof(pnum)); portscanner(p, ip);
}
// main function trigger int main(int argc, char const *argv[]){ /* * This Main Function Is Completely Dedicated To Verify And Filter User * Provided Arguments. * */
char ip[16]; // IP Address Expression char port[100]; // Port Range expression // if no arguments passed if (argc < 2) { printf(" Please Provide Arguments To Perform Function\n\n \ :~$ %s IP_ADDRESS PORT-RANGE\n\nSupported Port Expression 1,2,3-80,5 \n\t\t\t[Note] Range Supported Only In Middle Of Expression \n\t\t\t\t like port1,(Range: Port2-9) ,port10\n", argv[0]);
error("Program Terminating... \n\t\tCause :- No Input Provided."); }
// If Only One Argument is Provided else if (argc < 3) { printf("Please Provide Arguments To Perform Function\n\n \ :~$ %s IP_ADDRESS PORT-RANGE\n\n\n", argv[0]);
error("Program Terminating... \n\t\tCause : Insuffecient Input Provided."); }
// Check if Provided Arguments are valid to process else if ((argc == 3) & (strlen(argv[1]) < 16) & (strlen(argv[1]) > 8)) { printf("Starting CheckPort Program Design/Written By Surajsinghbisht054@gmail.com\n\n\n");
strcpy(ip, argv[1]); // Get IP address strcpy(port, argv[2]); // Get Port Range Expression argument_parse(ip, port); // Transfer Controls To Argument Parse Function }
// Unformatted Arguments Provided else { printf("Please Provide Correct Arguments To Perform Function\n\n \ :~$ %s IP_ADDRESS PORT-RANGE\n\n\n", argv[0]);
error("Program Terminating... \n\t\tCause : Unformatted Input Provided."); } return 0; }
|