summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHåkon Nessjøen <haakon.nessjoen@gmail.com>2010-09-27 13:28:19 +0200
committerHåkon Nessjøen <haakon.nessjoen@gmail.com>2010-09-27 13:28:19 +0200
commitf68b4c26f99cad5efb608fab92ccb7d2a54ca7a8 (patch)
treef11b7db8a508515d45a2e5817d0935773987fe1c
parentdcf815703b327bd6500c0bdea60cb85f67994d5a (diff)
downloadMAC-Telnet-f68b4c26f99cad5efb608fab92ccb7d2a54ca7a8.tar.gz
MAC-Telnet-f68b4c26f99cad5efb608fab92ccb7d2a54ca7a8.zip
Started code for automatically defining src ip/mac for packets
-rw-r--r--Makefile2
-rw-r--r--devices.c49
-rw-r--r--devices.h2
-rw-r--r--main.c34
4 files changed, 77 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 01dfb0a..e911bec 100644
--- a/Makefile
+++ b/Makefile
@@ -7,4 +7,4 @@ dist-clean:
rm -f mactelnet
mactelnet: config.h main.c udp.h udp.c mactelnet.c mactelnet.h console.c console.h
- gcc -g -o mactelnet -lcrypto main.c udp.c mactelnet.c console.c
+ gcc -g -o mactelnet -lcrypto main.c udp.c mactelnet.c console.c devices.c
diff --git a/devices.c b/devices.c
new file mode 100644
index 0000000..14ba5c4
--- /dev/null
+++ b/devices.c
@@ -0,0 +1,49 @@
+#include <stdlib.h>
+#include <string.h>
+#include <malloc.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+
+int getDeviceIndex(int sockfd, unsigned char *deviceName) {
+ struct ifreq ifr;
+
+ strncpy(ifr.ifr_name, deviceName, 16);
+ if (ioctl(sockfd, SIOCGIFINDEX, &ifr) != 0) {
+ return -1;
+ }
+
+ return ifr.ifr_ifindex;
+}
+
+int getDeviceIp(const int sockfd, const unsigned char *deviceName, struct sockaddr_in *ip) {
+ struct ifconf ifc;
+ struct ifreq *ifr;
+ int i,numDevices;
+
+ memset(&ifc, 0, sizeof(ifc));
+ if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) {
+ return -1;
+ }
+
+ if ((ifr = malloc(ifc.ifc_len * 2)) == NULL) {
+ perror("malloc");
+ exit(1);
+ }
+
+ ifc.ifc_req = ifr;
+
+ if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) {
+ return -1;
+ }
+
+ numDevices = ifc.ifc_len / sizeof(struct ifreq);
+ for (i = 0; i < numDevices; ++i) {
+ if (strcmp(ifr[i].ifr_name, deviceName) == 0) {
+ memcpy(ip, &(ifr[i].ifr_addr), sizeof(ip));
+ return 1;
+ }
+ }
+ return -1;
+}
diff --git a/devices.h b/devices.h
new file mode 100644
index 0000000..69ba246
--- /dev/null
+++ b/devices.h
@@ -0,0 +1,2 @@
+extern int getDeviceIndex(int sockfd, unsigned char *deviceName);
+extern int getDeviceIp(const int sockfd, const unsigned char *deviceName, struct sockaddr_in *ip);
diff --git a/main.c b/main.c
index bde22c1..60dbd3c 100644
--- a/main.c
+++ b/main.c
@@ -28,6 +28,7 @@
#include "mactelnet.h"
#include "udp.h"
#include "console.h"
+#include "devices.h"
#include "config.h"
int sockfd;
@@ -146,15 +147,16 @@ int main (int argc, char **argv) {
struct sockaddr_in si_me;
char buff[1500];
int plen = 0;
+ int deviceIndex;
- if (argc < 3) {
- fprintf(stderr, "Usage: %s <MAC> <username> <password>\n", argv[0]);
+ if (argc < 4) {
+ fprintf(stderr, "Usage: %s <ifname> <MAC> <username> <password>\n", argv[0]);
return 1;
}
- strncpy(dst, argv[1], 17);
- strncpy(username, argv[2], 254);
- strncpy(password, argv[3], 254);
+ strncpy(dst, argv[2], 17);
+ strncpy(username, argv[3], 254);
+ strncpy(password, argv[4], 254);
srand(time(NULL));
@@ -164,15 +166,29 @@ int main (int argc, char **argv) {
// Receive regular udp packets with this socket
insockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- // Initialize receiving socket
+ deviceIndex = getDeviceIndex(sockfd, argv[1]);
+ if (deviceIndex < 0) {
+ fprintf(stderr, "Device %s not found.\n", argv[1]);
+ return 1;
+ }
+
+ // Even though we talk to the server without IP address, it makes it much
+ // easier to read packets when we use our real ip as the sender ip.
+ // This way we can listen to normal UDP traffic on port 20561
+ result = getDeviceIp(sockfd, argv[1], &si_me);
+ if (result < 0) {
+ fprintf(stderr, "Cannot determine IP of device %s\n", argv[1]);
+ return 1;
+ }
+
+ // Initialize receiving socket on the device chosen
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(20561);
- si_me.sin_addr.s_addr = htonl(INADDR_ANY);
// Bind to udp port
if (bind(insockfd, (struct sockaddr *)&si_me, sizeof(si_me))==-1) {
- fprintf(stderr, "Error binding to port 20561\n");
+ fprintf(stderr, "Error binding to %s:20561\n", inet_ntoa(si_me.sin_addr));
return 1;
}
@@ -182,7 +198,7 @@ int main (int argc, char **argv) {
printf("Connecting to %s...\n", dst);
plen = initPacket(data, MT_PTYPE_SESSIONSTART, src, dst, sessionkey, 0);
- result = sendCustomUDP(sockfd, src, dst, "213.236.240.252", 20561, "255.255.255.255", 20561, data, plen);
+ result = sendCustomUDP(sockfd, src, dst, inet_ntoa(si_me.sin_addr), 20561, "255.255.255.255", 20561, data, plen);
if (DEBUG)
printf("Plen = %d, Send result: %d\n", plen, result);
if (DEBUG)