Dalam beberapa pekerjaan. Menggunakan bot dapat mempermudah apa yang kita kerjakan. Namun seringkali kita hanya menggunakan bot atau program yang kita dapat dari internet saja. Kali ini kita akan coba membuat bot kita sendiri.

Kita akan memecahkan program menginginkan kita untuk menjumlahkan suatu nilai. Namun apabila kita terlalu lambat menyelesaikannya, maka program akan keluar.

Source code dari program ini dapat dilihat dibawah ini

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
#include <bits/stdc++.h>

using namespace std;

int main(){
 printf("Welcome to the first bot fight\nFigth this Bot if you can");
 printf("\n");
 srand(time(NULL));
 int lanjut = 1;
 long long jumlah;
 for (int i = 0; i < 100; ++i)
 {

  jumlah = 0;
  if(lanjut==0){
   break;
  }
  else
   printf("LANJUT GAN\n");
  for(int j = 0; j < i+2 ; j++){
   int temp;
   temp = rand()%10000;
   if(j==0){
    printf("%d", temp);

   }
   else{
    printf(" + %d", temp);
   }
   jumlah += temp;
  }
  printf("\n");
  long long input;
  int time0 = time(NULL);
  cin >> input;
  int time1 = time(NULL);
  printf("YOUR TIME : %d\n", time1 - time0);
  if(time1 - time0>=2){
   printf("LEBIH CEPET LAGI YUK GAN\n");
   return 0;
  }
  if(jumlah!=input){
   lanjut = 0;
  }
 }
 std::vector<char> v;
 char buf[1024];
 if (FILE *fp = fopen("flag.txt", "r"))
 {

  while (size_t len = fread(buf, 1, sizeof(buf), fp))
   v.insert(v.end(), buf, buf + len);
  fclose(fp);
 }

 if(lanjut==1){
  cout << buf << '\n';
  cout << "Kamu menang gan. Diatas nih flagnya";
 } else {
  printf("no flag for you sorry\n");
 }
 return 0;
}

Kita akan membuat bot kita menggunakan python dan library pwntools.

Kita coba lihat dahulu output dari program.

alfakatsuki@justPC:~/Desktop/Project/Bot$ ./bot1
Welcome to the first bot fight
Figth this Bot iff you can
LANJUT GAN
9164 + 4191
1
YOUR TIME : 2
LEBIH CEPET LAGI YUK GAN

Sesuai dari program maka mari kita buat bot sederhana kita untuk menyelesaikan program tersebut. Code sederhana kita bisa dilihat dibawah ini

from pwn import *

p = process('./bot1')
l = 0
while True:
 print p.recvuntil('LANJUT GAN\n')
 print 'prosess ke : '+str(l)
 a = p.recvline()
 print a
 k =  eval(a)
 print k
 p.sendline(str(k))
 l += 1
 #if(l>99):
 # break
print p.recvall()

Kita coba jalankan bot kita diterminal


prosess ke : 99
2470 + 2995 + 2417 + 1874 + 5032 + 7636 + 4166 + 8181 + 8796 + 8864 + 8700 + 69 + 5469 + 6239 + 1770 + 2091 + 7276 + 7983 + 9578 + 4671 + 917 + 8104 + 5894 + 8531 + 3852 + 8332 + 5210 + 1537 + 7882 + 8929 + 6666 + 352 + 1924 + 5436 + 8579 + 3309 + 3072 + 9097 + 7842 + 8220 + 7961 + 2895 + 8290 + 3431 + 9134 + 6412 + 1874 + 2763 + 4396 + 7804 + 7434 + 5313 + 5908 + 9680 + 3844 + 9760 + 8013 + 5406 + 1298 + 5895 + 687 + 4316 + 2599 + 8964 + 9752 + 1178 + 2273 + 9177 + 6628 + 6467 + 7397 + 4589 + 9362 + 2039 + 4372 + 8497 + 8452 + 6246 + 7612 + 2848 + 4050 + 5046 + 4513 + 6311 + 1079 + 4710 + 6071 + 5444 + 116 + 3721 + 7691 + 804 + 8038 + 6642 + 9768 + 4142 + 4173 + 8393 + 3319 + 801 + 4860

550620
[*] Process './bot1' stopped with exit code 0 (pid 26461)
Traceback (most recent call last):
  File "bot1.py", line 6, in <module>
    print p.recvuntil('LANJUT GAN\n')
  File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/tube.py", line 305, in recvuntil
    res = self.recv(timeout=self.timeout)
  File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/tube.py", line 78, in recv
    return self._recv(numb, timeout) or ''
  File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/tube.py", line 156, in _recv
    if not self.buffer and not self._fillbuffer(timeout):
  File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/tube.py", line 126, in _fillbuffer
    data = self.recv_raw(self.buffer.get_fill_size())
  File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/process.py", line 698, in recv_raw
    raise EOFError
EOFError
alfakatsuki@justPC:~/Desktop/Project/Bot$

Bot ini berhasil menyelesaikan tugas namun gagal untuk menerima flag. Karena itu kita tambahkan baris untuk memberhentikan program

from pwn import *
#kita import library pwntools
p = process('./bot1')
#jalankan program
l = 0
while True:
 print p.recvuntil('LANJUT GAN\n')
 print 'prosess ke : '+str(l)
 a = p.recvline()
 print a
 k =  eval(a)
  #menjumlahkan seluruh nilai
 print k
 p.sendline(str(k))
 l += 1
 if(l>99):
  break
  #proses memberhentikan
print p.recvall()

Kita lihat hasilnya di terminal

alfakatsuki@justPC:~/Desktop/Project/Bot$ python bot1.py
.
.
.
[+] Receiving all data: Done (77B)
[*] Process './bot1' stopped with exit code 0 (pid 12521)
YOUR TIME : 0
CTF{BOTS_important_to_US}\x1c�
Kamu menang gan. Diatas nih flagnya
alfakatsuki@justPC:~/Desktop/Project/Bot$

Sekian postingan sederhana untuk membuat bot. Semoga bermanfaat :)